2009-01-26 88 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单独缓存它。