public void AddOrUpdateValue(string key, object value, TimeSpan?expiryPeriod = null) { var ser = JsonConvert.SerializeObject(value); _cacheRepo.AddOrUpdate(new CacheEntry { Key = key, Value = ser, ExpiresIn = expiryPeriod }); #if DEBUG if (expiryPeriod == null) { System.Diagnostics.Debug.WriteLine($"Cache {key} added"); } else { System.Diagnostics.Debug.WriteLine($"Cache {key} added and will expire in {expiryPeriod.ToString()}."); } #endif }
public CacheValueWrapper <TValue> AddOrUpdate(TKey key, Func <TKey, CacheValueWrapper <TValue> > valueFactory) { using (_layer2Repository.GetSyncLock(key)) { CacheValueWrapper <TValue> value; if (!_layer2Repository.ContainsKey(key)) //double check lock { value = _layer2Repository.AddOrUpdate(key, valueFactory); } else { value = _layer2Repository.GetValue(key); } using (_layer1Repository.GetSyncLock(key)) _layer1Repository.AddOrUpdate(key, k => value); return(value); } }