使缓存对象在午夜到期

时间:2011-07-01 14:31:06

标签: asp.net caching

我有以下代码:

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

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

1 个答案:

答案 0 :(得分:2)

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

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 
{
    ...
}