国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

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

2019-11-17 01:46:13
字體:
供稿:網(wǎng)友

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

目錄

  • OutputCache概念學(xué)習(xí)

  • OutputCache屬性詳解(一)

  • OutputCache屬性詳解(二)

  • OutputCache屬性詳解(三)

  • OutputCache屬性詳解(四)— SqlDependency

VaryByHeader :分號(hào)分隔的 HTTP 標(biāo)頭列表,用于使輸出緩存發(fā)生變化。將該特性設(shè)為多標(biāo)頭時(shí),對(duì)于每個(gè)指定標(biāo)頭組合,輸出緩存都包含一個(gè)不同版本的請(qǐng)求文檔。

注意:設(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é)果也能保持一致:

VaryByContentEncodings:以分號(hào)分隔的字符串列表,用于更改輸出緩存。將 VaryByContentEncodings 屬性用于 Accept-Encoding 標(biāo)頭,可確定不同內(nèi)容編碼獲得緩存響應(yīng)的方式。

測試使用谷歌,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ì)失效。

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

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

VaryByControl 獲取或設(shè)置一組分號(hào)分隔的控件標(biāo)識(shí)符,這些標(biāo)識(shí)符包含在當(dāng)前頁或用戶控件內(nèi),用于改變當(dāng)前緩存項(xiàng)。指當(dāng)前頁緩存依賴與制定控件的值,如下代碼所示:

<%@ 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)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接。


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 绥化市| 天水市| 冷水江市| 黑水县| 北票市| 襄垣县| 河南省| 潜江市| 重庆市| 邹城市| 井陉县| 淳化县| 巴青县| 黄山市| 昆明市| 寻甸| 滕州市| 岗巴县| 西吉县| 平和县| 北流市| 莆田市| 社旗县| 永定县| 蚌埠市| 崇礼县| 新丰县| 台州市| 新营市| 陆良县| 且末县| 郯城县| 台江县| 南京市| 泸水县| 通州市| 大庆市| 义乌市| 新河县| 定日县| 当涂县|