ASP.NET 2.0学习3--缓存性能
内容介绍
• 页面 Caching
• 控件 Caching
• 数据 Caching
• SQL数据Cache
• Cache 配置
一、ASP.NET 2.0 Output Caching
当一个网页频繁被访问的时候,我们可以通过把整个网页缓存来提高执行效率。这样作的优点是,当用户再次访问这个网页的时候,被格式化好的HTML会被直接送显,
1.Output Cache
• Output Cache 是一项非常有效的增强访问性能的技术,由于IIS的一些特性,默认情况下Output Cache是打开的,但是要对某些请求进行缓存,还需要开发者进行定制。
定制Output Caching
• 对于Output Caching的定制,我们有两种方法,一种是基于底层的API技术,一种是基于高层的@OutputCaching
• 一旦Output Caching被定制过,那么这个网页就会在第一次被访问的时候生成cache,直到请求过期为止。
• 通过使用@Output Cache定义一个缓存页,显示当前时间,大家可以看到,当这个页面被缓存之后,刷新时,当前时间显示不发生变化。
示例讲解
• 在这个页面中<%@ Output Cache Duration=“60” VaryByParam=“none”%>
• 这段话定义了页面将要被缓存,并且,其缓存时间为60秒,并在一个页面被缓存之后,通过定义varybyparam属性,它不会因为request接受的参数而改变,只有在60秒之后,页面自动清除出缓存,此时,第一个访问提供了新的缓存。
2.参数缓存
性能优势,由参数改变缓存内容
• 有些时候我们需要根据用户的请求来生成页面,但是用户的请求只有有限的几种组合,这个时候就可以根据用户请求来生成几种缓存页面,来进行缓存。
• 接受参数示例,根据用户选择不同的state显示不同的数据库内容
接收参数示例解说
• 关键语句<%@ Output Cache Duration=“60” VaryByParam=“state”%>
• 上面显示一个数据库查询示例,并在页面下端放置一个访问的当前时间显示,可以清楚的看到,对于每个不同的参数,一分钟之内的访问会得到同一个时间戳,这表明用户在这个时间内访问的是同一个cache
3.硬盘Output Cache
• 默认情况下,Output Cache会被缓存到硬盘上。我们可以通过修改diskcacheenable的属性来设置其是否缓存,还可以通过在web config里配置缓存文件的大小
<caching>
<outputCache>
<diskCache enabled="true" maxSizePerApp="2" />
</outputCache>
</caching>
• 使用DiskOutput Cache
<%@ OutputCache Duration="3600" VaryByParam="name" DiskCacheable="true" %>
4.回调缓存
• 通过设置回调缓存机制,可以针对每个请求在页面中插入动态的部分,以弥补单独使用静态缓存的不足。
• 回调缓存示例
• 通过API实现回调缓存
<%@ OutputCache Duration="60" VaryByParam = "none" %>
<script runat="server">
Shared Function GetCurrentDate(context As HttpContext) As String
return DateTime.Now.ToString()
End Function
</script>
<html>
<head id="Head1" runat="server">
<title>Post Cache Substitution</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>This page uses post cache substitution to insert a dynamic value into a cached page.</b>
<br /><br />
Time: <%= DateTime.Now.ToString() %>
<br /><br />
<b>
Real Time: <% Response.WriteSubstitution(New HttpResponseSubstitutionCallback(GetCurrentDate)) %>
</b>
</div>
</div>
</form>
</body>
</html>
• 通过使用Substitution来实现缓存
<%@ OutputCache Duration="60" VaryByParam="none" %>
<script runat="server">
Shared Function GetCurrentDate(ByVal context As HttpContext) As String
Return Now.ToString()
End Function
</script>
<html>
<head id="Head1" runat="server">
<title>Post Cache Substitution</title>
</head>
<body>
<form id="form1" runat="server">
<h4>
This page uses post cache substitution to insert a dynamic value into a cached page.</h4>
<p>
Time:
<%= DateTime.Now.ToString() %>
</p>
<p>
<b>Real Time:
<asp:Substitution ID="Substitution1" runat="server" MethodName="GetCurrentDate" />
</b>
</p>
</form>
</body>
</html>
• 如果需要对缓存进行更详细的设置,可以通过设置System.Web.HttpCachePolicy属性来进行配置下面这个语句和我们<%@Output Cache Duration=“60” VaryByParam=“none”%>完全一样
• Response.Cache.SetExpires(Now.AddSeconds(60))
Response.Cache.SetCacheability(HttpCacheability.Public)
二、控件缓存Page Fragment Caching
• 作为Output的缓存的附加功能,还提供一种缓存技术,专门用于缓存用户控件。
• 在页面中,通过指定返回参数,来决定控件被缓存的部分。使用语句VaryByParam语句指定控件根据参数来改变。
• 通过返回参数改变缓存内容示例
接受控件传参数修改
• 缓存用户控件同样还可以使用控件作为参数来源。通过指定控件作为缓存控件的数据来源,可以达到缓存控件数据的目的
• 通过控件参数改变缓存内容示例
三、数据缓存(Cache对象)
• Asp.net提供了一种非常快捷的方法进行数据库缓存,用户可以非常方便的对页面变量进行缓存。并以此提高程序效率。
• 一个页面变量的缓存生命周期与应用程序的缓存生命周期相同。
• 同时在对后台数据进行修改的时候,还需要对cache进行相应的处理。
• 使用数据缓存示例
Cache.Insert(“Mydata”,dataset)
DataSet ds = Cache[“Mydata”]
四、数据库依赖缓存SQL Caching
• 前面的例子中,我们使用的是缓存技术一旦时间到,无论服务器端的数据是否被改变,都会释放缓存。
• 下面我们介绍的例子,通过配置数据库连接池,只有当数据库数据数据被改变的时候,缓存才会改变。
• 配置连接池示例
Cache 配置
• 可以通过在webconfig里配置不同的缓存描述,在页面中调用该描述来减少重复定义缓存描述的工作量。
<caching>
<sqlCacheDependency enabled="true" pollTime="1000" >
<databases>
<add name="PubsDB" connectionStringName="pubsConnectionString" />
</databases>
</sqlCacheDependency>
</caching>
• 缓存描述定义示例
1. 数据库配置
aspnet_regsql命令在c:/windows/Microsoft.net/fframework/…
启动缓存数据库
aspnet_regsql –s localhost –u sa –p mypassword –d pubs –dd
启动缓存表
aspnet_regsql –s localhost –u sa –p mypassword –d pubs –t titles –dt
2. 页面配置
<%@ OutputCache Duration=”10000” SqlDependency=”pubs:titles” VaryByParam=”none” %>