-
在页中包含 @ OutputCache 指令,并定义 Duration 和 VaryByParam 属性。
-
在 @ OutputCache 指令中包含 Location 属性,并将其值定义为 OutputCacheLocation
枚举中的下列值之一:Any、Client、Downstream、Server、ServerAndClient 或 None。下面的代码演示如何将页的可缓存性设置为 60 秒:
<%@ OutputCache Duration="60" VaryByParam="None"%>
注意
默认设置为 Any。如果未定义 Location 属性,则可以将页输出缓存在与响应有关的所有具有缓存功能的网络设备上。其中包括请求客户端、原服务器、以及响应通过的任何代理服务器。
使用缓存配置文件以声明方式设置页的可缓存性
-
在应用程序的 Web.config 文件中定义缓存配置文件,在配置文件中包括 duration 和
varyByParam 设置。下面的 <caching> 配置元素定义名为
Cache30Seconds
的缓存配置文件,它将在服务器
上将页缓存 30 秒之久。<caching> <outputCacheSettings> <outputCacheProfiles> <add name="Cache30Seconds" duration="30" varyByParam="none" /> </outputCacheProfiles> </outputCacheSettings> </caching>
-
在使用配置文件的每个 ASP.NET 页中包含 @ OutputCache 指令,并将 CacheProfile
属性设置为 Web.config 文件中定义的缓存配置文件的名称。下面的代码指定页应当使用名为
Cache30Seconds
的缓存配置文件:<%@ OutputCache CacheProfile="Cache30Seconds" %>