有时,当缓存中的某一项被移除时,您可能需要从输出缓存中移除一页。例如,您可能使用一页来显示放置在应用程序缓存中并供多个页使用的占用大量进程的报告。当该报告已更改或已从缓存中移除时,您希望将页输出也从缓存中移除,因为该报告不再有效。为此,可使缓存的页输出依赖于其他缓存项。
注意 |
|---|
| 通过调用 RemoveOutputCacheItem 方法,可显式移除输出缓存中的任何页。可以从 Global.asax 文件、自定义 ASP.NET 服务器控件或页中执行此操作,具体取决于应用程序的需要。 |
使缓存的页输出依赖于另一缓存项
-
在页中,以声明方式或编程方式指定缓存设置。有关更多信息,请参见如何:设置 ASP.NET 页缓存的过期时间值、设置页的可缓存性和缓存页的多个版本。
-
在页代码中调用 AddCacheItemDependency 方法。将创建依赖项的缓存项的名称作为 cacheKey 参数传递。
下面的代码示例演示如何在名为 ProcessIntensiveReport 的项上设置依赖项。当此项被修改或移除时,将从缓存中移除页输出。
protected void Page_Load(object sender, EventArgs e)
{
Response.AddCacheItemDependency("ProcessIntensiveReport");
// Set additional properties to enable caching.
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);
}
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.AddCacheItemDependency("ProcessIntensiveReport")
' Set additional properties to enable caching.
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60))
Response.Cache.SetCacheability(HttpCacheability.Public)
Response.Cache.SetValidUntilExpires(True)
End Sub
请参见