在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秒,并且页面不随任何GET或POST参数改变(即使不使用VaryByParam属性,但是仍然需要在@ OutputControl指令中显式声明该属性)。如果用户控件中包含ID属性为“DropDownList1”的服务器控件(例如下拉框控件),那么缓存将根据该控件的变化来更新页面数据。