public void Set <T>(string key, T value, CacheOpts opts = null) where T : class { using (MiniProfiler.Current.Step("RedisCache_Set_" + key)) { EnsureCacheOpts(ref opts); TryOnClient(client => { if (opts.IsSlidingExpireIn) { client.Add(key, value, GetDefaultSlidingExpirationIfEmpty(opts.ExpireIn)); } else { if (opts.ExpireAt.HasValue) { client.Add(key, value, GetDefaultExpirationDateIfEmpty(opts.ExpireAt)); } else { client.Add(key, value); } } }); } }
public void Set(string key, object value, CacheOpts opts = null) { if (opts == null) { opts = DefaultOpts; } if (value == null) { value = DBNull.Value; } HandleBadDataCacheBug(() => { if (opts.ExpireAt.HasValue) { _dataCache.Add(key, value, opts.ExpireAt.Value - DateTime.Now); } else if (opts.ExpireIn.HasValue) { _dataCache.Add(key, value, opts.ExpireIn.Value); } else { _dataCache.Add(key, value); } }); }
private void EnsureCacheOpts(ref CacheOpts opts) { if (opts == null) { opts = new CacheOpts(); } }
public void Set <T>(string key, T value, CacheOpts opts = null) where T : class { using (MiniProfiler.Current.Step("DictionaryCache_Set_" + key)) { var serializedValue = _serializer.Serialize(value); _dictionary.AddOrUpdate(key, serializedValue, (ky, val) => serializedValue); } }
protected void InitDataCache() { var currentLastVersion = _lastCacheVersion; lock (_lockObject) { if (_lastCacheVersion == currentLastVersion) { _dataCache = GetDataCache(EnvironmentUtils.GetConfigSettingStr("AzureCache_CacheName")); _lastCacheVersion = Guid.NewGuid(); DefaultOpts = new CacheOpts(); } } }
public void Set(string key, object value, CacheOpts opts = null) { if (opts == null) { opts = DefaultOpts; } //ASP.NET Cache doesn't allow nulls so use DBNull instead if (value == null) { value = DBNull.Value; } if (opts.ExpireAt.HasValue) { MemoryCache.Default.Add(key, value, opts.ExpireAt.Value); } else if (opts.ExpireIn.HasValue) { if (opts.IsSlidingExpireIn) { MemoryCache.Default.Add(key, value, new CacheItemPolicy { SlidingExpiration = opts.ExpireIn.Value }); } else { DateTimeOffset expireAt = DateTimeOffset.UtcNow.Add(opts.ExpireIn.Value); MemoryCache.Default.Add(key, value, new CacheItemPolicy { AbsoluteExpiration = expireAt }); } } else { MemoryCache.Default.Add(key, value, null); } }
public static T GetOrSet <T>(this ICache cache, string key, Func <T> setter, TimeSpan?slidingExpiration = null, TimeSpan?absoluteExpiration = null) where T : class { object cached = null; if (cache.TryGet(key, ref cached)) { if (cached is T) { return((T)cached); } } var value = setter(); var cacheOpts = new CacheOpts() { ExpireIn = slidingExpiration ?? absoluteExpiration, IsSlidingExpireIn = slidingExpiration.HasValue }; cache.Set(key, value, cacheOpts); return(value); }
//static MsCache() //{ // _instance = new MsCache(); //} public MsCache() { DefaultOpts = new CacheOpts(); }
//TryMerge shouldn't be used for Dictionary Cache public bool TryMerge<T>(string key, T instance, CacheOpts opts = null) { throw new NotImplementedException(); }
public void Set(string key, object value, CacheOpts opts = null) { _cacheData.AddOrUpdate(key, value, (ky, val) => value); }