前面讲了如何使用页面缓存,现在接着学习一下如何使用数据缓存,在讲数据缓存之前还要先说一下如果在页面中使用参数缓存。前面讲过一个缓存设置VaryByParam="none"为无参数,我们也可以对VaryByParam进行设置,设置的参数与随 GET 方法属性发送的查询字符串值对应,或与使用 POST 方法发送的参数对应。将该属性设置为多个参数时,对于每个指定参数组合,输出缓存都包含一个不同版本的请求文档。可能的值包括 none、星号 (*) 以及任何有效的查询字符串或 POST 参数名称。简单点说,就是设置成我们在页面中使用的QueryString名称,看个例子:
<% ... @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " date.aspx.cs " Inherits = " date " %> <% ... @ OutputCache Duration = " 60 " VaryByParam = " CustomerID " %> <! 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 > 无标题页 </ title > </ head > < body > < form id ="form1" runat ="server" > < div > < asp:GridView ID ="GridView1" runat ="server" BackColor ="LightGoldenrodYellow" BorderColor="Tan" BorderWidth ="1px" CellPadding ="2" ForeColor ="Black" GridLines ="None" > < FooterStyle BackColor ="Tan" /> < SelectedRowStyle BackColor ="DarkSlateBlue" ForeColor ="GhostWhite" /> < PagerStyle BackColor ="PaleGoldenrod" ForeColor ="DarkSlateBlue" HorizontalAlign ="Center" /> < HeaderStyle BackColor ="Tan" Font-Bold ="True" /> < AlternatingRowStyle BackColor ="PaleGoldenrod" /> </ asp:GridView > < br /> < br /> < asp:HyperLink ID ="HyperLink1" runat ="server" NavigateUrl ="~/date.aspx?CustomerID=16" > 16 </ asp:HyperLink > < asp:HyperLink ID ="HyperLink2" runat ="server" NavigateUrl ="~/date.aspx?CustomerID=19" > 19 </ asp:HyperLink > </ div > </ form > </ body > </ html >
protected void Page_Load( object sender, EventArgs e) ... { string conn, comm, id; if (Request.QueryString[ " CustomerID " ] == null ) ... { id = " 16 " ; } else ... { id = Request.QueryString[ " CustomerID " ]; } conn = " Server=WEB/SQLEXPRESS;Uid=moon;Pwd=1qaz2wsx;Database=store " ; comm = " SELECT * FROM orders WHERE CustomerID = " + id; SqlDataAdapter da = new SqlDataAdapter(comm, conn); DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds.Tables[ 0 ]; GridView1.DataBind(); Response.Write(DateTime.Now.ToString()); }
运行后分别点击16和19会根据这两个关键字SELECT出不同的数据,这时候根据我们传递的两个参数会分别建立两个缓存页,在每点击一个关键字后请记住显示的时间,再反复刷新看看时间有什么变化!好了接下来讲一下数据缓存。
数据缓存(Data Caching)
在System.Web.Caching空间里有一个类“Cache”我们可以通过这个类对数据进行缓存。
最简单的缓存方法:Cache["MyCacheString"] = "My 优快云 BLOG!!!"; 通过赋值的形式建立一个缓存,再通过赋值的形式取出缓存:myLabel.Text = Cache["MyCacheString"].ToString();这种方法使用非常的简单可是功能上受到了一些限制,为了更完善的订制缓存,应该使用Cache.Insert()方法,下面举个例子:
页面里只需要放一下GridView就可以了
using System; using System.Web.Caching; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class DataCache : System.Web.UI.Page ... { DataView dv; // 先声明一个数据视图用来存放数据库里的数据表 protected void Page_Load( object sender, EventArgs e) ... { dv = (DataView)Cache[ " ds " ]; // 从缓存中读取数据表 if (dv == null ) // 如果缓存是空的,就建立数据库连接,从数据库里读数据 ... { string conn, comm; conn = " Server=WEB/SQLEXPRESS;Uid=moon;Pwd=1qaz2wsx;Database=store " ; comm = " SELECT * FROM orders " ; SqlDataAdapter da = new SqlDataAdapter(comm, conn); DataSet ds = new DataSet(); da.Fill(ds); dv = ds.Tables[ 0 ].DefaultView; // 下面这句是关键,具体参数后面介绍 Cache.Insert( " ds " , dv, null , System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes( 3 )); Databind(); Label1.Text = DateTime.Now.ToString(); // 参考用的时间,可有可无 } else ... { Databind(); Response.Write( " Is Cache Data!!! " ); // 此句可有可无 } } protected void Databind() // 自定义的数据绑定方法 ... { GridView1.DataSource = dv; GridView1.DataBind(); } }
参数说明 Cache.Insert (String, Object, CacheDependency, DateTime, TimeSpan) 1是缓存的名称,2是缓存的数据对象,3是缓存键依赖项,通常为Null,4是过期时间,如果使用相对过期时间则设为NoAbsoluteExpiration,5是可调过期时间,如果参数4使用了固定过期时间,则此参数要设成NoSlidingExpiration。呵呵是不是看的有点晕啊,举两个具体例子说一下过期时间的问题 Cache.Insert("ds", dv, null,DateTime.Now.AddMinutes(5) , System.Web.Caching.Cache.NoSlidingExpiration); 在这个例子里当缓存建立后过5分钟就过期。 Cache.Insert("ds", dv, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(5));
这个例子里缓存建立后,过期时间为可调,比如1:20秒建立的缓存过期时间应该是6:20但如果在3:20有人访问了缓存,则过期时间将调整为8:20,以此类推……
我们在VS2005里建立一个测试看看使用缓存前和使用缓存后的性能变化吧! 看到没有,没有缓存前用了0.43秒而使用缓存后只用了0.08秒性能相差5倍多啊!!!