ASP.NET數據緩存之數據緩存淺談
ASP.NET數據緩存的學習是如何呢?如何使用ASP.NET數據緩存呢?在講ASP.NET數據緩存之前還要先說一下如果在頁面中使用參數緩存。前面講過一個緩存設置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﹥ASP.NET數據緩存﹤/title﹥ ﹤/head﹥ ﹤body﹥ ﹤form id="form1" runat="server"﹥ ﹤div﹥ ﹤asp:GridView ID="GridView1" runat="server" BackColor="LightGo
ldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black"
GridLines="None"﹥ ﹤FooterStyle BackColor="Tan" /﹥ ﹤SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" /﹥ ﹤PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" Horizont
alAlign="Center" /﹥ ﹤HeaderStyle BackColor="Tan" Font-Bold="True" /﹥ ﹤AlternatingRowStyle BackColor="PaleGoldenrod" /﹥ ﹤/asp:GridView﹥ ﹤br /﹥ ﹤br /﹥ ﹤asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/date.aspx?Cu
stomerID=16"﹥16﹤/asp:HyperLink﹥ ﹤asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/date.aspx?Cu
stomerID=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出不同的數據,這時候根據我們傳遞的兩個參數會分別建立兩個緩存頁,在每點擊一個關鍵字后請記住顯示的時間,再反復刷新看看時間有什么變化!好了接下來講一下數據緩存。
ASP.NET數據緩存(Data Caching)
在System.Web.Caching空間里有一個類“Cache”我們可以通過這個類對數據進行緩存。
最簡單的緩存方法:Cache["MyCacheString"] = "My CSDN 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"];//從ASP.NET數據緩存中讀取數據表 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(); } }
ASP.NET數據緩存參數說明
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倍多啊!!!
ASP.NET數據緩存的相關內容就向你介紹到這里,希望對你了解和學習ASP.NET數據緩存有所幫助。