【问题标题】:Make Cache object expire at midnight使缓存对象在午夜过期
【发布时间】:2011-07-01 22:52:46
【问题描述】:

我有以下代码:

var tempList = new BrandCollection();
            if (HttpContext.Current.Cache["cachedDeviceList"] == null)
            {
                tempList = Provider.GetDeviceData(info);
                HttpContext.Current.Cache.Insert(...);
            }
            else
            {
                tempList = 
            }

Cache.Insert() 方法被重载,我可以在其中设置依赖关系、滑动和绝对过期。我想让缓存在午夜过期。我怎么做?提前致谢!

【问题讨论】:

    标签: asp.net caching


    【解决方案1】:

    绝对到期是执行此操作的方法 - 它是“在绝对时间点到期”而不是“从现在起二十分钟内”的简写。因此,当您将项目放入缓存时,您需要计算午夜的时间,然后将其用作过期点,例如

    var tempList = new BrandCollection(); 
    if (HttpContext.Current.Cache["cachedDeviceList"] == null) 
    { 
        tempList = Provider.GetDeviceData(info); 
    
        // Find out when midnight will be by taking Today and adding a day to it
        DateTime expirationTime = DateTime.Today.AddDays(1)
        HttpContext.Current.Cache.Insert("cachedDeviceList", tempList, null, expirationTime, NoSlidingExpiration, CacheItemPriority.Normal, null); 
    } 
    else 
    {
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 2011-10-14
      • 2022-10-24
      • 2015-12-21
      • 1970-01-01
      相关资源
      最近更新 更多