目录

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 、VaryByContentEncodings、VaryByCustom、VaryByControl 写到这,如有问题欢迎指正。

作者:释迦苦僧   出处:http://www.cnblogs.com/woxpp/p/3980851.html 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

OutputCache属性详解(三)— VaryByHeader,VaryByCustom的更多相关文章

  1. OutputCache属性详解(一)一Duration、VaryByParam

    目录 OutputCache概念学习 OutputCache属性详解(一) OutputCache属性详解(二) OutputCache属性详解(三) OutputCache属性详解(四)— SqlD ...

  2. OutputCache属性详解(二)一 Location

    目录 OutputCache概念学习 OutputCache属性详解(一) OutputCache属性详解(二) OutputCache属性详解(三) OutputCache属性详解(四)— SqlD ...

  3. OutputCache属性详解(四)— SqlDependency

    目录 OutputCache概念学习 OutputCache属性详解(一) OutputCache属性详解(二) OutputCache属性详解(三) OutputCache属性详解(四)— SqlD ...

  4. Intent属性详解三 data、type和extra

    1 Data  执行时要操作的数据 在目标<data/>标签中包含了以下几种子元素,他们定义了url的匹配规则: android:scheme 匹配url中的前缀,除了“http”.“ht ...

  5. tomcat 三种部署方式以及server.xml文件的几个属性详解

    一.直接将web项目文件件拷贝到webapps目录中 这是最常用的方式,Tomcat的Webapps目录是Tomcat默认的应用目录,当服务器启动时,会加载所有这个目录下的应用.如果你想要修改这个默认 ...

  6. [置顶] MVC输出缓存(OutputCache参数详解)

    1.学习之前你应该知道这些 几乎每个项目都会用到缓存,这是必然的.以前在学校时做的网站基本上的一个标准就是1.搞定增删改查2.页面做的不要太差3.能运行(ps真的有这种情况,答辩验收的时候几个人在讲台 ...

  7. MVC输出缓存(OutputCache参数详解)

    版权声明:本文为博主原创文章,未经博主允许转载随意. https://blog.csdn.net/kebi007/article/details/59199115 1.学习之前你应该知道这些 几乎每个 ...

  8. HTML video 视频标签全属性详解

    HTML 5 video 视频标签全属性详解   现在如果要在页面中使用video标签,需要考虑三种情况,支持Ogg Theora或者VP8(如果这玩意儿没出事的话)的(Opera.Mozilla.C ...

  9. Android组件---四大布局的属性详解

    [声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4372222.html Android常见布局有下面几种: LinearL ...

随机推荐

  1. 使用html2canvas实现批量生成条形码

    /*前台代码*/ <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Generat ...

  2. spring事务源码研读1

    转载摘录自:Spring事务源码分析(一)Spring事务入门 有时为了保证一些操作要么都成功,要么都失败,这就需要事务来保证. 传统的jdbc事务如下: @Test public void test ...

  3. Tabs - 标签页

    <div class="J_TWidget tab" data-widget-type="Tabs"style="width:宽度px; hei ...

  4. Java学习笔记 06 数字格式化及数学运算

    一.数字格式化 DecimalFormat类 >>DecimalFormat是NumberFormat的子类,用于格式化十进制数,可以将一些数字格式化为整数.浮点数.百分数等.通过使用该类 ...

  5. Spring 学习笔记 7. 尚硅谷_佟刚_Spring_Bean 的作用域

    1,理论 •在 Spring 中, 可以在 <bean> 元素的 scope 属性里设置 Bean 的作用域. •默认情况下, Spring 只为每个在 IOC 容器里声明的 Bean 创 ...

  6. 使用Glyph Designer创建位图字体

     使用Glyph Designer创建位图字体 转http://book.2cto.com/201210/6610.html   <iOS 5 cocos2d游戏开发实战(第2版)>将引导 ...

  7. C# 直接调用vs 委托vs动态调用vs动态类型vs反射,最佳性能测试

    懒得解释,自己看代码 测试结果: Direct call:00:00:00.0742191Delegate Direct:00:00:00.0687487Method Factory(IL):00:0 ...

  8. [Leetcode][JAVA] Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  9. C# interface

    我们学习了interface,即接口,其与抽象类有点像,但是他们也有一些区别,比如类不能多重继承但是接口却可以多重继承. 接口只包含方法.委托或事件和属性的签名(接口包含的成员).不能包含字段(因为字 ...

  10. RHEL6.5 删除桌面启动器(计算机/Home/回收站)

    首先,安装gconf-editor以获得gconftool-2命令 终端命令: gconftool-2 --set /apps/nautilus/desktop/computer_icon_visib ...