예제 #1
0
        public T Get(string key, string region)
        {
            key = CzarOcelotHelper.GetKey(_options.RedisOcelotKeyPrefix, region, key);
            if (region == CzarCacheRegion.CzarClientRateLimitCounterRegion && _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.CzarCacheTime));
                    }
                }
            }
            return(result);
        }
예제 #2
0
 public void Add(string key, T value, TimeSpan ttl, string region)
 {
     key = CzarOcelotHelper.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, (int)ttl.TotalSeconds); //加入redis缓存
             RedisHelper.Publish(key, msg);                    //发布
         }
         else if (typeof(T) == typeof(CzarClientRateLimitCounter?))
         {//限流缓存,直接使用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); //添加本地缓存
     }
 }
예제 #3
0
        public async Task DeleteResponseCache(string region, string downurl)
        {
            var key = CzarOcelotHelper.GetKey(_options.RedisOcelotKeyPrefix, region, downurl);

            if (_options.ClusterEnvironment)
            {
                await RedisHelper.DelAsync(key);

                RedisHelper.Publish(key, "");//发布时间
            }
            else
            {
                _cache.Remove(key);
            }
        }
예제 #4
0
        public async Task UpdateRpcCache(string UpUrl)
        {
            var region = CzarCacheRegion.RemoteInvokeMessageRegion;
            var key    = UpUrl;

            key = CzarOcelotHelper.GetKey(_options.RedisOcelotKeyPrefix, region, key);
            var result = await _rpcRepository.GetRemoteMethodAsync(UpUrl);

            if (_options.ClusterEnvironment)
            {
                RedisHelper.Set(key, result);              //加入redis缓存
                RedisHelper.Publish(key, result.ToJson()); //发布事件
            }
            else
            {
                _cache.Remove(key);
            }
        }
예제 #5
0
        public async Task UpdateInternalConfigurationCache()
        {
            var key = CzarCacheRegion.InternalConfigurationRegion;

            key = CzarOcelotHelper.GetKey(_options.RedisOcelotKeyPrefix, "", key);
            var fileconfig = await _fileConfigurationRepository.Get();

            var internalConfig = await _internalConfigurationCreator.Create(fileconfig.Data);

            var config = (InternalConfiguration)internalConfig.Data;

            if (_options.ClusterEnvironment)
            {
                RedisHelper.Set(key, config);              //加入redis缓存
                RedisHelper.Publish(key, config.ToJson()); //发布事件
            }
            else
            {
                _cache.Remove(key);
            }
        }
예제 #6
0
        public async Task UpdateClientRuleCache(string clientid, string path)
        {
            var region = CzarCacheRegion.AuthenticationRegion;
            var key    = CzarOcelotHelper.ComputeCounterKey(region, clientid, "", path);

            key = CzarOcelotHelper.GetKey(_options.RedisOcelotKeyPrefix, region, key);
            var result = await _clientAuthenticationRepository.ClientAuthenticationAsync(clientid, path);

            var data = new ClientRoleModel()
            {
                CacheTime = DateTime.Now, Role = result
            };

            if (_options.ClusterEnvironment)
            {
                RedisHelper.Set(key, data);              //加入redis缓存
                RedisHelper.Publish(key, data.ToJson()); //发布事件
            }
            else
            {
                _cache.Remove(key);
            }
        }
예제 #7
0
        public async Task UpdateClientReRouteWhiteListCache(string clientid, string path)
        {
            var region = CzarCacheRegion.ClientReRouteWhiteListRegion;
            var key    = clientid + path;

            key = CzarOcelotHelper.GetKey(_options.RedisOcelotKeyPrefix, region, key);
            var result = await _clientRateLimitRepository.CheckClientReRouteWhiteListAsync(clientid, path);

            var data = new ClientRoleModel()
            {
                CacheTime = DateTime.Now, Role = result
            };

            if (_options.ClusterEnvironment)
            {
                RedisHelper.Set(key, data);              //加入redis缓存
                RedisHelper.Publish(key, data.ToJson()); //发布事件
            }
            else
            {
                _cache.Remove(key);
            }
        }
예제 #8
0
        public async Task UpdateRateLimitRuleCache(string clientid, string path)
        {
            var region = CzarCacheRegion.RateLimitRuleModelRegion;
            var key    = clientid + path;

            key = CzarOcelotHelper.GetKey(_options.RedisOcelotKeyPrefix, region, key);
            var result = await _clientRateLimitRepository.CheckClientRateLimitAsync(clientid, path);

            var data = new RateLimitRuleModel()
            {
                RateLimit = result.RateLimit, rateLimitOptions = result.rateLimitOptions
            };

            if (_options.ClusterEnvironment)
            {
                RedisHelper.Set(key, data);              //加入redis缓存
                RedisHelper.Publish(key, data.ToJson()); //发布事件
            }
            else
            {
                _cache.Remove(key);
            }
        }