日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳解ASP.NET緩存的工作原理

這里我們將介紹ASP.NET緩存的工作原理,包括簡單的定義,數(shù)據(jù)緩存以及緩存的設(shè)置等等內(nèi)容。希望本文能對(duì)大家今后的工作有所幫助。

#T#

介紹

緩存是在內(nèi)存存儲(chǔ)數(shù)據(jù)的一項(xiàng)技術(shù),也是ASP.NET中提供的重要特性之一。例如你可以在復(fù)雜查詢的時(shí)候緩存數(shù)據(jù),這樣后來的請(qǐng)求就不需要從數(shù)據(jù)庫中取數(shù)據(jù),而是直接從緩存中獲取。通過使用緩存可以提高應(yīng)用程序的性能。

主要有兩種類型的緩存:

1.輸出緩存Output caching

2.數(shù)據(jù)緩存Data caching

1. 輸出緩存(Output Caching)

使用輸出緩存,你可以緩存最后輸出的HTML頁面,當(dāng)相同的頁面再次請(qǐng)求的時(shí)候,ASP.NET不會(huì)再執(zhí)行頁面的生命周期和相關(guān)代碼而是直接使用緩存的頁面,語法如下:

 
 
 
  1. <%@ OutputCache Duration=”60” VaryByParam=”None”  %> 

