Ejemplo n.º 1
0
        ///// <summary>
        ///// 启用Redis TODO
        ///// 启用Redis TODO
        ///// </summary>
        ///// <param name="configuration">配置</param>
        ///// <param name="useHybridMode">是否启用混合模式</param>
        //public static void UseRedis(IConfiguration configuration, bool useHybridMode = false)
        //{
        //    RedisCacheOptions options = new RedisCacheOptions();
        //    SetRedisCacheOptions(configuration, options);

        //    // 设置配置
        //    DependencyConfigurator.RegisterInstance<IOptions<RedisCacheOptions>, RedisCacheOptions>(options);

        //    if (useHybridMode)
        //        DependencyConfigurator.RegisterType<IRedisCacheManager, DefaultRedisCacheManager>();
        //    else
        //        DependencyConfigurator.RegisterType<ICacheManager, DefaultRedisCacheManager>();

        //    Console.WriteLine("Redis注入完成。");
        //}

        /// <summary>
        /// 设置
        /// </summary>
        /// <param name="configuration">配置</param>
        /// <param name="options">配置对象</param>
        internal static void SetRedisCacheOptions(IConfiguration configuration, RedisCacheOptions options)
        {
            string host = configuration.GetValue <string>("Cache:Redis:Host");

            if (string.IsNullOrWhiteSpace(host))
            {
                throw new ConfigException("Cache.Redis.Host");
            }
            string port = configuration.GetValue <string>("Cache:Redis:Port");

            if (string.IsNullOrWhiteSpace(port))
            {
                port = "6379";
            }
            string password = configuration.GetValue <string>("Cache:Redis:Password");
            string dbStr    = configuration.GetValue <string>("Cache:Redis:Db");
            int    db       = 0;

            if (!string.IsNullOrWhiteSpace(dbStr) && int.TryParse(dbStr, out int result))
            {
                db = result;
            }

            options.Host     = host;
            options.Port     = port;
            options.Password = password;
            options.Db       = db;

            // 设置缓存区域
            CacheKeyConverter.SetRegion(configuration);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 解锁实例
 /// </summary>
 /// <param name="key">key</param>
 /// <param name="value">值</param>
 private void UnlockInstance(string key, byte[] value)
 {
     RedisKey[]   keys   = { CacheKeyConverter.GetKeyWithRegion(key) };
     RedisValue[] values = { value };
     GetDatebase().ScriptEvaluate(
         UnlockScript,
         keys,
         values
         );
 }
Ejemplo n.º 3
0
        public T GetOrAdd <T>(string key, Func <T> getData, TimeSpan?expiry = null)
        {
            var value = GetDatebase().StringGet(CacheKeyConverter.GetKeyWithRegion(key));

            if (value.HasValue)
            {
                return(GetValue <T>(value));
            }

            var mutexKey = string.Format(MutexKeyFormat, key);

            // 防击穿(热点key并发)
            var uniqueValue = CreateUniqueLockId();

            try
            {
                if (SetNotExists(mutexKey, uniqueValue, TimeSpan.FromMinutes(1)))
                {
                    var result = getData();
                    _loggerLazy.Value.LogInformation($"GetOrAdd, [key:{key}], call getData func");

                    //防穿透
                    if (result == null)
                    {
                        Set(key, default(T), TimeSpan.FromSeconds(1));
                    }
                    else
                    {
                        Set(key, result, expiry);
                    }

                    return(result);
                }
            }
            finally
            {
                UnlockInstance(mutexKey, uniqueValue);
            }

            Thread.Sleep(50);
            return(GetOrAdd(key, getData, expiry));
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 设置不存在的值
 /// </summary>
 /// <param name="key">缓存键</param>
 /// <param name="value">值</param>
 /// <param name="expiry">过期时间</param>
 /// <returns></returns>
 private bool SetNotExists(string key, byte[] value, TimeSpan?expiry = null)
 {
     return(GetDatabase().StringSet(CacheKeyConverter.GetKeyWithRegion(key), value, expiry, When.NotExists));
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 删除指定key
 /// </summary>
 /// <param name="key">缓存键</param>
 /// <returns></returns>
 public bool Delete(string key)
 {
     return(GetDatabase().KeyDelete(CacheKeyConverter.GetKeyWithRegion(key)));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// 设置数据
 /// </summary>
 /// <typeparam name="T">数据类型</typeparam>
 /// <param name="key">缓存键</param>
 /// <param name="value">值</param>
 /// <param name="expiry">过期时间</param>
 /// <returns></returns>
 public bool Set <T>(string key, T value, TimeSpan?expiry = null)
 {
     return(GetDatabase().StringSet(CacheKeyConverter.GetKeyWithRegion(key), JsonUtil.ToJson(value), expiry));
 }
Ejemplo n.º 7
0
 /// <summary>
 /// 获取key的数据
 /// </summary>
 /// <typeparam name="T">数据类型</typeparam>
 /// <param name="key">缓存键</param>
 /// <returns></returns>
 public T Get <T>(string key)
 {
     return(GetValue <T>(GetDatabase().StringGet(CacheKeyConverter.GetKeyWithRegion(key))));
 }
Ejemplo n.º 8
0
 public bool Set <T>(string key, T value, TimeSpan?expiry = null)
 {
     return(GetDatebase().StringSet(CacheKeyConverter.GetKeyWithRegion(key), JsonHelper.SerializeObject(value), expiry));
 }