ASP.NET 2.0緩存

MSDN上緩存概述:
http://msdn2.microsoft.com/zh-cn/library/726btaeh(VS.80).aspx  


1、頁輸出緩存

1.設置 ASP.NET 頁緩存的兩種方式


1.1 以聲明方式設置 ASP.NET 頁的緩存

以聲明方式設置 ASP.NET 頁的緩存的方法是在頁中使用 @ OutputCache 指令,它的經常使用屬性以下:

 html

 程序代碼:
<%@ OutputCache Duration="" VaryByParam="" VaryByControl="" VaryByHeader="" VaryByCustom="" CacheProfile="" Location="" %>


Duration:設置緩存到期時間,單位:秒。
VaryByParam:可用來使緩存輸出因查詢字符串而異,多個查詢字符用分號隔開。
VaryByControl:可用來使緩存輸出因控制值而異。
VaryByHeader:可用來使緩存輸出因請求的 HTTP 標頭而異。
VaryByCustom:可用來使緩存輸出因瀏覽器類型或您定義的自定義字符串而異。
CacheProfile:結合配置文件使用。
Location:設置頁的可緩存性,值有Any,Client,Downstream,None,Server,ServerAndClient。

注:在使用 @ OutputCache 指令時,必須包括一個 VaryByParam 屬性,不然將出現分析器錯誤。若是不但願使用 VaryByParam 屬性提供的功能,請將它的值設置爲「None」。

@ OutputCache 指令使用示例

①使用參數對頁的各個版本進行緩存:

 web

 程序代碼:
<%@ OutputCache Duration="60" VaryByParam="City" %>


注:若是要根據多個參數改變輸出緩存,請包括以分號 (;) 做爲分隔符的參數名稱的列表;若是要根據全部的參數值來改變緩存,請將VaryByParam 屬性設置爲星號 (*);若是不要根據參數值來改變緩存,請將 VaryByParam 屬性設置爲"None"。

②使用 HTTP 標頭對某頁的各個版本進行緩存:

 編程

 程序代碼:
<%@ OutputCache Duration="60" VaryByParam="None" VaryByHeader="Accept-Language" %>


注:若是要根據多個標頭改變緩存的內容,請以分號 (;) 做爲分隔符包括標頭名稱的列表;若是要根據全部標頭值改變緩存的內容,請將VaryByHeader 屬性設置爲星號 (*)。

③使用請求瀏覽器緩存頁的各個版本:

 瀏覽器

 程序代碼:
<%@ OutputCache Duration="10" VaryByParam="None" VaryByCustom="browser" %>


④使用自定義字符串對頁的各個版本進行緩存:

 緩存

 程序代碼:
<%@ OutputCache Duration="10" VaryByParam="None" VaryByCustom="minorversion" %>


注:還要在應用程序的 Global.asax 文件中,重寫 GetVaryByCustomString 方法以指定自定義字符串的輸出緩存行爲。post

參考:http://msdn2.microsoft.com/zh-cn/library/5ecf4420(VS.80).aspx

⑤結合配置文件:
將如下 XML 添加爲 system.web 元素的子項:

 spa

 程序代碼:
<!-- caching section group -->
<caching>
<outputCacheSettings>
    <outputCacheProfiles>
        <add name="AppCache1" enabled="true" duration="60"/>
    </outputCacheProfiles>
</outputCacheSettings>
</caching>


@ OutputCache 指令:

 code

 程序代碼:
<%@ OutputCache CacheProfile="AppCache1" VaryByParam="None" %>


使用這種方法咱們能夠從單個配置文件更改緩存行爲,而無需編輯各個頁面的 @ OutputCache 指令,而且還能夠根據須要創建不一樣的緩存規則,再應用到各組單獨頁面中。

1.2 以編程方式設置 ASP.NET 頁的緩存

以編程方式設置 ASP.NET 頁的緩存的方法是在頁的代碼中,調用 Response 對象的 Cache 屬性:

 server

程序代碼:
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60)); //設置緩存過時時間
Response.Cache.SetCacheability(HttpCacheability.Public); //設置頁的可緩存性
Response.Cache.SetValidUntilExpires(true); //緩存忽略 Cache-Control 無效標頭


使用示例

①使用參數對頁的各個版本進行緩存:

 htm

程序代碼:
protected void Page_Load(object sender, EventArgs e)
{
    Response.Cache.SetExpires(DateTime.Now.AddMinutes(1.0));
    Response.Cache.SetCacheability(HttpCacheability.Public);
    Response.Cache.SetValidUntilExpires(true);
    Response.Cache.VaryByParams["Zip"] = true;
}


注:若是要根據多個參數改變緩存的內容,請屢次設置 VaryByParams 屬性。

②使用 HTTP 標頭對某頁的各個版本進行緩存:

 

程序代碼:
protected void Page_Load(object sender, EventArgs e)
{
    Response.Cache.SetExpires(DateTime.Now.AddMinutes(1.0));
    Response.Cache.SetCacheability(HttpCacheability.Public);
    Response.Cache.SetValidUntilExpires(true);
    Response.Cache.VaryByHeaders["Accept-Language"] = true;
}


