ASP.NET缓存通过在内存中存储页面输出来构建一个高性能的,可伸缩的asp.net web应用程序。在随后的请求中,网页代码不用执行,直接使缓存的输出用于服务的请求。在本文中,我们专注于ASP.NET页面输出缓存。
这只是在一站式开发技术框架中Silverlight样例的一部分,你能从http://cfx.codeplex.com/上获得更多的例子。
Microsoft All-In-One Code Framework ,微软通过典型的三种流行的编程语言(Visual C#,VB.NET和Visual C++)编写代码示例代码来描绘微软主要的开发技术。每个示例是精心挑选,组成的,并提供测试,或使用的编码方案。
默认情况下,我们去请求一个ASP.NET 站点,每次请求都将进行多步处理的。 例如页面的初始化,加载,渲染等等。这样会消耗大量的服务器资源。考虑以下情形:许多客户浏览ASP.NET的新闻网页和新闻网站的页面几个小时都不会改变 。基于共同的路线,多个客户的几乎在同一时间在请求同一个新闻网页,ASP.NET将执行相同的代码生成多次相同新闻。这是一种浪费资源的过程。因此,我们开始考虑能否生成一次响应,它能服务于多个客户。答案就是缓存。
Location = " Any | Client | Downstream | Server | None | ServerAndClient "
Shared = " True | False "
VaryByControl = " controlname "
VaryByCustom = " browser | customstring "
VaryByHeader = " headers "
VaryByParam = " parametername "
VaryByContentEncoding = " encodings "
CacheProfile = " cache profile name | '' "
NoStore = " true | false "
SqlDependency = " database/table name pair | CommandNotification "
%>
在本文中,我们将介绍@ OutputCache的 Duration
, VaryByCustom
, VaryByParam
, 和VaryByControl属性来缓存我们的页面输出
,对于其它属性,您可以参考:http://msdn.microsoft.com/en-us/library/hdxfb6cy.aspx.
下面演练一步一步如何使用它们。在演示中,我们使用的日期时间,以确定页面是否被缓存。(在页面加载事件中,我们将日期时间写在一个名字为“lblResult”的控件中)
1、Duration属性:
在ASPX标记中添加@OutputCache指令,并指定到期时间。在这种情况下,我们分配给它10秒。例如: OutputCache Duration="10" VaryByParam="none"。
运行ASP.NET Web应用程序,推出这个页面。在页面重新加载时候,我们将看到在网页上的日期时间10秒内不会改变。
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > OutPutCacheWithDuration </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
< asp:Label ID ="lblResult" runat ="server" ></ asp:Label >
< br />
< br />
< asp:Button ID ="btnPostBack" runat ="server" Text ="Post Back" />
< p >
The page will be cached 10s, and then you can
click Button to update datetime.
</ p >
</ div >
</ form >
</ body >
</ html >
2、VaryByControl属性
拖放一个DropDownList到页面中和给它增加3个item。
在ASPX标记中添加@ OutputCache指令,并指定到期时间和VaryByControl属性。 例如: OutputCache Duration="1000" VaryByControl="ddlOption".
运行ASP.NET Web应用程序和启动这个页面,我们可以看到,不同item有其相应的缓存。
代码
<%
@ OutputCache Duration
=
"
1000
"
VaryByControl
=
"
ddlOption
"
%>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
runat
="server"
>
<
title
>
OutPutCacheWithVaryByControl
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
div
>
<
asp:Label
ID
="lblResult"
runat
="server"
></
asp:Label
>
<
br
/>
<
br
/>
<
asp:DropDownList
ID
="ddlOption"
runat
="server"
AutoPostBack
="True"
OnSelectedIndexChanged
="ddlOption_SelectedIndexChanged"
>
<
asp:ListItem
Selected
="True"
>
Option One
</
asp:ListItem
>
<
asp:ListItem
>
Option Two
</
asp:ListItem
>
<
asp:ListItem
>
Option Three
</
asp:ListItem
>
</
asp:DropDownList
>
<
p
>
The page will be rendered from cache basing
on the selected item of DropDownList.
The different item has corresponding cache.
</
p
>
</
div
>
</
form
>
</
body
>
</
html
>
3、VaryByCustom属性
在ASPX标记中添加@ OutputCache指令,并指定到期时间和设置VaryByControl属性为“browser”。For example: OutputCache Duration="1000"VaryByCustom="browser"VaryByParam="none"。
运行ASP.NET Web应用程序和使用IE和Firefox(不同的浏览器和版本)启动这个页面,我们将看到,对于不同的浏览器,缓存是不同的。
代码
<%
@ OutputCache Duration
=
"
1000
"
VaryByCustom
=
"
browser
"
VaryByParam
=
"
none
"
%>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
runat
="server"
>
<
title
>
OutPutCacheWithVaryByCustom
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
div
>
<
asp:Label
ID
="lblResult"
runat
="server"
></
asp:Label
>
<
br
/>
<
br
/>
<
asp:Button
ID
="btnPostBack"
runat
="server"
Text
="Post Back"
/>
<
p
>
The page will be rendered from cache basing
on the version of browser, such as IE and FireFox.
</
p
>
</
div
>
</
form
>
</
body
>
</
html
>
4、VaryByParam属性
在ASPX标记中添加@ OutputCache指令,并指定到期时间和指定VaryByParam属性为“id”。例如: OutputCache Duration="1000" VaryByParam="id"。
运行ASP.NET Web应用程序和启动这个网页, 通过使用QueryString “id”。,你可以请求使用不同的值。
~/OutputCacheWithParam.aspx?id=2 >
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > OutPutCacheWithVaryByParam </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div >
< asp:Label ID ="lblResult" runat ="server" ></ asp:Label >
< p >
The page will be rendered from cache until
the value of QueryString named "id" is
changed or Duration is expiration.
</ p >
</ div >
</ form >
</ body >
</ html >
原文:http://www.codeproject.com/KB/web-cache/OutputCache.aspx
出处: http://zhuqil.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。