Duration 屬性設(shè)置頁面將被緩存60妙。任何的用戶請(qǐng)求都會(huì)被緩存,在緩沖的60秒內(nèi)相同的請(qǐng)求都會(huì)直接使用緩存的頁面。當(dāng)緩存過期后ASP.NET會(huì)再次執(zhí)行頁面代碼并且為下一個(gè)60秒創(chuàng)建一個(gè)新的HTML緩存。

 
 
 
  1.  <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
  2.         CodeFile="OutputCachingTest.aspx.cs" Inherits="OutputCachingTest" Title="Untitled Page" %>
  3.  <%@ OutputCache Duration="20" VaryByParam="None" %>
  4.   ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> 
  5.     class="title">Output Cache
  •    Date:  ID="lblDate" runat="server" Text="" />
  •    Time:  ID="lblTime" runat="server" Text="" />       
  •  
  • protected void Page_Load(object sender, EventArgs e)
  • {
  •    lblDate.Text = DateTime.Now.ToShortDateString();
  •    lblTime.Text = DateTime.Now.ToLongTimeString(); 
  • 在這個(gè)例子中頁面將被緩存20秒。

    通過查詢字符串緩存(Cache by Query String )
    在實(shí)際應(yīng)用中頁面往往會(huì)根據(jù)一些參數(shù)動(dòng)態(tài)的改變頁面的內(nèi)容。如果你的頁面是通過查詢字符串來獲取信息的,你可以根據(jù)查詢字符串很容易的緩存頁面的不同拷貝。VarByParam=”None”指定ASP.NET只存儲(chǔ)緩存頁面的一個(gè)拷貝。VarByParam=”*” 指定ASP.NET根據(jù)不同的查詢字符串存儲(chǔ)不同的緩存頁面。

     
     
     
    1. <%@ OutputCache Duration="60" VaryByParam="*" %>
    2.  align="right">
    3.     href="OutputCachingTest2.aspx">No Query String | 
    4.     href="OutputCachingTest2.aspx?id=1">ID 1 | 
    5.     href="OutputCachingTest2.aspx?id=2">ID 2 | 
    6.     href="OutputCachingTest2.aspx?id=3">ID 3 |
    7.     href="OutputCachingTest2.aspx?id=3&langid=1">ID 3
     

    上面的例子中,在查詢字符串中傳了不同的ID.ASP.NET為每一個(gè)ID都存儲(chǔ)了單獨(dú)的緩存頁面。這種方式會(huì)有一些問題就是當(dāng)查詢字符串范圍很廣的時(shí)候。這個(gè)時(shí)候我們可以在VarByParam 屬性中指定重要的查詢字符串變量的名字,如下:

     
     
     
    1. <%@OutputCacheDuration="60"VaryByParam="id;langid"%> 

    這樣,ASP.NET可以根據(jù)id” or “l(fā)angid”來緩存不同的緩存版本。

    自定義緩存(Custom Caching)

    你也可以創(chuàng)建自定義的程序來緩存頁面。ASP.NET提供了一種很便捷的方式來創(chuàng)建自定義緩存,使用VarByCustom屬性指定自定義緩存類型的名字。

     
     
     
    1. %@OutputCacheDuration="20"VaryByParam="None"VaryByCustom="browser"%

    你還要?jiǎng)?chuàng)建為緩存生成自定義字符串的方法,如下:

     
     
     
    1. public override stringGetVaryByCustomString(HttpContext context, stringcustom)
    2. {
    3.     if(custom == "browser")
    4.     {
    5.        returncontext.Request.Browser.Browser +
    6.               context.Request.Browser.MajorVersion;
    7.     }
    8.     else
    9.    {
    10.        return base.GetVaryByCustomString(context, custom);
    11.     }

    這個(gè)方法必須寫在global.asax文件中。ASP.NET使用該方法返回的字符串來實(shí)現(xiàn)緩存,如果這個(gè)方法在不同的請(qǐng)求中返回相同的字符串,ASP.NET就會(huì)使用緩存的頁面,否則就會(huì)生成新的緩存版本。

    上面的例子中GetVaryByCustomString()方法根據(jù)瀏覽器的名字創(chuàng)建緩存字符串,ASP.NET會(huì)根據(jù)不同的瀏覽器請(qǐng)求創(chuàng)建不同版本的緩存。

    控件緩存(Control Cache )
    上面的緩存技術(shù)可以讓你很容易的緩存整個(gè)頁面,如果要緩存指定控件的內(nèi)容,可以通過指定VaryByControl 屬性來完成。

     
     
     
    1. <%@OutputCacheDuration="20"VaryByControl="MyControl_1"%>

    上面代碼ASP.NET將會(huì)緩存MyControl_1控件20分鐘。如果要根據(jù)一些屬性值來緩存控件只需要將OutPutCache指令加入*.ascx頁面。

     
     
     
    1. <%@Control Language="C#"AutoEventWireup="true"CodeFile="MyControl.ascx.cs"Inherits="Controls_MyControl"%>
    2. <%@OutputCacheDuration="20"VaryByControl="EmployeeID"%>
    3. ......
    4. ...... 

    VaryByControl=”EmployeeID”告訴ASP.NET根據(jù)控件中聲明的EmployeeID屬性來創(chuàng)建不同版本的緩存。

    在 .ascx.cs 文件加入EmplyeeID屬性為ASP.NET 緩存使用。

     
     
     
    1. private int_employeeID;
    2. public intEmployeeID
    3. {
    4.    get{ return_employeeID; }
    5.    set{ _employeeID = value; }
    6. }
    7. protected voidPage_Load(objectsender, EventArgs e)
    8. {
    9.    lblDate.Text = DateTime.Now.ToShortDateString();
    10.    lblTime.Text = DateTime.Now.ToLongTimeString();
    11.    lblEmployeeID.Text = EmployeeID.ToString();
    12. }

    在頁面中增加控件并且設(shè)置 EmployeeID.

     
     
     
    1. <%@RegisterSrc="Controls/MyControl.ascx"TagName="MyControl"TagPrefix="uc1"%>
    2. <asp:ContentIDasp:ContentID="Content1"ContentPlaceHolderID="ContentPlaceHolder1"runat="Server">
    3.     <divaligndivalign="center">
    4.         <uc1:MyControlIDuc1:MyControlID="MyControl1"runat="server"EmployeeID="1">
    5.     
  • 緩存配置文件(Cache Profile )
    web.config可以配置緩存相關(guān)的設(shè)置,

     
     
     
    1.   
    2.     
    3.       
    4.      <addnameaddname="ProductItemCacheProfile" duration="60"/>
    5.    
    6.    

    你可以通過設(shè)置 CacheProfile=”ProfileName” 屬性 來使用上面的配置:

     
     
     
    1. %@OutputCacheCacheProfile="ProductItemCacheProfile"VaryByParam="None"%

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

    ASP.NET還提供了另一種靈活的緩存類型:數(shù)據(jù)緩存。你可以將一些耗費(fèi)時(shí)間的條目加入到一個(gè)對(duì)象緩存集合中,以鍵值的方式存儲(chǔ)。

    Cache["Name"] = data;

    我們可以通過使用Cache.Insert()方法來設(shè)置緩存的過期,優(yōu)先級(jí),依賴項(xiàng)等。

     
     
     
    1. date1 = DateTime.Now;
    2. Cache.Insert("Date1", date1, null, DateTime.Now.AddSeconds(20), TimeSpan.Zero);

    ASP.NET允許你設(shè)置一個(gè)絕對(duì)過期時(shí)間或滑動(dòng)過期時(shí)間,但不能同時(shí)使用。

    緩存依賴項(xiàng)Cache dependency

    緩存依賴項(xiàng)使緩存依賴于其他資源,當(dāng)依賴項(xiàng)更改時(shí),緩存條目項(xiàng)將自動(dòng)從緩存中移除。緩存依賴項(xiàng)可以是應(yīng)用程序的 Cache 中的文件、目錄或與其他對(duì)象的鍵。如果文件或目錄更改,緩存就會(huì)過期。

     
     
     
    1. date2 = DateTime.Now;
    2. string[] cacheKeys = { "Date1"};
    3. CacheDependency cacheDepn = newCacheDependency(null, cacheKeys);
    4. Cache.Insert("Date2", date2, cacheDepn);

    上面的例子“Date2”緩存對(duì)象依賴“Date1”緩存條目,當(dāng) “Date1” 對(duì)象過期后“Date2” 將會(huì)自動(dòng)過期。CacheDependency(null, cacheKeys)中的第一個(gè)參數(shù)為空是由于我們只監(jiān)視緩存鍵的更改情況。

    回調(diào)函數(shù)和緩存優(yōu)先級(jí)(Callback Method and Cache Priority)

    ASP.NET允許我們寫一個(gè)回調(diào)函數(shù),當(dāng)緩存條目從緩存中移除的時(shí)候觸發(fā)。還可以設(shè)置緩存條目的優(yōu)先級(jí)。

     
     
     
    1. protected void Page_Load(object sender, EventArgs e)
    2. {
    3.   DateTime? date1 = (DateTime?)Cache["Date1"];
    4.   if (!date1.HasValue) // date1 == null
    5.   {
    6.     date1 = DateTime.Now;
    7.     Cache.Insert("Date1", date1, null, DateTime.Now.AddSeconds(20), TimeSpan.Zero, 
    8.                  CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));
    9.   }
    10.   DateTime? date2 = (DateTime?)Cache["Date2"];
    11.   if (!date2.HasValue) // date2 == null
    12.   {
    13.     date2 = DateTime.Now;
    14.     Cache.Insert("Date2", date2, null, DateTime.Now.AddSeconds(40), TimeSpan.Zero, 
    15.                  CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));
    16.   }
    17.   // Set values in labels
    18.   lblDate.Text = date1.Value.ToShortDateString();
    19.   lblTime.Text = date1.Value.ToLongTimeString();
    20.   lblDate1.Text = date2.Value.ToShortDateString();
    21.   lblTime1.Text = date2.Value.ToLongTimeString();
    22. }
    23.    
    24. private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason)
    25. {
    26.   if (key == "Date1" || key == "Date2")
    27.   { 
    28.      Cache.Remove("Date1");
    29.      Cache.Remove("Date2");
    30.   }
    31. }

    例子中創(chuàng)建了“Date1” 和 “Date2”緩存?!癉ate1” 在20秒后過期“Date2”為40秒。但是由于我們注冊(cè)了移除的回調(diào)函數(shù),當(dāng)“Date1” 或 “Date2”其中一個(gè)過期都會(huì)執(zhí)行CachedItemRemoveCallBack 方法,在這個(gè)方法中移除了兩個(gè)緩存條目,ASP.NET還提供了處理緩存條目更新時(shí)的回調(diào)函數(shù)CacheItemUpdateCallback 。

    鏈接:http://www.cnblogs.com/carysun/archive/2009/11/08/AspDotNetCache.html


    網(wǎng)頁題目:詳解ASP.NET緩存的工作原理
    網(wǎng)頁URL:http://m.5511xx.com/article/ccshcjg.html