예제 #1
0
 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);
                 }
             }
         });
     }
 }
예제 #2
0
        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);
                }
            });
        }
예제 #3
0
 private void EnsureCacheOpts(ref CacheOpts opts)
 {
     if (opts == null)
     {
         opts = new CacheOpts();
     }
 }
예제 #4
0
 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);
     }
 }
예제 #5
0
        protected void InitDataCache()
        {
            var currentLastVersion = _lastCacheVersion;

            lock (_lockObject)
            {
                if (_lastCacheVersion == currentLastVersion)
                {
                    _dataCache        = GetDataCache(EnvironmentUtils.GetConfigSettingStr("AzureCache_CacheName"));
                    _lastCacheVersion = Guid.NewGuid();
                    DefaultOpts       = new CacheOpts();
                }
            }
        }
예제 #6
0
        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);
            }
        }
예제 #7
0
        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);
        }
예제 #8
0
        //static MsCache()
        //{
        //	_instance = new MsCache();
        //}

        public MsCache()
        {
            DefaultOpts = new CacheOpts();
        }
예제 #9
0
 //TryMerge shouldn't be used for Dictionary Cache
 public bool TryMerge<T>(string key, T instance, CacheOpts opts = null)
 {
     throw new NotImplementedException();
 }
예제 #10
0
 public void Set(string key, object value, CacheOpts opts = null)
 {
     _cacheData.AddOrUpdate(key, value, (ky, val) => value);
 }