Esempio n. 1
0
        private bool Add(string key, object item, Action <object> expireCallback, DateTimeOffset?absoluteExpire, TimeSpan?slidingExpire)
        {
            if (string.IsNullOrEmpty(key) || item == null)
            {
                return(false);
            }

            var options = new CacheItemOptions();

            if (absoluteExpire.HasValue)
            {
                options.AbsoluteExpiration = absoluteExpire;
                if (absoluteExpire >= DateTimeOffset.UtcNow)
                {
                    return(false);
                }
            }

            if (slidingExpire.HasValue)
            {
                options.SlidingExpiration = slidingExpire;
            }

            if (expireCallback != null)
            {
                options.ExpirationCallback = expireCallback;
            }

            return(Add(key, item, options));
        }
Esempio n. 2
0
 private void ProcessCacheCleanupItem(string key, CacheItem item, CacheItemOptions options, DateTimeOffset currentTimestamp)
 {
     if (IsExpired(item, options, currentTimestamp))
     {
         ExpireItem(key, item);
     }
 }
Esempio n. 3
0
        public bool Add(string key, object item, CacheItemOptions options = null)
        {
            if (string.IsNullOrEmpty(key) || item == null)
            {
                return(false);
            }

            if (options == null)
            {
                options = new CacheItemOptions();
            }

            if (!options.AbsoluteExpiration.HasValue && !options.SlidingExpiration.HasValue && !options.AbsoluteExpirationRelativeToNow.HasValue)
            {
                options.SlidingExpiration = TimeSpan.FromHours(1);
            }

            var now = DateTimeOffset.UtcNow;

            if (options.AbsoluteExpirationRelativeToNow.HasValue && !options.AbsoluteExpiration.HasValue)
            {
                options.AbsoluteExpiration = now + options.AbsoluteExpirationRelativeToNow.Value;
                options.AbsoluteExpirationRelativeToNow = null;
            }

            if (_memoryCache.ContainsKey(key))
            {
                ExpireItem(key, _memoryCache[key]);
            }

            _memoryCache[key] = new CacheItem()
            {
                Options      = options,
                StoredObject = item,
                LastAccess   = now
            };

#if DEBUG
            PrintWarning($"Cached new item with key {key} with value {item}");
#endif
            return(true);
        }
Esempio n. 4
0
        private bool IsExpired(CacheItem item, CacheItemOptions options, DateTimeOffset current)
        {
            if (options.AbsoluteExpiration.HasValue)
            {
                if (options.AbsoluteExpiration <= current)
                {
                    return(true);
                }
            }

            if (options.SlidingExpiration.HasValue)
            {
                if ((current - item.LastAccess) <= options.SlidingExpiration.Value)
                {
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 5
0
        /// <summary>
        /// 保存对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public bool Set <T>(string key, T value, CacheItemOptions options)
        {
            RedisHelper.StartPipe(p => p.HMSet(key, SlidingSecsField, options.GetSlidingExpirationSecs(), ValueField, value).Expire(key, options.GetAbsluteExpirationSecs()));

            return(true);
        }