コード例 #1
0
        private void Remove(string key, bool publishCleanCache)
        {
            try
            {
                if (IsExists(key))
                {
                    Db.KeyDelete(key);
                }

                CurrentRuntimeCache.Remove(key);

                if (publishCleanCache)
                {
                    PublishCleanCache(key);
                }
            }
            catch (RedisConnectionException ex)
            {
                CacheManager.UseBackupConnection(ex).Remove(key);
            }
            catch
            {
                // ignored
            }
        }
コード例 #2
0
        public bool TryGet <T>(string key, out T value) where T : class
        {
            try
            {
                if (CurrentRuntimeCache.TryGet(key, out value))
                {
                    return(true);
                }

                if (!IsExists(key))
                {
                    value = default(T);
                    return(false);
                }
                value = Get <T>(key);
            }
            catch (RedisConnectionException ex)
            {
                return(CacheManager.UseBackupConnection(ex).TryGet(key, out value));
            }
            catch
            {
                value = default(T);
                return(false);
            }
            return(true);
        }
コード例 #3
0
        public T Get <T>(string key)
        {
            var model = default(T);

            try
            {
                var runtimeCacheObject = CurrentRuntimeCache.Get <T>(key);
                if (runtimeCacheObject != null)
                {
                    return(runtimeCacheObject);
                }

                model = Db.StringGet(key).ToString().FromJson <T>();
            }
            catch (RedisConnectionException ex)
            {
                return(CacheManager.UseBackupConnection(ex).Get <T>(key));
            }
            catch
            {
                // ignored
            }

            return(model);
        }
コード例 #4
0
        internal void SubscribeToCacheUpdate()
        {
            var subscriber = Connection.GetSubscriber();

            subscriber.Subscribe(KeyRedisChannelNameForUpdate, (channel, message) =>
            {
                if (channel != KeyRedisChannelNameForUpdate)
                {
                    return;
                }

                var messageStr = (string)message;

                if (string.IsNullOrWhiteSpace(messageStr))
                {
                    return;
                }

                try
                {
                    var model = messageStr.FromJson <CacheUpdateModel>();

                    // Yayınlayan bu sunucuysa işlem yapmaması için
                    if (model.Publisher == PublisherId)
                    {
                        return;
                    }

                    if (model.Key.StartsWith("##remove##"))
                    {
                        var key = model.Key.ReplaceFirst("##remove##", "");
                        CurrentRuntimeCache.Remove(key);
                        return;
                    }

                    if (model.Key.StartsWith("##removeall##"))
                    {
                        var prefix = model.Key.ReplaceFirst("##removeall##", "");
                        CurrentRuntimeCache.RemoveAll(prefix);
                        return;
                    }

                    // Gelen nesnenin anlık tipini bilmediğimiz için Json dönüştürücü bunu yapamıyor
                    // Model üzerinden tip okunup, modeldeki json bu tipe göre nesneye çevriliyor.
                    var cacheItemValueJson = JObject.Parse(messageStr)["Value"].ToString();
                    var value = JsonConvert.DeserializeObject(cacheItemValueJson, model.ValueType);

                    // Rediste zaten var o yüzden sadece runtimecache'de değişecek.
                    CurrentRuntimeCache.Set(model.Key, value, model.ExpireDate);
                }
                catch
                {
                    // ignored
                }
            });
        }
コード例 #5
0
        public bool IsExists(string key)
        {
            try
            {
                return(CurrentRuntimeCache.IsExists(key) || Db.KeyExists(key));
            }
            catch (RedisConnectionException ex)
            {
                CacheManager.UseBackupConnection(ex).IsExists(key);
            }
            catch
            {
                // ignored
            }

            return(false);
        }
コード例 #6
0
        public object Get(string key)
        {
            try
            {
                var runtimeCacheObject = CurrentRuntimeCache.Get(key);
                return(runtimeCacheObject ?? Db.StringGet(key).ToString().FromJson <object>());
            }
            catch (RedisConnectionException ex)
            {
                return(CacheManager.UseBackupConnection(ex).Get(key));
            }
            catch
            {
                // ignored
            }

            return(null);
        }
コード例 #7
0
        public void Set(string key, object value, DateTime absoluteExpration)
        {
            try
            {
                var valueJson = JsonConvert.SerializeObject(value);
                Db.StringSet(key, valueJson, absoluteExpration.ToUniversalTime().Subtract(DateTime.UtcNow));

                CurrentRuntimeCache.Set(key, value, absoluteExpration);

                Publish(key, value, absoluteExpration);
            }
            catch (RedisConnectionException ex)
            {
                CacheManager.UseBackupConnection(ex).Set(key, value, absoluteExpration);
            }
            catch
            {
                // ignored
            }
        }
コード例 #8
0
        public bool RemoveAll(string prefix = "")
        {
            try
            {
                foreach (var key in GetRedisKeys(prefix))
                {
                    Current.Remove(key, false);
                }

                CurrentRuntimeCache.RemoveAll(prefix);

                PublishCleanAllCache(prefix);
            }
            catch (RedisConnectionException ex)
            {
                return(CacheManager.UseBackupConnection(ex).RemoveAll(prefix));
            }
            catch
            {
                return(false);
            }
            return(true);
        }