Exemple #1
0
        public T Get(string key, string region)
        {
            key = CusKeyHelper.GetKey(_options.RedisOcelotKeyPrefix, region, key);
            if (region == nameof(DiffClientRateLimitCounter) && _options.ClusterEnvironment)
            {//限流且开启了集群支持,默认从redis取
                return(RedisHelper.Get <T>(key));
            }
            var result = _cache.Get <T>(key);

            if (result == null && _options.ClusterEnvironment)
            {
                result = RedisHelper.Get <T>(key);
                if (result != null)
                {
                    if (typeof(T) == typeof(CachedResponse))
                    {//查看redis过期时间
                        var second = RedisHelper.Ttl(key);
                        if (second > 0)
                        {
                            _cache.Set(key, result, TimeSpan.FromSeconds(second));
                        }
                    }
                    else
                    {
                        _cache.Set(key, result, TimeSpan.FromSeconds(_options.CacheTime));
                    }
                }
            }
            return(result);
        }
Exemple #2
0
 public void Add(string key, T value, TimeSpan ttl, string region)
 {
     key = CusKeyHelper.GetKey(_options.RedisOcelotKeyPrefix, region, key);
     if (_options.ClusterEnvironment)
     {
         var msg = value.ToJson();
         if (typeof(T) == typeof(CachedResponse))
         {                                  //带过期时间的缓存
             _cache.Set(key, value, ttl);   //添加本地缓存
             RedisHelper.Set(key, msg);     //加入redis缓存
             RedisHelper.Publish(key, msg); //发布
         }
         else if (typeof(T) == typeof(DiffClientRateLimitCounter?))
         {//限流缓存,直接使用redis
             RedisHelper.Set(key, value, (int)ttl.TotalSeconds);
         }
         else
         {                                  //正常缓存,发布
             _cache.Set(key, value, ttl);   //添加本地缓存
             RedisHelper.Set(key, msg);     //加入redis缓存
             RedisHelper.Publish(key, msg); //发布
         }
     }
     else
     {
         _cache.Set(key, value, ttl); //添加本地缓存
     }
 }