OutputCache屬性詳解(三)— VaryByHeader,VaryByCustom


目錄

 

VaryByHeader :分號分隔的 HTTP 標頭列表,用於使輸出緩存發生變化。將該特性設為多標頭時,對於每個指定標頭組合,輸出緩存都包含一個不同版本的請求文檔。 

注意:設置 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數據時一致的,瀏覽器數據結果也能保持一致:

 

VaryByContentEncodings:以分號分隔的字符串列表,用於更改輸出緩存。將 VaryByContentEncodings 屬性用於 Accept-Encoding 標頭,可確定不同內容編碼獲得緩存響應的方式。

測試使用谷歌,IE,火狐三種瀏覽器,這三種瀏覽器的Accept-Encoding如下:
谷歌:Accept-Encoding:gzip,deflate,sdch

IE:Accept-Encoding gzip, 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"/>

則三個瀏覽器的緩存都將會失效。

 

VaryByCustom表示自定義輸出緩存要求的任意文本。

如果賦予該屬性的值為 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

 

VaryByControl 獲取或設置一組分號分隔的控件標識符,這些標識符包含在當前頁或用戶控件內,用於改變當前緩存項。指當前頁緩存依賴與制定控件的值,如下代碼所示:

<%@ 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 VaryByContentEncodingsVaryByCustom、VaryByControl 寫到這,如有問題歡迎指正。

 

作者:釋迦苦僧   出處:http://www.cnblogs.com/woxpp/p/3980851.html 本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM