之前遇到一个问题 cache过几秒自动删除 回调函数也看了

DateTime now = DateTime.Now;
            string cachedString = (string)HttpContext.Cache["Now"];
            if (string.IsNullOrEmpty(cachedString))
            {
                cachedString = now.ToString();
                HttpContext.Cache.Insert("Now", now.ToString(), null, DateTime.Now.AddMinutes(1), Cache.NoSlidingExpiration,
                      System.Web.Caching.CacheItemPriority.NotRemovable,
                     new System.Web.Caching.CacheItemRemovedCallback(OnMoveCacheBack)//移除时调用的回调函数
                );
            }

   private void OnMoveCacheBack(string key, object value, CacheItemRemovedReason reason)
        {
            DateTime now = DateTime.Now;
            string cachedString = (string)HttpContext.Cache[key];
            if (!string.IsNullOrEmpty(cachedString))
            {
                HttpContext.Cache.Remove(key);
            }
            HttpContext.Cache.Insert("Now", now.ToString(), null, DateTime.Now.AddMinutes(1), Cache.NoSlidingExpiration,
                   System.Web.Caching.CacheItemPriority.NotRemovable,
                  new System.Web.Caching.CacheItemRemovedCallback(OnMoveCacheBack)//移除时调用的回调函数
             );
        }
回调函数

相关文章: