Esempio n. 1
0
        bool _validateAge <T>(XCacheItem <T> cacheItem)
            where T : class, new()
        {
            if (cacheItem.MaxAge == null)
            {
                return(true);
            }

            var ts = DateTime.UtcNow.Subtract(cacheItem.DateStamp);

            return(ts < cacheItem.MaxAge);
        }
Esempio n. 2
0
        void _updateItem <T>(T cacheItem, XCacheItem <T> cacheWrapper)
            where T : class, new()
        {
            var asCache = cacheItem as ICacheInfo;

            if (asCache == null)
            {
                return;
            }

            asCache.CacheId        = cacheWrapper.Id;
            asCache.CacheDateStamp = cacheWrapper.DateStamp;
        }
Esempio n. 3
0
        public async Task <XCacheItem <T> > Set <T>(string key, T item, TimeSpan?maxAge) where T : class, new()
        {
            var cacheItem = new XCacheItem <T>();

            cacheItem.DateStamp = DateTime.UtcNow;

            cacheItem.Item   = item;
            cacheItem.Key    = key;
            cacheItem.MaxAge = maxAge;

            if (maxAge != null && maxAge.Value > TimeSpan.FromDays(7))
            {
                maxAge = TimeSpan.FromDays(7);
            }

            var result = await _redisCache.SetEntity(key, cacheItem, maxAge);

            return(cacheItem);
        }
Esempio n. 4
0
        public async Task <XCacheItem <T> > Set <T>(string key, T item, TimeSpan?maxAge) where T : class, new()
        {
            var t = typeof(T);

            Dictionary <string, object> dict = null;

            if (_memoryCache != null)
            {
                if (!_memoryCache.ContainsKey(t))
                {
                    _memoryCache.Add(t, new Dictionary <string, object>());
                }
                dict = _memoryCache[t];
            }
            else
            {
                dict = new Dictionary <string, object>();
            }

            XCacheItem <T> cacheItem = null;

            if (dict.ContainsKey(key))
            {
                cacheItem = dict[key] as XCacheItem <T>;
            }

            if (cacheItem == null)
            {
                cacheItem = new XCacheItem <T>();
                dict[key] = cacheItem;
            }

            cacheItem.DateStamp = DateTime.UtcNow;
            cacheItem.MaxAge    = maxAge;

            cacheItem.Item = item;
            cacheItem.Key  = key;

            return(cacheItem);
        }