12、使用页面输出缓存

使用页面输出缓存

可以给页面添加<%@ OutputCache%>指令启用页面输出缓存。

CachePageOutput.aspx
[code]
<%@ Page Language="C#" %>

<%@ OutputCache Duration="15" VaryByParam="none" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
lblTime.Text = DateTime.Now.ToString("T");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblTime" runat="server" />
</div>
</form>
</body>
</html>
[/code]
页面缓存15秒。

1、使用参数改变输出缓存
例程:
一概要面和详情页。单击概要页标题,详情面显示选中的详细信息。
通常会在概要面和详情页间传递一个查询字符串参数来指定显示在详细页中的内容。如果缓存详情页的输出内容,那么就会导致每个人都只能看到第一个选中的内容。
通过使用VaryByParam特性解决这个问题。

数据表
[code]
Movies
id int 自动增长
Title Nvarchar(50)
Director nvarchar(50)
DateReleased datetime
Description nvarchar(MAX)
[/code]

主页,master.aspx
[code]
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grdMovies" AutoGenerateColumns="False" DataSourceID="srcMovies"
ShowHeader="False" GridLines="None" runat="server">
<Columns>
<asp:HyperLinkField DataTextField="Title" DataNavigateUrlFields="id" DataNavigateUrlFormatString="~/Details.aspx?id={0}" />
</Columns>
</asp:GridView>
</div>
<asp:SqlDataSource ID="srcMovies" runat="server" ConnectionString="<%$ ConnectionStrings:Movies %>"
SelectCommand="SELECT [id], [Title] FROM [Movies]"></asp:SqlDataSource>
</form>
</body>
</html>
[/code]

从页面:Details.aspx
[code]
<%@ Page Language="C#" %>

<%@ OutputCache Duration="3600" VaryByParam="id" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=DateTime.Now.ToString("T") %>
<hr />
<asp:DetailsView ID="dtlMovie" runat="server" DataSourceID="srcMovies">
</asp:DetailsView>
</div>
<asp:SqlDataSource ID="srcMovies" runat="server" ConnectionString="<%$ ConnectionStrings:Movies %>"
SelectCommand="SELECT Movies.* FROM Movies Where Id=@id">
<SelectParameters>
<asp:QueryStringParameter Name="id" QueryStringField="Id" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
[/code]

Web.config增加
[code]
<connectionStrings>
<add name="Movies" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\S4_2.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
[/code]

结果:

单击从页

当前时间 11:47
可以看出进行了缓存。
Details.aspx中代码<%@ OutputCache Duration="3600" VaryByParam="id" %>
缓存1小时

VarByParam会导致更多的缓存而不是更少的缓存。

2011-5-17 11:47 danny

2、基于控件变化的输出缓存
VarByControl特性用于根据页面中的特定控件的值生成不同版本的页面缓存。

MasterDetails.aspx
[code]
<%@ Page Language="C#" %>

<%@ OutputCache Duration="3600" VaryByControl="dropCategories" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=DateTime.Now.ToString("T") %>
<hr />
<asp:DropDownList ID="dropCategories" runat="server" DataSourceID="srcCategories"
DataTextField="Title" DataValueField="id">
</asp:DropDownList>
<asp:Button ID="btnSelect" Text="Select" runat="server" />
<br />
<asp:GridView ID="GridView1" runat="server" m GridLines="None" DataSourceID="srcMovies"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Director" HeaderText="Director" SortExpression="Director" />
<asp:BoundField DataField="DateReleased" HeaderText="DateReleased" SortExpression="DateReleased" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
</Columns>
</asp:GridView>
<br />
</div>
<asp:SqlDataSource ID="srcCategories" runat="server" ConnectionString="<%$ ConnectionStrings:Movies %>"
SelectCommand="SELECT [id], [Title] FROM [Movies]"></asp:SqlDataSource>
<asp:SqlDataSource ID="srcMovies" runat="server" ConnectionString="<%$ ConnectionStrings:Movies %>"
SelectCommand="SELECT id, Title, Director, DateReleased, Description FROM Movies WHERE id = @id">
<SelectParameters>
<asp:ControlParameter ControlID="dropCategories" DefaultValue="Id" Name="id" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
[/code]
2011-5-17 13:05 danny

