目錄
OutputCache概念學(xué)習(xí)
OutputCache屬性詳解(一)
OutputCache屬性詳解(二)
OutputCache屬性詳解(三)
OutputCache屬性詳解(四)— SqlDependency
注意:設(shè)置 VaryByHeader 特性將啟用在所有 HTTP 1.1 版緩存中緩存項(xiàng),而不僅僅在 asp.net 緩存中進(jìn)行緩存。用戶控件中的 @ OutputCache 指令不支持此特性。
準(zhǔn)備測試代碼配置文件和頁面如下:
<system.web> <caching> <outputCacheSettings> <outputCachePRofiles> <!--name 緩存配置名稱 duration 緩存的時(shí)間(以秒計(jì)) enabled 指定緩存有效 --> <add name="outputCache60" duration="60" enabled="true" varyByParam="none" location="Any" varyByHeader="User-Agent"/> </outputCacheProfiles> </outputCacheSettings> </caching> <compilation debug="true"/> </system.web>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><%@ OutputCache CacheProfile="outputCache60" %><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title> <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script></head><body> <form id="form1" runat="server"> <div> <%=DateTime.Now %> <br /> <asp:Label ID="lblTime" runat="server"></asp:Label> </div> <a href="Default2.aspx" >Default2.aspx</a> </form></body></html>
打開火狐和IE訪問這個(gè)頁面,我們可以看到火狐和IE下的HTTP請(qǐng)求頭中的User-Agent不一致,如下:
則兩個(gè)瀏覽器訪問的結(jié)果也不一致,如下
我們修改參數(shù),采用HttpHeader中的Host參數(shù),如下:
<add name="outputCache60" duration="60" enabled="true" varyByParam="none" location="Any" varyByHeader="Host"/>
這兩個(gè)瀏覽器的請(qǐng)求的HttpHeader中Host數(shù)據(jù)時(shí)一致的,瀏覽器數(shù)據(jù)結(jié)果也能保持一致:
測試使用谷歌,IE,火狐三種瀏覽器,這三種瀏覽器的Accept-Encoding如下:谷歌:Accept-Encoding:gzip,deflate,sdch
IE:Accept-Encodinggzip, deflate
火狐:Accept-Encoding gzip, deflate
修改配置文件如下:
<add name="outputCache60" duration="60" enabled="true" varyByParam="none" location="Any" varyByContentEncoding="sdch"/>
在三個(gè)瀏覽器中輸入測試地址,刷新我們會(huì)發(fā)現(xiàn) 火狐和IE數(shù)據(jù)保持一致,讀取緩存數(shù)據(jù),而谷歌的數(shù)據(jù)則一直在變。
如果我們?cè)O(shè)置配置文件為:
<add name="outputCache60" duration="60" enabled="true" varyByParam="none" location="Any" varyByContentEncoding="sdch;gzip"/>
則三個(gè)瀏覽器的緩存都將會(huì)失效。
如果賦予該屬性的值為 browser,緩存將隨瀏覽器名稱和主要版本信息的不同而異。如果輸入自定義字符串,則必須在應(yīng)用程序的 Global.asax 文件中重寫 GetVaryByCustomString 方法。
<add name="outputCache60" duration="60" enabled="true" varyByParam="none" location="Any" varyByCustom="browser"/>
測試在兩臺(tái)機(jī)器上進(jìn)行,一臺(tái)機(jī)器火狐版本為32.0.1 IE8,另一臺(tái)火狐版本為32.0.1 IE9,如下圖所示,火狐的緩存數(shù)據(jù)保持一致,但I(xiàn)E的數(shù)據(jù)則不會(huì)一致(版本不一樣)。
如果輸入自定義字符串,則必須在應(yīng)用程序的 Global.asax 文件中重寫 GetVaryByCustomString 方法。如代碼所示:
<add name="outputCache60" duration="60" enabled="true" varyByParam="none" location="Any" varyByCustom="UserHostName"/>
Global.asax文件新增如下:
public override string GetVaryByCustomString(HttpContext context, string custom) { if (string.Equals(custom, "UserHostName", StringComparison.OrdinalIgnoreCase)) { return Context.Request.UserHostName; } return base.GetVaryByCustomString(context, custom); }
如果我們將 varyByCustom="UserHostName" 代碼去掉,那在多個(gè)客戶端同時(shí)訪問這個(gè)頁面時(shí),多個(gè)客戶端所輸出的內(nèi)容都一致,但是采用varyByCustom="UserHostName" 這種自定義緩存模式,則每個(gè)客戶端的緩存是按它們的請(qǐng)求的UserHostName 進(jìn)行區(qū)分的,效果如下:
關(guān)于varyByCustom 推薦個(gè)論壇大家可以看下:http://bbs.csdn.net/topics/390667018
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><%@ OutputCache Duration="60" VaryByControl="slt_VaryByControl" %><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title> <script src="Scripts/jquery-1.4.1.js" type="text/Javascript"></script></head><body> <form id="form1" runat="server"> <div> <%=DateTime.Now %> <br /> <asp:DropDownList ID="slt_VaryByControl" AutoPostBack="true" runat="server"> <asp:ListItem Text="測試數(shù)據(jù)1" Value="1"></asp:ListItem> <asp:ListItem Text="測試數(shù)據(jù)2" Value="2"></asp:ListItem> <asp:ListItem Text="測試數(shù)據(jù)3" Value="3"></asp:ListItem> </asp:DropDownList> </div> <a href="Default2.aspx">Default2.aspx</a> </form></body></html>
當(dāng)我們切換slt_VaryByControl值時(shí),緩存機(jī)制會(huì)根據(jù)所選的值來輸出新的頁面還是緩存頁面。
VaryByHeader 、VaryByContentEncodings、VaryByCustom、VaryByControl 寫到這,如有問題歡迎指正。
作者:釋迦苦僧 出處:http://www.survivalescaperooms.com/woxpp/p/3980851.html 本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注