自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET數(shù)據(jù)緩存之?dāng)?shù)據(jù)緩存淺談

開發(fā) 后端
ASP.NET數(shù)據(jù)緩存是什么呢?那么我們?cè)趯W(xué)習(xí)ASP.NET數(shù)據(jù)緩存的時(shí)候要注意什么呢?本文就向你介紹這方面的內(nèi)容。

ASP.NET數(shù)據(jù)緩存的學(xué)習(xí)是如何呢?如何使用ASP.NET數(shù)據(jù)緩存呢?在講ASP.NET數(shù)據(jù)緩存之前還要先說一下如果在頁面中使用參數(shù)緩存。前面講過一個(gè)緩存設(shè)置VaryByParam="none"為無參數(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)說,就是設(shè)置成我們?cè)陧撁嬷惺褂玫腝ueryString名稱,看個(gè)例子:

  1. ﹤%...@ Page Language="C#" AutoEventWireup="true" CodeFile="date.aspx.cs" Inherits="date" %﹥  
  2. ﹤%...@ OutputCache Duration="60" VaryByParam="CustomerID" %﹥  
  3. ﹤!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"﹥  
  4.  
  5. ﹤html xmlns="http://www.w3.org/1999/xhtml" ﹥  
  6. ﹤head runat="server"﹥  
  7.     ﹤title﹥ASP.NET數(shù)據(jù)緩存﹤/title﹥  
  8. ﹤/head﹥  
  9. ﹤body﹥  
  10.     ﹤form id="form1" runat="server"﹥  
  11.     ﹤div﹥  
  12.           ﹤asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow" 
  13.             BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None"﹥  
  14.             ﹤FooterStyle BackColor="Tan" /﹥  
  15.             ﹤SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" /﹥  
  16.             ﹤PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" /﹥  
  17.             ﹤HeaderStyle BackColor="Tan" Font-Bold="True" /﹥  
  18.             ﹤AlternatingRowStyle BackColor="PaleGoldenrod" /﹥  
  19.         ﹤/asp:GridView﹥  
  20.          ﹤br /﹥  
  21.         ﹤br /﹥  
  22.         ﹤asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/date.aspx?CustomerID=16"﹥16﹤/asp:HyperLink﹥  
  23.         ﹤asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/date.aspx?CustomerID=19"﹥19﹤/asp:HyperLink﹥  
  24.         ﹤/div﹥  
  25.     ﹤/form﹥  
  26. ﹤/body﹥  
  27. ﹤/html﹥protected void Page_Load(object sender, EventArgs e)  
  28.     ...{  
  29.         string conn, comm, id;  
  30.         if (Request.QueryString["CustomerID"] == null)  
  31.         ...{  
  32.             id = "16";  
  33.         }  
  34.         else 
  35.         ...{   
  36.             id = Request.QueryString["CustomerID"];  
  37.         }  
  38.           
  39.         conn = "Server=WEB\SQLEXPRESS;Uid=moon;Pwd=1qaz2wsx;Database=store";  
  40.         comm = "SELECT * FROM orders WHERE CustomerID =" + id;  
  41.  
  42.         SqlDataAdapter da = new SqlDataAdapter(comm, conn);  
  43.         DataSet ds = new DataSet();  
  44.         da.Fill(ds);  
  45.  
  46.         GridView1.DataSource = ds.Tables[0];  
  47.         GridView1.DataBind();  
  48.           
  49.         Response.Write(DateTime.Now.ToString());  
  50.     } 

運(yùn)行后分別點(diǎn)擊16和19會(huì)根據(jù)這兩個(gè)關(guān)鍵字SELECT出不同的數(shù)據(jù),這時(shí)候根據(jù)我們傳遞的兩個(gè)參數(shù)會(huì)分別建立兩個(gè)緩存頁,在每點(diǎn)擊一個(gè)關(guān)鍵字后請(qǐng)記住顯示的時(shí)間,再反復(fù)刷新看看時(shí)間有什么變化!好了接下來講一下數(shù)據(jù)緩存。

ASP.NET數(shù)據(jù)緩存(Data Caching)

在System.Web.Caching空間里有一個(gè)類“Cache”我們可以通過這個(gè)類對(duì)數(shù)據(jù)進(jìn)行緩存。

最簡(jiǎn)單的緩存方法:Cache["MyCacheString"] = "My CSDN BLOG!!!"; 通過賦值的形式建立一個(gè)緩存,再通過賦值的形式取出緩存:myLabel.Text = Cache["MyCacheString"].ToString();這種方法使用非常的簡(jiǎn)單可是功能上受到了一些限制,為了更完善的訂制緩存,應(yīng)該使用Cache.Insert()方法,下面舉個(gè)例子:

