MSDN上緩衝概述:http://msdn2.microsoft.com/zh-cn/library/726btaeh(VS.80).aspx
一、頁輸出緩衝
1.設定 ASP.NET 頁緩衝的兩種方式
1.1 以聲明方式設定 ASP.NET 頁的緩衝
以聲明方式設定 ASP.NET 頁的緩衝的方法是在頁中使用 @ OutputCache 指令,它的常用屬性如下:
程式碼<%@ 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 指令使用樣本
①使用參數對頁的各個版本進行緩衝:
程式碼<%@ 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 方法以指定自訂字串的輸出緩衝行為。參考:http://msdn2.microsoft.com/zh-cn/library/5ecf4420(VS.80).aspx
⑤結合設定檔:
將以下 XML 添加為 system.web 元素的子項:
程式碼<!-- caching section group -->
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="AppCache1" enabled="true" duration="60"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
@ OutputCache 指令:
程式碼<%@ OutputCache CacheProfile="AppCache1" VaryByParam="None" %>
使用這種方法我們可以從單個設定檔更改緩衝行為,而無需編輯各個頁面的 @ OutputCache 指令,並且還可以根據需要建立不同的緩衝規則,再應用到各組單獨頁面中。
1.2 以編程方式設定 ASP.NET 頁的緩衝
以編程方式設定 ASP.NET 頁的緩衝的方法是在頁的代碼中,調用 Response 對象的 Cache 屬性:
程式碼Response.Cache.SetExpires(DateTime.Now.AddSeconds(60)); //設定緩衝到期時間
Response.Cache.SetCacheability(HttpCacheability.Public); //設定頁的可緩衝性
Response.Cache.SetValidUntilExpires(true); //緩衝忽略 Cache-Control 無效標題
使用樣本
①使用參數對頁的各個版本進行緩衝:
程式碼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,而且還必須為靜態方法!
二、應用程式緩衝
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");