ASP.NET缓存全解析2:页面输出缓存

 页面输出缓存是最为简单的缓存机制,该机制将整个ASP.NET页面内容保存在服务器内存中。当用户请求该页面时,系统从内存中输出相关数据,直到缓存数据过期。在这个过程中,缓存内容直接发送给用户,而不必再次经过页面处理生命周期。通常情况下,页面输出缓存对于那些包含不需要经常修改内容的,但需要大量处理才能编译完成的页面特别有用。需要读者注意的是,页面输出缓存是将页面全部内容都保存在内存中,并用于完成客户端请求。 

  在ASP.NET中页面缓存的使用方法非常的简单,只需要在aspx页的顶部加这样一句声明即可:

  
  
<% @ OutputCache Duration = " 60 " VaryByParam = " none " %>

  Duration:缓存的时间(秒),这是必选属性。如果未包含该属性,将出现分析器错误。 

  
  
<% @ Page Language = " C# " AutoEventWireup = " true " CodeBehind = " WebForm1.aspx.cs " Inherits = " CacheWebApp._16_4_3.WebForm1 " %>
<% @ OutputCache Duration = " 60 " VaryByParam = " none " %>
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > 页面缓存示例 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
< asp:Label ID ="Label1" runat ="server" Text ="Label" ></ asp:Label >
</ div >
</ form >
</ body >
</ html >
  后台代码:

  
  
protected void Page_Load( object sender, EventArgs e)
{
if ( ! IsPostBack)
{
Label1.Text
= DateTime.Now.ToString();
}
}

   如果不加<%@ OutputCache Duration="60" VaryByParam="none" %>,每次刷新页面上的时间每次都是在变。而加了缓存声明以后,每次刷新页面的时间并不变化,60秒后才变化一次,说明数据被缓存了60秒。

  VaryByParam是指页面根据使用 POST  GET 发送的名称/值对(参数)来更新缓存的内容,多个参数用分号隔开。如果不希望根据任何参数来改变缓存内容,请将值设置为 none。如果希望通过所有的参数值改变都更新缓存,请将属性设置为星号 (*) 

  例如: http://localhost:1165/16-4-3/WebForm1.aspx?p=1 
  则可以在WebForm1.aspx页面头部声明缓存:<%@ OutputCache Duration="60" VaryByParam="p" %> 

  以上代码设置页面缓存时间是60秒,并根据p参数的值来更新缓存,即p的值发生变化才更新缓存。 

  如果一直是WebForm1.aspx?p=1访问该页,则页面会缓存当前数据,当p=2时又会执行后台代码更新缓存内容。

  如果有多个参数时,如:http://localhost:1165/16-4-3/WebForm1.aspx?p=1&n=1

  可以这样声明:<%@ OutputCache Duration="60" VaryByParam="p;n" %> 

   除此之外,@OutputCache 还有一些其他的属性。@OutputCache指令中的属性参数描述如下:

  
  
<% @ OutputCache Duration = " #ofseconds "
Location
= " Any | Client | Downstream | Server | None |
ServerAndClient "
Shared = " True | False "
VaryByControl
= " controlname "
VaryByCustom
= " browser | customstring "
VaryByHeader
= " headers "
VaryByParam
= " parametername "
CacheProfile
= " cache profile name | '' "
NoStore
= " true | false "
SqlDependency
= " database/table name pair |CommandNotification "
%>

  CacheProfile用于调用Web.config配置文件中设置的缓存时间。这是可选属性,默认值为空字符("")

   例如在Web.config中加入配置:

  
  
< system.web >
< caching >
< outputCacheSettings >
< outputCacheProfiles >
< add name = " CacheTest " duration = " 50 " />
</ outputCacheProfiles >
</ outputCacheSettings >
</ caching >
</ system.web >

  页面中声明:

  
  
<% @ OutputCache CacheProfile = " CacheTest " VaryByParam = " none " %>

  注意:包含在用户控件(.ascx 文件)中的 @ OutputCache 指令不支持此属性。在页中指定此属性时,属性值必须与 outputCacheSettings 节下面的 outputCacheProfiles 元素中的一个可用项的名称匹配。如果此名称与配置文件项不匹配,将引发异常。

  如果每个页面的缓存时间相同,则不需要每个页面设置,而是通过统一一个地方控制,这样就可以更好的统一控制所有页面的缓存时间。如果想改变缓存时间,只需要改一下web.config的配置信息即可,而不用每个页面去修改。

  VaryByControl通过用户控件文件中包含的服务器控件来改变缓存(值是控件ID,多控件用分号隔开)。

  在 ASP.NET 页和用户控件上使用 @ OutputCache 指令时,需要该属性或 VaryByParam 属性。

  
  
<% @ Page Language = " C# " AutoEventWireup = " true " CodeBehind = " WebForm2.aspx.cs " Inherits = " CacheWebApp._16_4_3.WebForm2 " %>
<% @ OutputCache Duration = " 60 " VaryByParam = " none " VaryByControl = " DropDownList1 " %>
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > 根据控件页面缓存 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
<% = DateTime.Now %>
< br >
< asp:DropDownList ID ="DropDownList1" runat ="server" >
< asp:ListItem > beijing </ asp:ListItem >
< asp:ListItem > shanghai </ asp:ListItem >
< asp:ListItem > guangzhou </ asp:ListItem >
</ asp:DropDownList >
< asp:Button ID ="Button1" runat ="server" Text ="提交" />
</ div >
</ form >
</ body >
</ html >

  以上代码设置缓存有效期是60秒,并且页面不随任何GETPOST参数改变(即使不使用VaryByParam属性,但是仍然需要在@ OutputControl指令中显式声明该属性)。如果用户控件中包含ID属性为“DropDownList1”的服务器控件(例如下拉框控件),那么缓存将根据该控件的变化来更新页面数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_千鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值