轉(zhuǎn)帖|其它|編輯:郝浩|2010-06-24 10:29:13.000|閱讀 564 次
概述:本文向您介紹ASP.NET數(shù)據(jù)緩存的相關(guān)內(nèi)容,希望對(duì)你了解和學(xué)習(xí)ASP.NET數(shù)據(jù)緩存有所幫助。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
ASP.NET數(shù)據(jù)緩存的學(xué)習(xí)是如何呢?如何使用ASP.NET數(shù)據(jù)緩存呢?在講ASP.NET數(shù)據(jù)緩存之前還要先說(shuō)一下如果在頁(yè)面中使用參數(shù)緩存。前面講過(guò)一個(gè)緩存設(shè)置VaryByParam="none"為無(wú)參數(shù),我們也可以對(duì)VaryByParam進(jìn)行設(shè)置,設(shè)置的參數(shù)與隨 GET 方法屬性發(fā)送的查詢字符串值對(duì)應(yīng),或與使用 POST 方法發(fā)送的參數(shù)對(duì)應(yīng)。將該屬性設(shè)置為多個(gè)參數(shù)時(shí),對(duì)于每個(gè)指定參數(shù)組合,輸出緩存都包含一個(gè)不同版本的請(qǐng)求文檔。可能的值包括 none、星號(hào) (*) 以及任何有效的查詢字符串或 POST 參數(shù)名稱。簡(jiǎn)單點(diǎn)說(shuō),就是設(shè)置成我們?cè)陧?yè)面中使用的QueryString名稱,看個(gè)例子:
﹤%@ 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" "//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"﹥ ﹤html xmlns="//www.w3.org/1999/xhtml" ﹥ ﹤head runat="server"﹥ ﹤title﹥ASP.NET數(shù)據(jù)緩存﹤/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()); } |
運(yùn)行后分別點(diǎn)擊16和19會(huì)根據(jù)這兩個(gè)關(guān)鍵字SELECT出不同的數(shù)據(jù),這時(shí)候根據(jù)我們傳遞的兩個(gè)參數(shù)會(huì)分別建立兩個(gè)緩存頁(yè),在每點(diǎn)擊一個(gè)關(guān)鍵字后請(qǐng)記住顯示的時(shí)間,再反復(fù)刷新看看時(shí)間有什么變化!好了接下來(lái)講一下數(shù)據(jù)緩存。
ASP.NET數(shù)據(jù)緩存(Data Caching)
在System.Web.Caching空間里有一個(gè)類“Cache”我們可以通過(guò)這個(gè)類對(duì)數(shù)據(jù)進(jìn)行緩存。
最簡(jiǎn)單的緩存方法:Cache["MyCacheString"] = "My CSDN BLOG!!!"; 通過(guò)賦值的形式建立一個(gè)緩存,再通過(guò)賦值的形式取出緩存:myLabel.Text = Cache["MyCacheString"].ToString();這種方法使用非常的簡(jiǎn)單可是功能上受到了一些限制,為了更完善的訂制緩存,應(yīng)該使用Cache.Insert()方法,下面舉個(gè)例子:
頁(yè)面里只需要放一下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;//先聲明一個(gè)數(shù)據(jù)視圖用來(lái)存放數(shù)據(jù)庫(kù)里的數(shù)據(jù)表 protected void Page_Load(object sender, EventArgs e) { dv = (DataView)Cache["ds"];//從ASP.NET數(shù)據(jù)緩存中讀取數(shù)據(jù)表 if (dv == null)//如果緩存是空的,就建立數(shù)據(jù)庫(kù)連接,從數(shù)據(jù)庫(kù)里讀數(shù)據(jù) { 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; //下面這句是關(guān)鍵,具體參數(shù)后面介紹 Cache.Insert("ds", dv, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(3)); Databind(); Label1.Text = DateTime.Now.ToString();//參考用的時(shí)間,可有可無(wú) } else { Databind(); Response.Write("Is Cache Data!!!");//此句可有可無(wú) } } protected void Databind()//自定義的數(shù)據(jù)綁定方法 { GridView1.DataSource = dv; GridView1.DataBind(); } } |
ASP.NET數(shù)據(jù)緩存參數(shù)說(shuō)明
Cache.Insert (String, Object, CacheDependency, DateTime, TimeSpan) 1是緩存的名稱,2是緩存的數(shù)據(jù)對(duì)象,3是緩存鍵依賴項(xiàng),通常為Null,4是過(guò)期時(shí)間,如果使用相對(duì)過(guò)期時(shí)間則設(shè)為NoAbsoluteExpiration,5是可調(diào)過(guò)期時(shí)間,如果參數(shù)4使用了固定過(guò)期時(shí)間,則此參數(shù)要設(shè)成NoSlidingExpiration。呵呵是不是看的有點(diǎn)暈啊,舉兩個(gè)具體例子說(shuō)一下過(guò)期時(shí)間的問(wèn)題
Cache.Insert("ds", dv, null,DateTime.Now.AddMinutes(5) , System.Web.Caching.Cache.NoSlidingExpiration); |
在這個(gè)例子里當(dāng)緩存建立后過(guò)5分鐘就過(guò)期。
Cache.Insert("ds", dv, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(5)); |
這個(gè)例子里緩存建立后,過(guò)期時(shí)間為可調(diào),比如1:20秒建立的緩存過(guò)期時(shí)間應(yīng)該是6:20但如果在3:20有人訪問(wèn)了緩存,則過(guò)期時(shí)間將調(diào)整為8:20,以此類推……
我們?cè)赩S2005里建立一個(gè)測(cè)試看看使用緩存前和使用緩存后的性能變化吧!看到?jīng)]有,沒(méi)有緩存前用了0.43秒而使用緩存后只用了0.08秒性能相差5倍多啊!!!
ASP.NET數(shù)據(jù)緩存的相關(guān)內(nèi)容就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)ASP.NET數(shù)據(jù)緩存有所幫助。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:網(wǎng)絡(luò)轉(zhuǎn)載