注:若是要根據多個標頭改變緩存的內容,須要在 VaryByHeaders 屬性中設置多個值。若是要根據全部標頭改變緩存的內容,請將VaryByHeaders["VaryByUnspecifiedParameters"] 設置爲 true。

③使用請求瀏覽器緩存頁的各個版本:

 

程序代碼:
protected void Page_Load(object sender, EventArgs e)
{
    Response.Cache.SetExpires(DateTime.Now.AddMinutes(1.0));
    Response.Cache.SetCacheability(HttpCacheability.Public);
    Response.Cache.SetValidUntilExpires(true);
    Response.Cache.SetVaryByCustom("browser");
}


④使用自定義字符串對頁的各個版本進行緩存:

 

程序代碼:
protected void Page_Load(object sender, EventArgs e)
{
    Response.Cache.SetExpires(DateTime.Now.AddMinutes(1.0));
    Response.Cache.SetCacheability(HttpCacheability.Public);
    Response.Cache.SetValidUntilExpires(true);
    Response.Cache.SetVaryByCustom("minorversion");
}


說明:要在應用程序的 Global.asax 文件中,重寫 GetVaryByCustomString 方法以指定自定義字符串的輸出緩存行爲。

2.頁輸出緩存的幾種模型

2.1 整頁緩存:當設置 ASP.NET 頁緩存的位置發生在頁面上時,便是整頁緩存。
2.2 部分頁緩存(控件緩存):當設置 ASP.NET 頁緩存的位置發生在用戶控件上時,便是控件緩存。
2.3 部分頁緩存(緩存後替換):在整個緩存的頁面上以聲明方式使用 Substitution 控件或以編程方式使用 Substitution 控件 API 或以隱式方式使用 AdRotator 控件,便是採用了緩存後替換。

緩存後替換舉例(以聲明方式使用 Substitution 控件)

aspx代碼:
 

程序代碼:
<asp:Label ID="Label1" runat="server" Text="Label" Width="276px"></asp:Label>
<br />
<asp:Substitution ID="Substitution1" runat="server" MethodName="NoCache" />


aspx.cs代碼:
 

程序代碼:
protected void Page_Load(object sender, EventArgs e)
{
    Label1.Text = DateTime.Now.ToString();
}
protected static string NoCache(HttpContext context)
{
    return DateTime.Now.ToString();
}


說明:Substitution 控件的 MethodName 屬性值爲一個方法的名稱(本例爲NoCache),對該方法的要求是它接受的參數類型必須爲HttpContext且返回值類型爲string,並且還必須爲靜態方法!

2、應用程序緩存

1.建立

方法1:Cache["CacheName"] = "CacheValue";
方法2:Cache.Insert(String key,object value,System.Web.Caching.CacheDependency dependencies,DateTime absoluteExpiration,TimeSpan slidingExpiration,System.Web.Caching.CacheItemPriority priority,System.Web.Caching.CacheItemRemovedCallback onRemoveCallback);
方法3:Cache.Add(String key,object value,System.Web.Caching.CacheDependency dependencies,DateTime absoluteExpiration,TimeSpan slidingExpiration,System.Web.Caching.CacheItemPriority priority,System.Web.Caching.CacheItemRemovedCallback onRemoveCallback);

Add方法和Insert方法的區別是Add 方法將返回您添加到緩存中的對象。另外,若是使用 Add 方法,而且緩存中已經存在與現有項同名的項,則該方法不會替換該項,而且不會引起異常。

建立示例

①經過使用 Insert 方法將項添加到緩存中:

 

程序代碼:
Cache.Insert("CacheItem2", "Cached Item 2");


②經過指定依賴項向緩存添加項:

 

 程序代碼:
string[] dependencies = { "CacheItem2" };
Cache.Insert("CacheItem3", "Cached Item 3",new System.Web.Caching.CacheDependency(null, dependencies));


③將設有過時策略的項添加到緩存中:

 

程序代碼:
Cache.Insert("CacheItem6", "Cached Item 6",null, DateTime.Now.AddMinutes(1d), System.Web.Caching.Cache.NoSlidingExpiration);


④將設有優先級設置的項添加到緩存中:

 

程序代碼:
Cache.Insert("CacheItem8", "Cached Item 8",
    null, System.Web.Caching.Cache.NoAbsoluteExpiration,
    System.Web.Caching.Cache.NoSlidingExpiration,
    System.Web.Caching.CacheItemPriority.High, null);


2.檢索

 

 程序代碼:
string cachedString;
cachedString = (string)Cache["CacheItem"];
if (cachedString == null)
{
  cachedString = "Www.Mzwu.Com";
  Cache.Insert("CacheItem", cachedString);
}


注:因爲緩存中所存儲的信息爲易失信息,即該信息可能由 ASP.NET 移除,所以建議的開發模式是首先肯定該項是否在緩存中。若是不在,則應將它從新添加到緩存中,而後檢索該項。

3.移除

 

 程序代碼:
Cache.Remove("MyData1");

 

出處:http://www.cnblogs.com/codecrazy/archive/2010/10/14/1851935.html

相關文章
相關標籤/搜索