3、基于头变化的输出缓存
VaryByHeader特性,使得在特定的浏览器头的值变化时,可以创建不同的页面缓存版本。
Accept-Language
User-Agent
Cookie

varyByHeader.aspx
[code]
<%@ Page Language="C#" %>

<%@ OutputCache Duration="3600" VaryByParam="none" VaryByHeader="User-Agent" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Vary By Header</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=DateTime.Now.ToString("T") %>
<hr />
<%=Request.UserAgent %>
</div>
</form>
</body>
</html>

[/code]

不同的浏览器有不同的值,不可预知,不建议使用。
2011-5-17 13:17 danny

4、基于浏览器变化的输出缓存
VaryByCustom 值为browser  两个要考虑的:浏览器类型和它的主版本号。
IE 和FireFox不同的页面缓存
IE5、IE6不同的页面缓存

VaryByBrowser.aspx
[code]
<%@ Page Language="C#" %>

<%@ OutputCache Duration="3600" VaryByParam="none" VaryByCustom="browser" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=DateTime.Now.ToString("T") %>
<hr />
<%=Request.UserAgent %>
</div>
</form>
</body>
</html>
[/code]
其中Maxthon和IE8显示的是同一缓存,但和FireFox是不同的。
由此可见,Maxthon其实用的是IE的内核。

5、基于自定义函数变化的输出缓存
VaryByCustom可以指定一个自定义函数来决定何时生成不同的页面缓存版本。
在Global.asax文件中重写GetVaryByCustomString()方法来创建自定义函数。

VaryByCustom.aspx
[code]
<%@ Page Language="C#" %>

<%@ OutputCache Duration="3600" VaryByParam="none" VaryByCustom="css" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
if (Request.Browser.SupportsCss)
pnlCss.Visible = true;
else
pnlNotCss.Visible = true;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Vary by custom</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=DateTime.Now.ToString("T") %>
<hr />
<asp:Panel ID="pnlCss" Visible="false" runat="server">
<span style="font-weight: bold">Hello!</span>
</asp:Panel>
<asp:Panel ID="pnlNotCss" Visible="false" runat="server">
<b>Hello! not css</b>
</asp:Panel>
</div>
</form>
</body>
</html>

[/code]
其中,IE8,Maxthon和FireFox都是同一缓存的。

2011-5-17 13:33 danny

6、设置缓存位置
<%@OutputCache%>
Location特性指定页面被缓存在哪里
Any --页面被缓存在浏览器、代理服务器和Web服务器(默认值)
client --页面只缓存在浏览器
DownStream --页面被缓存在浏览器和任何代理服务器,但不缓存在服务器
none --页面不缓存
ServerAndClient --页面被缓存在浏览器和Web服务器,但不缓存在代理服务器

CacheLocation.aspx
[code]
<%@ Page Language="C#" %>

<%@ OutputCache Duration="3600" VaryByParam="none" Location="Client" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
Random rnd = new Random();
lblRandom.Text = rnd.Next(10).ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=DateTime.Now.ToString("T") %>
<hr />
Your random number is:
<asp:Label ID="lblRandom" runat="server" />
<br />
<br />
<a href="CacheLocation.aspx">Request Page</a>
</div>
</form>
</body>
</html>
[/code]

还是以前一样,Maxthon和IE是同一内核,显示同一缓存,而FireFox是不同的。

7、创建页面输出缓存文件依赖
可以在一缓存页面和硬盘上的一个文件(或一级文件)之间创建一个依赖。当文件修改时,缓存页面自动失效并重新在下次页面请求时生成。

Movies.xml
[code]
<?xml version="1.0" encoding="utf-8" ?>
<movies>
<movie title="Star Wars" />
<movie title="King Kong" />
<movie title="The Fly" />
</movies>
[/code]

OutputCacheFileDependency.aspx
[code]
<%@ Page Language="C#" %>

<%@ OutputCache Duration="9999" VaryByParam="none" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
Response.AddFileDependency(MapPath("Movies.xml"));
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=DateTime.Now.ToString("T") %>
<hr />
<asp:GridView ID="GridView1" runat="server" DataSourceID="srcMovies" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
</Columns>
</asp:GridView>
</div>
<asp:XmlDataSource ID="srcMovies" runat="server" DataFile="Movies.xml"></asp:XmlDataSource>
</form>
</body>
</html>
[/code]
2011-5-17 13:57 danny
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值