请单击以进行评分并提供反馈
MSDN
MSDN Library
网页
缓存
 如何:使用缓存键依赖项缓存页输出
此页面仅适用于
Microsoft Visual Studio 2005/.NET Framework 2.0

同时提供下列产品的其他版本:
ASP.NET
如何:使用缓存键依赖项缓存页输出

有时,当缓存中的某一项被移除时,您可能需要从输出缓存中移除一页。例如,您可能使用一页来显示放置在应用程序缓存中并供多个页使用的占用大量进程的报告。当该报告已更改或已从缓存中移除时,您希望将页输出也从缓存中移除,因为该报告不再有效。为此,可使缓存的页输出依赖于其他缓存项。

Note注意

通过调用 RemoveOutputCacheItem 方法,可显式移除输出缓存中的任何页。可以从 Global.asax 文件、自定义 ASP.NET 服务器控件或页中执行此操作,具体取决于应用程序的需要。

使缓存的页输出依赖于另一缓存项

  1. 在页中,以声明方式或编程方式指定缓存设置。有关更多信息,请参见如何:设置 ASP.NET 页缓存的过期时间值设置页的可缓存性缓存页的多个版本

  2. 在页代码中调用 AddCacheItemDependency 方法。将创建依赖项的缓存项的名称作为 cacheKey 参数传递。

    下面的代码示例演示如何在名为 ProcessIntensiveReport 的项上设置依赖项。当此项被修改或移除时,将从缓存中移除页输出。

    C#
    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);
    }

    Visual Basic
    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
    Note注意

    不能在 ASP.NET 用户控件中调用 AddCacheItemDependency 方法。不过,在指定 @ OutputCache 指令的任何用户控件中,都可以创建描述缓存键依赖项的 CacheDependency 对象,并将其分配给 UserControl 对象的 Dependency 属性。

请参见

社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2009 Microsoft Corporation 版权所有。 保留所有权利 | 商标 | 隐私权声明
Page view tracker