您的位置:首页 > 编程语言 > ASP

ASP.NET缓存:缓存ASP.NET页

2012-05-27 21:05 155 查看
当Web 服务器向请求浏览器发送响应时,服务器会在响应的 HTTP 头中包含一个 Cache-Control 字段,该字段定义可以缓存该页的设备。 根据您应用程序的需要,可以分别定义哪些设备应该或不应缓存各个 ASP.NET 页。 例如,您可能希望用户登录页的可缓存性设置不同于显示产品选择的目录页的对应设置。对于登录页,出于安全方面的考虑,您可能希望只将页缓存到服务器上,而目录页可以缓存到任何设备上。

对于 ASP.NET 页,可以使用 HttpCacheability 枚举中的值设置可缓存性。该枚举具有下列值。 前三个值与 Cache-Control HTTP 头设置直接对应,后三个值为特殊值。

NoCache 指定发出请求的设备每次应从 Web 服务器获取响应。

Public 允许由客户端和共享(代理)缓存来缓存响应。

Private 指定响应只能在客户端上缓存,而不能由共享(代理服务器)缓存来缓存。

Server 指定仅在原始服务器上缓存响应。

ServerAndNoCache 应用 Server 和 NoCache 两者的设置,以指示在该服务器上缓存内容,但显式拒绝其他所有服务器缓存响应的功能。

ServerAndPrivate 指定仅在原始服务器和请求客户端上缓存响应;不允许代理服务器缓存响应。

以声明方式设置页的可缓存性

1、在页中包含 @ OutputCache 指令,并定义 Duration 和 VaryByParam 特性。
2、Duration为缓存持续时间,单位为秒。
3、VaryByParam参数必须存在,否则报错。
4、Location指定缓存的位置,默认为Any,将页输出缓存在与响应有关的所有具有缓存功能的网络设备上,包括请求客户端、原服务器、以及响应通过的任何代理服务器。OutputCacheLocation 枚举中的下列值之一:Any、Client、Downstream、Server、ServerAndClient 或 None。

<%@ OutputCache VaryByParam="None"  Duration="5" Location="ServerAndClient"  %>


使用配置文件设置页的可缓存性

1、首先在web.config配置文件<system.web>节点下配置缓存。

<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="Cache5Seconds" duration="5" varyByParam="None"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>


2、在使用配置文件的每个 ASP.NET 页中包含 @ OutputCache 指令,并将 CacheProfile 特性设置为 Web.config 文件中定义的缓存配置文件的名称。

<%@ OutputCache CacheProfile="Cache5Seconds" %>


以编程方式设置页的可缓存性

1、通过在SetCacheability方法参数传入HttpCacheability枚举值,设置缓存方式。
2、通过SetExpires方法设置缓存持续时间
3、HttpCacheability 枚举值
NoCache 指定发出请求的设备每次应从 Web 服务器获取响应。
Public 允许由客户端和共享(代理)缓存来缓存响应。
Private 指定响应只能在客户端上缓存,而不能由共享(代理服务器)缓存来缓存。
Server 指定仅在原始服务器上缓存响应。
ServerAndNoCache 应用 Server 和 NoCache 两者的设置,以指示在该服务器上缓存内容,但显式拒绝其他所有服务器缓存响应的功能。
ServerAndPrivate 指定仅在原始服务器和请求客户端上缓存响应;不允许代理服务器缓存响应。

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(15));


以编程方式检查缓存页的有效性

1、定义 HttpCacheValidateHandler 类型的事件处理程序,并包括检查缓存页响应的有效性的代码。
验证处理程序必须返回下列 HttpValidationStatus 值之一:
Invalid 指示缓存页无效,将从缓存中移除该页,并且该请求将被作为缓存未命中处理。
IgnoreThisRequest 导致将请求视为缓存未命中处理。 因此,将重新处理该页,但不会使缓存页无效。
Valid 指示缓存页有效。

public static void ValidateCacheOutput(HttpContext context, object data,
ref HttpValidationStatus status)
{
if (context.Request.QueryString["Status"] != null)
{
string pageStatus = context.Request.QueryString["Status"];

if (pageStatus == "invalid")
status = HttpValidationStatus.Invalid;
else if (pageStatus == "ignore")
status = HttpValidationStatus.IgnoreThisRequest;
else
status = HttpValidationStatus.Valid;
}
else
status = HttpValidationStatus.Valid;
}


2、从其中一个页生命周期事件(如页的 Load 事件)中调用 AddValidationCallback 方法,将您在步骤 1 中定义的事件处理程序作为第一个参数传递。

protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.AddValidationCallback(
new HttpCacheValidateHandler(ValidateCacheOutput),
null);
this.lblTime.Text = DateTime.Now.ToString();
}


缓存页输出依赖于一个文件

缓存页输出依赖某一文件,当文件发生改变,移除输出页。

protected void Page_Load(object sender, EventArgs e)
{
string fileDependencyPath = Server.MapPath("txtFile.txt");
Response.AddFileDependency(fileDependencyPath);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
}


缓存页输出依赖文件组

缓存页输出依赖多个文件,当有一个文件发生改变,移除输出页。

protected void Page_Load(object sender, EventArgs e)
{
string[] fileDependencies;
string fileDependency1 = Server.MapPath("txtFile.txt");
string fileDependency2 = Server.MapPath("xmlFile.xml");
fileDependencies = new string[] {fileDependency1,
fileDependency2};
Response.AddFileDependencies(fileDependencies);//AddFileDependencies参数类型为string数组或者ArrayList

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
}


缓存页输出依赖应用程序缓存项

缓存页输出依赖应用程序缓存项,当应用程序缓存项修改或移除,移除缓存输出页。

protected void Page_Load(object sender, EventArgs e)
{
//定义一个应用程序缓存项
Cache["Report"] = "Report1";
Cache.Remove("Report");

//缓存页依赖该应用程序缓存项
Response.AddCacheItemDependency("Report");

Response.Cache.SetExpires(DateTime.Now.AddSeconds(5));
Response.Cache.SetCacheability(HttpCacheability.Public);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: