Exemple #1
0
 public void AddObject(string key, object obj, TimeExpirationEnum timeEnum)
 {
     if (timeEnum == TimeExpirationEnum.Absolute)
     {
         WebDataCache.Insert(key, obj, null, DateTime.Now.AddSeconds(_defuaultTime), Cache.NoSlidingExpiration);
     }
     else
     {
         WebDataCache.Insert(key, obj, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(_defuaultTime));
     }
 }
Exemple #2
0
        public T GetObject <T>(string key, Func <T> tdataFunc, TimeExpirationEnum timeoffEnum) where T : class
        {
            T tObj = GetObject <T>(key);

            if (tObj != null)
            {
                return(tObj);
            }
            tObj = tdataFunc();
            AddObject(key, tObj, timeoffEnum);
            return(tObj);
        }
Exemple #3
0
        public void AddObject(string key, object obj, TimeExpirationEnum timeEnum)
        {
            CacheItemPolicy policy = new CacheItemPolicy();

            if (timeEnum == TimeExpirationEnum.Absolute)
            {
                policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(TimeOut);
            }
            else
            {
                TimeSpan ts = TimeSpan.FromSeconds(TimeOut);
                policy.SlidingExpiration = ts;
            }
            MCache.Add(key, obj, policy);
        }
Exemple #4
0
        public void AddObject(string key, object obj, TimeExpirationEnum timeEnum, Action <string> doAction)
        {
            CacheItemRemovedCallback removedCallback =
                delegate(string keys, object cacheItem, CacheItemRemovedReason removedReason)
            {
                if (removedReason == CacheItemRemovedReason.Expired)
                {
                    doAction(key);
                }
            };

            if (timeEnum == TimeExpirationEnum.Absolute)
            {
                WebDataCache.Insert(key, obj, null, DateTime.Now.AddSeconds(_defuaultTime), Cache.NoSlidingExpiration, CacheItemPriority.Normal, removedCallback);
            }
            else
            {
                WebDataCache.Insert(key, obj, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(_defuaultTime), CacheItemPriority.Normal, removedCallback);
            }
        }
Exemple #5
0
        /// <summary>
        /// 添加一个过期回调的 方法
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <param name="timeEnum"></param>
        /// <param name="doAction"></param>
        public void AddObject(string key, object obj, TimeExpirationEnum timeEnum, Action <string> doAction)
        {
            CacheItemPolicy policy = new CacheItemPolicy();

            if (timeEnum == TimeExpirationEnum.Absolute)
            {
                policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(TimeOut);
            }
            else
            {
                TimeSpan ts = TimeSpan.FromSeconds(TimeOut);
                policy.SlidingExpiration = ts;
            }
            CacheEntryRemovedCallback removedCallback = delegate(CacheEntryRemovedArguments cera)
            {
                if (cera.RemovedReason == CacheEntryRemovedReason.Expired)
                {
                    doAction(key);
                }
            };

            policy.RemovedCallback = removedCallback;
            MCache.Add(key, obj, policy);
        }