目录

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. 安卓开发:DateUtils

    public class DateUtils{ /** * 显示友好时间 * * @param ms * @return */ public static String getTimeDes(long ...

  2. Ubuntu-14.04.3下SDL2测试

    最近突然蛋痛想入门Linux,想写个跨平台的游戏,各种坑,备忘,稍微记点笔记. 主要参考这个文章 buntu14.04下C++开发SDL2应用 下载与安装 到http://www.libsdl.org ...

  3. Sql Server插入数据并返回自增ID,@@IDENTITY,SCOPE_IDENTITY和IDENT_CURRENT的区别

    预备知识:SQLServer的IDENTITY关键字IDENTITY关键字代表的是一个函数,而不是identity属性.在access里边没有这个函数,所以在access不能用这个语句.语法:iden ...

  4. JSF中使用jquery拦截ajax请求

    jsf提供一个内置的jsf.ajax.request方法给我们使用,如果在jquery中使用,则需要做一些更改.  此处因为使用jquery,所以可以不必在控件中添加onclick方法了,可以给控件配 ...

  5. jboss hello world

    http://developers.redhat.com/products/devstudio/get-started/ 1. 下载 Red Hat JBoss Developer studio 2. ...

  6. 关于java8 interface的default方法

    转自鸟窝 博主写的挺详细,不了解的看一看啊 以前经常谈论的Java对比c++的一个优势是Java中没有多继承的问题. 因为Java中子类只能继承(extends)单个父类, 尽管可以实现(implem ...

  7. css居中解决方案

    水平居中 行内或者具有行内元素性质的元素(比如文字或者链接)? 让一个父元素为块级元素的行内元素水平居中,可以:CSS: 1 2 3 .center-children { text-align: ce ...

  8. [XAF] How to define a business class at runtime or allow end-users to configure its members via the application UI?

    How to define a business class at runtime or allow end-users to configure its members via the applic ...

  9. 初步了解yield_python

    yield 关键字是在学习python生成器(Generator)时遇到的,对于它及Generator至今我还不能很深入的理解,当前只是把所理解的知识作下记录,以便以后翻查. yield关键字是用来定 ...

  10. CocoaPods的版本升级

    我们在项目开发过程中为了更好的管理项目中引用的一些第三方的开源代码,我们在项目开发中都会使用CocoaPods,在项目中不使用Cocoapods可以绕过这篇帖子,但是Cocopods升级比较快,但是怎 ...