diff --git a/src/Volo.Abp.Caching/Volo.Abp.Caching.csproj b/src/Volo.Abp.Caching/Volo.Abp.Caching.csproj index e8779ef390..6ce669a75b 100644 --- a/src/Volo.Abp.Caching/Volo.Abp.Caching.csproj +++ b/src/Volo.Abp.Caching/Volo.Abp.Caching.csproj @@ -18,7 +18,7 @@ - + diff --git a/src/Volo.Abp.Caching/Volo/Abp/Caching/AbpCachingModule.cs b/src/Volo.Abp.Caching/Volo/Abp/Caching/AbpCachingModule.cs index aca65dd1f3..0274b2db8c 100644 --- a/src/Volo.Abp.Caching/Volo/Abp/Caching/AbpCachingModule.cs +++ b/src/Volo.Abp.Caching/Volo/Abp/Caching/AbpCachingModule.cs @@ -1,10 +1,12 @@ using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Modularity; +using Volo.Abp.Serialization; using Volo.Abp.Threading; namespace Volo.Abp.Caching { [DependsOn(typeof(AbpThreadingModule))] + [DependsOn(typeof(AbpSerializationModule))] public class AbpCachingModule : AbpModule { public override void ConfigureServices(IServiceCollection services) diff --git a/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs b/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs index 78bfd77cf0..a43011c82b 100644 --- a/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs +++ b/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs @@ -2,6 +2,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Caching.Distributed; +using Volo.Abp.Serialization; using Volo.Abp.Threading; namespace Volo.Abp.Caching @@ -10,35 +11,49 @@ namespace Volo.Abp.Caching where TCacheItem : class { protected IDistributedCache Cache { get; } - - public ICancellationTokenProvider CancellationTokenProvider { get; } + protected ICancellationTokenProvider CancellationTokenProvider { get; } + protected IObjectSerializer ObjectSerializer { get; } public DistributedCache( IDistributedCache cache, - ICancellationTokenProvider cancellationTokenProvider) + ICancellationTokenProvider cancellationTokenProvider, + IObjectSerializer objectSerializer) { Cache = cache; CancellationTokenProvider = cancellationTokenProvider; + ObjectSerializer = objectSerializer; } - public async Task GetAsync(string key, CancellationToken token = default) + public async Task GetAsync( + string key, + CancellationToken token = default) { - var cachedValue = await Cache.GetAsync(key, CancellationTokenProvider.FallbackToProvider(token)); - if (cachedValue == null) + var cachedBytes = await Cache.GetAsync(key, CancellationTokenProvider.FallbackToProvider(token)); + if (cachedBytes == null) { return null; } - throw new NotImplementedException(); + return ObjectSerializer.Deserialize(cachedBytes); } - public Task SetAsync(string key, TCacheItem value, DistributedCacheEntryOptions options = null, + public Task SetAsync( + string key, + TCacheItem value, + DistributedCacheEntryOptions options = null, 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)); }