Ejemplo n.º 1
0
        /// <summary>
        /// 将对象插入到缓存管理器中。
        /// </summary>
        /// <typeparam name="T">缓存对象的类型。</typeparam>
        /// <param name="cacheKey">用于引用对象的缓存键。</param>
        /// <param name="value">要插入到缓存的对象。</param>
        /// <param name="expiration">判断对象过期的对象。</param>
        /// <param name="removeCallback">当对象从缓存中移除时,使用该回调方法通知应用程序。</param>
        public T Add <T>(string cacheKey, T value, ICacheItemExpiration expiration, Fireasy.Common.Caching.CacheItemRemovedCallback removeCallback = null)
        {
            if (expiration is FileDependency)
            {
                webCache.Insert(cacheKey, value, new CacheDependency((expiration as FileDependency).FilePath));
            }
            else
            {
                var expire = TimeSpan.Zero;
                if (expiration is NeverExpired)
                {
                    expire = Cache.NoSlidingExpiration;
                }
                else if (expiration is AbsoluteTime)
                {
                    expire = (expiration as AbsoluteTime).ExpirationTime - DateTime.Now;
                }
                else if (expiration is RelativeTime)
                {
                    expire = (expiration as RelativeTime).Expiration;
                }

                return(Add(cacheKey, value, expire, removeCallback));
            }

            return(value);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 将对象插入到缓存管理器中。
        /// </summary>
        /// <typeparam name="T">缓存对象的类型。</typeparam>
        /// <param name="cacheKey">用于引用对象的缓存键。</param>
        /// <param name="value">要插入到缓存的对象。</param>
        /// <param name="expire">对象存放于缓存中的有效时间,到期后将从缓存中移除。如果此值为 null,则默认有效时间为 30 分钟。</param>
        /// <param name="removeCallback">当对象从缓存中移除时,使用该回调方法通知应用程序。</param>
        public T Add <T>(string cacheKey, T value, TimeSpan?expire = null, Fireasy.Common.Caching.CacheItemRemovedCallback removeCallback = null)
        {
            var expireDate = DateTime.Now.Add(expire != null ? (TimeSpan)expire : TimeSpan.FromMinutes(30));

            webCache.Insert(
                cacheKey,
                value,
                null,
                expireDate,
                Cache.NoSlidingExpiration,
                CacheItemPriority.Normal,
                (k, v, r) =>
            {
                if (removeCallback != null)
                {
                    removeCallback(k, v);
                }
            });
            return(value);
        }