2009-01-26 32 views
4

我在包含usercontrol的頁面上使用Asp.net OutputCache,在某些情況下當編輯usercontrol時,我希望能夠過期頁面緩存並用新數據重新加載頁面。Asp.Net OutputCache和Expiration

有沒有什麼辦法可以在usercontrol內做到這一點?

如果不是,緩存頁面的其他方法是什麼,這將允許我以這種方式進行編輯。

----------- -----------編輯

一些調查研究後,我發現,似乎運作良好的方法。

Dim cachekey As String = String.Format("Calendar-{0}", calendarID) 
HttpContext.Current.Cache.Insert(cachekey, DateTime.Now, Nothing, System.DateTime.MaxValue, System.TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, Nothing) 
Response.AddCacheItemDependency(cachekey) 

,將添加一個依賴於頁面緩存對象,則到期我這樣做:

Dim cachekey as string = String.Format("Calendar-{0}", CalendarID) 
HttpContext.Current.Cache.Insert(cachekey, DateTime.Now, Nothing, System.DateTime.MaxValue, System.TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, Nothing) 

現在只要依賴cachekey是已知的頁面可能到期。

回答

0

經過一些更多的研究,我發現了一種似乎很好的方法。

Dim cachekey As String = String.Format("Calendar-{0}", calendarID) 
HttpContext.Current.Cache.Insert(cachekey, DateTime.Now, Nothing, System.DateTime.MaxValue, System.TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, Nothing) 
Response.AddCacheItemDependency(cachekey) 

,將添加一個依賴於頁面緩存對象,則到期我這樣做:

Dim cachekey as string = String.Format("Calendar-{0}", CalendarID) 
HttpContext.Current.Cache.Insert(cachekey, DateTime.Now, Nothing, System.DateTime.MaxValue, System.TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, Nothing) 

現在只要依賴cachekey是已知的頁面可能到期。

1

你可以試試這個:

private void RemoveButton_Click(object sender, System.EventArgs e) 
{ 
    HttpResponse.RemoveOutputCacheItem("/caching/CacheForever.aspx"); 
} 

來源:http://aspalliance.com/668

感謝。

+0

是的,這似乎是唯一的例子,但它沒有考慮到VaryByParams,所以如果我過期那頁,我認爲仍然會有緩存中的數據在同一頁面上使用不同的查詢字符串。 – 2009-01-26 23:42:05

1

您的解決方案對我無效。然而......經過一些測試後,我得到了這個工作正常。此代碼將位於需要緩存的UserControl Page_Load中。

string key_refresh = "refresh_id_" + YourID; 
Cache[key_refresh] = DateTime.Now.ToString(); 

CacheDependency dep = new CacheDependency(null, new string[] { key_refresh }); 
this.CachePolicy.Dependency = dep; 

出於某種原因,使用Response.AddCacheItemDependency沒有任何效果,當我從Cache[key_refresh]更新我的數據。

在我的情況下,我通過用戶ID緩存我的控件,因此每個用戶都將擁有不同版本的此控件與不同的數據,我使用VaryByCustom單獨緩存它。