1、模块的生命周期定义
添加AddMemoryCache(),AddDistributedMemoryCache()
注册泛型的服务为单例。缓存的类型为class
slidingExpiration:用于设置可调过期时间,它表示当离最后访问超过某个时间段(20分钟)后就过期
[DependsOn( typeof(AbpThreadingModule), typeof(AbpSerializationModule), typeof(AbpMultiTenancyModule), typeof(AbpJsonModule))] public class AbpCachingModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.AddMemoryCache(); context.Services.AddDistributedMemoryCache(); context.Services.AddSingleton(typeof(IDistributedCache<>), typeof(DistributedCache<>)); context.Services.Configure(cacheOptions => { cacheOptions.GlobalCacheEntryOptions.SlidingExpiration = TimeSpan.FromMinutes(20); }); } }
2、IDistributedCache
1、Get方法,key
2、Set方法
////// Sets the cache item value for the provided key. /// /// The key of cached item to be retrieved from the cache. /// The cache item value to set in the cache. /// The cache options for the value. /// Indicates to throw or hide the exceptions for the distributed cache. /// Thefor the task. /// The Task SetAsync( [NotNull] string key, [NotNull] TCacheItem value, [CanBeNull] DistributedCacheEntryOptions options = null, bool? hideErrors = null, CancellationToken token = default );indicating that the operation is asynchronous.
缓存的Get和Set方法
////// Gets or Adds a cache item with the given key. If no cache item is found for the given key then adds a cache item /// provided by /// The key of cached item to be retrieved from the cache. /// The factory delegate is used to provide the cache item when no cache item is found for the givendelegate and returns the provided cache item. /// . /// The cache options for the factory delegate. /// Indicates to throw or hide the exceptions for the distributed cache. /// The for the task. /// The cache item. public async TaskGetOrAddAsync( string key, Func > factory, Func optionsFactory = null, bool? hideErrors = null, CancellationToken token = default) { token = CancellationTokenProvider.FallbackToProvider(token); var value = await GetAsync(key, hideErrors, token); if (value != null) { return value; } using (await AsyncLock.LockAsync(token)) { value = await GetAsync(key, hideErrors, token); if (value != null) { return value; } value = await factory(); await SetAsync(key, value, optionsFactory?.Invoke(), hideErrors, token); } return value; }
////// Sets the cache item value for the provided key. /// /// The key of cached item to be retrieved from the cache. /// The cache item value to set in the cache. /// The cache options for the value. /// Indicates to throw or hide the exceptions for the distributed cache. /// Thefor the task. /// The public virtual async Task SetAsync( string key, TCacheItem value, DistributedCacheEntryOptions options = null, bool? hideErrors = null, CancellationToken token = default) { hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; try { await Cache.SetAsync( NormalizeKey(key), Serializer.Serialize(value), options ?? DefaultCacheOptions, CancellationTokenProvider.FallbackToProvider(token) ); } catch (Exception ex) { if (hideErrors == true) { Logger.LogException(ex, LogLevel.Warning); return; } throw; } }indicating that the operation is asynchronous.
在Redis里面的定义
context.Services.AddDistributedRedisCache(options => { options.Configuration = configuration["Redis:Configuration"]; });