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