Browse Source

Implemented basic DistributedCache<TCacheItem>.

pull/216/head
Halil İbrahim Kalkan 9 years ago
parent
commit
ea2c646402
  1. 2
      src/Volo.Abp.Caching/Volo.Abp.Caching.csproj
  2. 2
      src/Volo.Abp.Caching/Volo/Abp/Caching/AbpCachingModule.cs
  3. 35
      src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs

2
src/Volo.Abp.Caching/Volo.Abp.Caching.csproj

@ -18,7 +18,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Volo.Abp.Core\Volo.Abp.Core.csproj" /> <ProjectReference Include="..\Volo.Abp.Serialization\Volo.Abp.Serialization.csproj" />
<ProjectReference Include="..\Volo.Abp.Threading\Volo.Abp.Threading.csproj" /> <ProjectReference Include="..\Volo.Abp.Threading\Volo.Abp.Threading.csproj" />
</ItemGroup> </ItemGroup>

2
src/Volo.Abp.Caching/Volo/Abp/Caching/AbpCachingModule.cs

@ -1,10 +1,12 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Volo.Abp.Serialization;
using Volo.Abp.Threading; using Volo.Abp.Threading;
namespace Volo.Abp.Caching namespace Volo.Abp.Caching
{ {
[DependsOn(typeof(AbpThreadingModule))] [DependsOn(typeof(AbpThreadingModule))]
[DependsOn(typeof(AbpSerializationModule))]
public class AbpCachingModule : AbpModule public class AbpCachingModule : AbpModule
{ {
public override void ConfigureServices(IServiceCollection services) public override void ConfigureServices(IServiceCollection services)

35
src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs

@ -2,6 +2,7 @@
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Caching.Distributed;
using Volo.Abp.Serialization;
using Volo.Abp.Threading; using Volo.Abp.Threading;
namespace Volo.Abp.Caching namespace Volo.Abp.Caching
@ -10,35 +11,49 @@ namespace Volo.Abp.Caching
where TCacheItem : class where TCacheItem : class
{ {
protected IDistributedCache Cache { get; } protected IDistributedCache Cache { get; }
protected ICancellationTokenProvider CancellationTokenProvider { get; }
public ICancellationTokenProvider CancellationTokenProvider { get; } protected IObjectSerializer ObjectSerializer { get; }
public DistributedCache( public DistributedCache(
IDistributedCache cache, IDistributedCache cache,
ICancellationTokenProvider cancellationTokenProvider) ICancellationTokenProvider cancellationTokenProvider,
IObjectSerializer objectSerializer)
{ {
Cache = cache; Cache = cache;
CancellationTokenProvider = cancellationTokenProvider; CancellationTokenProvider = cancellationTokenProvider;
ObjectSerializer = objectSerializer;
} }
public async Task<TCacheItem> GetAsync(string key, CancellationToken token = default) public async Task<TCacheItem> GetAsync(
string key,
CancellationToken token = default)
{ {
var cachedValue = await Cache.GetAsync(key, CancellationTokenProvider.FallbackToProvider(token)); var cachedBytes = await Cache.GetAsync(key, CancellationTokenProvider.FallbackToProvider(token));
if (cachedValue == null) if (cachedBytes == null)
{ {
return null; return null;
} }
throw new NotImplementedException(); return ObjectSerializer.Deserialize<TCacheItem>(cachedBytes);
} }
public Task SetAsync(string key, TCacheItem value, DistributedCacheEntryOptions options = null, public Task SetAsync(
string key,
TCacheItem value,
DistributedCacheEntryOptions options = null,
CancellationToken token = default) CancellationToken token = default)
{ {
throw new System.NotImplementedException(); return Cache.SetAsync(
key,
ObjectSerializer.Serialize(value),
options ?? new DistributedCacheEntryOptions { SlidingExpiration = TimeSpan.FromMinutes(20) }, //TODO: implement per cache item and global defaults!!!
CancellationTokenProvider.FallbackToProvider(token)
);
} }
public Task RemoveAsync(string key, CancellationToken token = default) public Task RemoveAsync(
string key,
CancellationToken token = default)
{ {
return Cache.RemoveAsync(key, CancellationTokenProvider.FallbackToProvider(token)); return Cache.RemoveAsync(key, CancellationTokenProvider.FallbackToProvider(token));
} }

Loading…
Cancel
Save