頁面里只需要放一下GridView就可以了

  1. using System;  
  2. using System.Web.Caching;  
  3. using System.Data;  
  4. using System.Data.SqlClient;  
  5. using System.Configuration;  
  6. using System.Collections;  
  7. using System.Web;  
  8. using System.Web.Security;  
  9. using System.Web.UI;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Web.UI.HtmlControls;  
  13.  
  14. public partial class DataCache : System.Web.UI.Page  
  15. ...{  
  16.     DataView dv;//先聲明一個(gè)數(shù)據(jù)視圖用來存放數(shù)據(jù)庫里的數(shù)據(jù)表  
  17.  
  18.     protected void Page_Load(object sender, EventArgs e)  
  19.     ...{  
  20.         dv = (DataView)Cache["ds"];//從ASP.NET數(shù)據(jù)緩存中讀取數(shù)據(jù)表  
  21.  
  22.         if (dv == null)//如果緩存是空的,就建立數(shù)據(jù)庫連接,從數(shù)據(jù)庫里讀數(shù)據(jù)  
  23.         ...{  
  24.             string conn, comm;  
  25.             conn = "Server=WEB\SQLEXPRESS;Uid=moon;Pwd=1qaz2wsx;Database=store";  
  26.             comm = "SELECT * FROM orders";  
  27.  
  28.             SqlDataAdapter da = new SqlDataAdapter(comm, conn);  
  29.             DataSet ds = new DataSet();  
  30.             da.Fill(ds);  
  31.             dv = ds.Tables[0].DefaultView;  
  32.             //下面這句是關(guān)鍵,具體參數(shù)后面介紹  
  33.             Cache.Insert("ds", dv, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(3));  
  34.             Databind();  
  35.             Label1.Text = DateTime.Now.ToString();//參考用的時(shí)間,可有可無  
  36.         }  
  37.         else 
  38.         ...{  
  39.             Databind();  
  40.             Response.Write("Is Cache Data!!!");//此句可有可無  
  41.         }  
  42.     }    
  43.       
  44.     protected void Databind()//自定義的數(shù)據(jù)綁定方法  
  45.     ...{  
  46.         GridView1.DataSource = dv;  
  47.         GridView1.DataBind();  
  48.     }  

ASP.NET數(shù)據(jù)緩存參數(shù)說明

Cache.Insert (String, Object, CacheDependency, DateTime, TimeSpan) 1是緩存的名稱,2是緩存的數(shù)據(jù)對(duì)象,3是緩存鍵依賴項(xiàng),通常為Null,4是過期時(shí)間,如果使用相對(duì)過期時(shí)間則設(shè)為NoAbsoluteExpiration,5是可調(diào)過期時(shí)間,如果參數(shù)4使用了固定過期時(shí)間,則此參數(shù)要設(shè)成NoSlidingExpiration。呵呵是不是看的有點(diǎn)暈啊,舉兩個(gè)具體例子說一下過期時(shí)間的問題

Cache.Insert("ds", dv, null,DateTime.Now.AddMinutes(5) , System.Web.Caching.Cache.NoSlidingExpiration);
在這個(gè)例子里當(dāng)緩存建立后過5分鐘就過期。
Cache.Insert("ds", dv, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(5));

這個(gè)例子里緩存建立后,過期時(shí)間為可調(diào),比如1:20秒建立的緩存過期時(shí)間應(yīng)該是6:20但如果在3:20有人訪問了緩存,則過期時(shí)間將調(diào)整為8:20,以此類推……

我們?cè)赩S2005里建立一個(gè)測(cè)試看看使用緩存前和使用緩存后的性能變化吧!看到?jīng)]有,沒有緩存前用了0.43秒而使用緩存后只用了0.08秒性能相差5倍多?。。?!

ASP.NET數(shù)據(jù)緩存的相關(guān)內(nèi)容就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)ASP.NET數(shù)據(jù)緩存有所幫助。

責(zé)任編輯:仲衡 來源: CSDN博客
相關(guān)推薦

2009-08-03 18:35:51

ASP.NET數(shù)據(jù)緩存

2009-07-31 09:57:47

ASP.NET數(shù)據(jù)庫緩

2009-08-17 17:36:57

ASP.NET緩存數(shù)據(jù)

2009-07-31 10:23:44

緩存頁面ASP.NET緩存

2009-07-29 15:38:01

2011-10-19 09:41:15

ASP.NET性能優(yōu)化

2009-07-20 17:12:17

ASP.NET訪問數(shù)據(jù)

2009-07-29 10:35:51

ASP.NET緩存

2009-08-04 15:22:33

ASP.NET緩存機(jī)制

2009-07-29 14:35:34

頁面輸出緩存ASP.NET

2009-07-31 10:33:54

ASP.NET頁面輸出

2011-10-17 09:54:18

ASP.NET性能

2009-08-17 17:19:00

ASP.NET緩存數(shù)據(jù)

2009-07-23 13:47:46

ASP.NET數(shù)據(jù)緩存

2009-07-22 17:21:27

ASP.NET 2.0

2009-08-19 10:54:42

ASP.NET數(shù)據(jù)訪問

2009-05-11 13:48:00

ASP.NET 2.0緩存效率

2009-08-17 16:59:36

ASP.NET緩存機(jī)制

2009-07-29 15:34:13

2009-11-09 08:53:21

ASP.NET緩存
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)