应用程序的缓存模块

匿名 (未验证) 提交于 2019-12-02 23:47:01

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 =>             {                 cacheOptions.GlobalCacheEntryOptions.SlidingExpiration = TimeSpan.FromMinutes(20);             });         }     } 

where TCacheItem : class,同一类型,不同命名空间不使用同一缓存

1、Get方法,key

2、Set方法

     /// <summary>         /// Sets the cache item value for the provided key.         /// </summary>         /// <param name="key">The key of cached item to be retrieved from the cache.</param>         /// <param name="value">The cache item value to set in the cache.</param>         /// <param name="options">The cache options for the value.</param>         /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>         /// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param>         /// <returns>The <see cref="T:System.Threading.Tasks.Task" /> indicating that the operation is asynchronous.</returns>         Task SetAsync(             [NotNull] string key,             [NotNull] TCacheItem value,             [CanBeNull] DistributedCacheEntryOptions options = null,             bool? hideErrors = null,             CancellationToken token = default         ); 

 

缓存的Get和Set方法

/// <summary>         /// 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 <paramref name="factory" /> delegate and returns the provided cache item.         /// </summary>         /// <param name="key">The key of cached item to be retrieved from the cache.</param>         /// <param name="factory">The factory delegate is used to provide the cache item when no cache item is found for the given <paramref name="key" />.</param>         /// <param name="optionsFactory">The cache options for the factory delegate.</param>         /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>         /// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param>         /// <returns>The cache item.</returns>         public async Task<TCacheItem> GetOrAddAsync(             string key,             Func<Task<TCacheItem>> factory,             Func<DistributedCacheEntryOptions> 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;         } 

  

        /// <summary>         /// Sets the cache item value for the provided key.         /// </summary>         /// <param name="key">The key of cached item to be retrieved from the cache.</param>         /// <param name="value">The cache item value to set in the cache.</param>         /// <param name="options">The cache options for the value.</param>         /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>         /// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param>         /// <returns>The <see cref="T:System.Threading.Tasks.Task" /> indicating that the operation is asynchronous.</returns>         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;             }         } 

  

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!