2012-08-22 27 views
1

我正在使用.net 4.0 HttpContext.Current.Cache.Add()將對象插入到我的應用程序的緩存中。在.aspx控制面板頁面中,我想顯示所有緩存對象及其插入時指定的各自到期日期。怎麼做?C#從HttpContext.Current.Cache中檢索緩存過期日期

+0

請參閱http://stackoverflow.com/questions/344479/how-can-i-get-the-expiry-datetime-of-an-httpruntime-cache-object – drch

回答

2

如果我正確地理解了你,你想要顯示已插入的靜態截止日期,對不對?如果是這樣,所有你需要的是存儲你的到期日期,並將其傳遞到控制面板。如果您使用的是asp.net mvc,則可以將此日期作爲ViewModel的屬性發送。如果你想「上飛」,你將不得不使用反射到期日期

public DateTime InsertItemOnCache(object item, DateTime expiration) 
{ 

    DateTime dateExpiration; 
    //Here you construct your cache key. 
    //You can use your asp.net sessionID if you want to your cache 
    //to be for a single user. 
    var key = string.Format("{0}--{1}", "Test", "NewKey"); 

    if (expiration != null) 
    { 
     dateExpiration = expiration; 
    } 
    else 
    { 
     //Set your default time 
     dateExpiration = DateTime.Now.AddHours(4); 
    } 
    //I recommend using Insert over Add, since add will return null if there are 
    //2 objects with the same key 
    HttpContext.Current.Cache.Insert(key, item, null, dateExpiration, Cache.NoSlidingExpiration); 

    return dateExpiration; 
} 

但是:我們給你什麼,我談論的例子。爲此,請參閱建議作爲對您問題的評論的帖子。