2011-07-01 57 views
1

到期我有以下代碼:使緩存對象在午夜

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

Cache.Insert()方法被重載哪裏可以設置的依賴關係,滑動和絕對過期。我想讓緩存在午夜過期。我怎麼做?預先感謝!

回答

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