目錄
OutputCache概念學習
OutputCache屬性詳解(一)
OutputCache屬性詳解(二)
OutputCache屬性詳解(三)
OutputCache屬性詳解(四)— SqlDependency
注意:設置 VaryByHeader 特性將啟用在所有 HTTP 1.1 版緩存中緩存項,而不僅僅在 asp.net 緩存中進行緩存。用戶控件中的 @ OutputCache 指令不支持此特性。
準備測試代碼配置文件和頁面如下:
<system.web> <caching> <outputCacheSettings> <outputCachePRofiles> <!--name 緩存配置名稱 duration 緩存的時間(以秒計) 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訪問這個頁面,我們可以看到火狐和IE下的HTTP請求頭中的User-Agent不一致,如下:
則兩個瀏覽器訪問的結果也不一致,如下
我們修改參數,采用HttpHeader中的Host參數,如下:
<add name="outputCache60" duration="60" enabled="true" varyByParam="none" location="Any" varyByHeader="Host"/>
這兩個瀏覽器的請求的HttpHeader中Host數據時一致的,瀏覽器數據結果也能保持一致:
測試使用谷歌,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"/>
在三個瀏覽器中輸入測試地址,刷新我們會發現 火狐和IE數據保持一致,讀取緩存數據,而谷歌的數據則一直在變。
如果我們設置配置文件為:
<add name="outputCache60" duration="60" enabled="true" varyByParam="none" location="Any" varyByContentEncoding="sdch;gzip"/>
則三個瀏覽器的緩存都將會失效。
如果賦予該屬性的值為 browser,緩存將隨瀏覽器名稱和主要版本信息的不同而異。如果輸入自定義字符串,則必須在應用程序的 Global.asax 文件中重寫 GetVaryByCustomString 方法。
<add name="outputCache60" duration="60" enabled="true" varyByParam="none" location="Any" varyByCustom="browser"/>
測試在兩臺機器上進行,一臺機器火狐版本為32.0.1 IE8,另一臺火狐版本為32.0.1 IE9,如下圖所示,火狐的緩存數據保持一致,但IE的數據則不會一致(版本不一樣)。
如果輸入自定義字符串,則必須在應用程序的 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" 代碼去掉,那在多個客戶端同時訪問這個頁面時,多個客戶端所輸出的內容都一致,但是采用varyByCustom="UserHostName" 這種自定義緩存模式,則每個客戶端的緩存是按它們的請求的UserHostName 進行區分的,效果如下:
關于varyByCustom 推薦個論壇大家可以看下: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="測試數據1" Value="1"></asp:ListItem> <asp:ListItem Text="測試數據2" Value="2"></asp:ListItem> <asp:ListItem Text="測試數據3" Value="3"></asp:ListItem> </asp:DropDownList> </div> <a href="Default2.aspx">Default2.aspx</a> </form></body></html>
當我們切換slt_VaryByControl值時,緩存機制會根據所選的值來輸出新的頁面還是緩存頁面。
VaryByHeader 、VaryByContentEncodings、VaryByCustom、VaryByControl 寫到這,如有問題歡迎指正。
作者:釋迦苦僧 出處:http://www.companysz.com/woxpp/p/3980851.html 本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接。
新聞熱點
疑難解答