• 缓存模块


    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);
                });
            }
        }
    

      2、IDistributedCache<TCacheItem> 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;
                }
            }
    

      在Redis里面的定义 

      context.Services.AddDistributedRedisCache(options =>
                {
                    options.Configuration = configuration["Redis:Configuration"];
                });
    

      

  • 相关阅读:
    [Usaco2005 open]Expedition
    舞会
    双栈维护之--Hdu4699 editor
    利用两个堆来维护第K大之Poj3784 Running Median
    Zju1061Web Navigation 网络导航
    Qsort求静态的第K大
    BZOJ2726【SDOI2012】任务安排(斜率优化Dp+二分查找)
    P2365 任务安排 斜率优化入门
    任务处理--斜率优化Dp入门
    结构体排序教学
  • 原文地址:https://www.cnblogs.com/cloudsu/p/11168958.html
Copyright © 2020-2023  润新知