之前遇到一个问题 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)//移除时调用的回调函数
             );
        }
回调函数

相关文章:

  • 2022-12-23
  • 2022-01-22
  • 2021-09-21
  • 2022-02-15
  • 2021-12-05
猜你喜欢
  • 2021-05-10
  • 2022-12-23
  • 2021-12-06
  • 2021-09-08
  • 2022-03-01
  • 2021-10-04
相关资源
相似解决方案