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