缓存一个伴随ASP.NET从1.0到4.0的OutputCache Bug介绍

outputcache  时间:2021-04-18  阅读:()

http://taskcn.blog. 163.com/

一个伴随ASP.NET从1.0到4.0的OutputCache Bug介绍我们先来一睹这个Bug的风采

在一个.aspx文件中增加OutputCache设置代码如下

复制代码代码如下:

%@ OutputCache Duration="300" VaryByParam="*"%

上面的设置表示缓存5分钟根据不同的查询字符串更新缓存。 Location使用的是默认值Any也就是可以在浏览器、代理服务器、 Web服务器三个地方进行缓存在Response Headers中的体现就是Cache-Control:public, max-age=300。 如果你要用CDN加速 Cache-Control就要用public 。

然后我们在Firefox浏览器中访问这个页面并打开Firebug见下图

第一次访问返回状态码为"200 OK"正常。这里Response Headers中的Vary:Accept-Encoding是因为IIS启用“动态内容压缩”产生的如果不启用就不会出现。

这时缓存应该被建立起来了我们按F5刷新一下浏览器看一下结果见下图http://taskcn.blog. 163.com/

http://taskcn.blog. 163.com/

第二次访问返回状态码为"304 Not Modified"浏览器缓存生效这也是我们期望的。

但是请注意一下上图中的Vary:*它会让浏览器的缓存失效我们再按一下F5验证一下。

果然浏览器缓存失效返回状态码变回了200 OK。缓存时间有5分钟呢第三次就失效了这样的结果显然不是我们期望的。

上面的测试是在Web服务器上IIS启用动态内容压缩dynamic contentcompression的情况下进行的如果关闭动态内容压缩每次请求返回都是200 OK Vary都是星号。也就是说浏览器游览缓存根本没起作用。

Bug欣赏完毕我们进行第二个测试。

将OutputCache的VaryByParam属性值设置为none

复制代码代码如下:

%@ OutputCache Duration="600" VaryByParam="none"%http://taskcn.blog. 163.com/

http://taskcn.blog. 163.com/

测试结果显示浏览器第一次请求之后接下来在缓存时间内服务器的响应都是"304 Not Modified"这才是我们想要的效果。

但是在实际应用中我们使用VaryByParam="none"很少用的更多的是为VaryByParam指定对应的值。

所以这个Bug影响很大增加了服务器负担浪费了带宽。

Bug相关信息

在微软的官方文档中专门提到了这个bug——"Output Caching Changes toVary * HTTP Header":

In ASP.NET 1.0, a bug caused cached pages that specified

Location="ServerAndClient" as an output –cache setting to emit aVary:* HTTP header in the response. This had the effect of tellingclient browsers to never cache the page locally.

In ASP.NET 1. 1, the System.Web.HttpCachePolicy.SetOmitVaryStar methodwas added, which you could call to suppress the Vary:* header. Thismethod was chosen because changing the emitted HTTP header wasconsidered a potentially breaking change at the time. However,developers have been confused by the behavior in ASP.NET, and bugreports suggest that developers are unaware of the existing

SetOmitVaryStar behavior.

In ASP.NET 4, the decision was made to fix the root problem. TheVary:* HTTP header is no longer emitted from responses that specifythe following directive:

%@OutputCache Location="ServerAndClient" %

As a result, SetOmitVaryStar is no longer needed in order to suppressthe Vary:* header.http://taskcn.blog. 163.com/

http://taskcn.blog. 163.com/

In applications that specify Location="ServerAndClient" in the @OutputCache directive on a page, you will now see the behaviorimplied by the name of the Location attribute' s value – that is,pages will be cacheable in the browser without requiring that youcall the SetOmitVaryStar method.

从上面的文档中我们可以知道这个Bug的历史

在ASP.NET 1.0时如果在OutputCache中设置

Location="ServerAndClient"在ASP.NET在响应时会浏览器发送Vary:*HTTP header。

在ASP.NET 1. 1时微软针对这个Bug提供一个专门的方法

System.Web.HttpCachePolicy.SetOmitVaryStar(bool omit) 通过

SetOmitVaryStar(true)修改HTTP header去掉Vary:*。

在ASP.NET 4时微软郑重地宣布从根本上解决了这个问题。

而且文档中提到这个bug只会出现在Location="ServerAndClient"时。可是我用ASP.NET 4的实测试情况是不仅Location="ServerAndClient"时的Bug没有解决而且Location="Any"时也会出现同样的Bug。

解决方法

解决方法很简单只要用ASP.NET 1. 1时代提供的

System.Web.HttpCachePolicy.SetOmitVaryStar(bool omit)就能解决问题只需在Page_Load中添加如下代码

复制代码代码如下:protected void Page_Load(object sender, EventArgs e)

{

Response.Cache.SetOmitVaryStar(true) ;

}

相关文档

小结

小bug解决方法也很简单。但是如果你不知道这个bug又会陷入微软的一个骗局之前提到一个WCF Client的 不知不觉中浪费了服务器资源与带宽。

微软那么有钱有那么多天才程序员可是Bug也很难避免可见开发优秀的软件是多么具有挑战性的工作

补充http://taskcn.blog. 163.com/

http://taskcn.blog. 163.com/

ASP.NET MVC中不存在这个问题。http://taskcn.blog. 163.com/

Sharktech鲨鱼服务器商提供洛杉矶独立服务器促销 不限流量月99美元

Sharktech(鲨鱼服务器商)我们还是比较懂的,有提供独立服务器和高防服务器,而且性价比都还算是不错,而且我们看到有一些主机商的服务器也是走这个商家渠道分销的。这不看到鲨鱼服务器商家洛杉矶独立服务器纷纷促销,不限制流量的独立服务器起步99美元,这个还未曾有过。第一、鲨鱼机房服务器方案洛杉矶机房,默认1Gbps带宽,不限流量,自带5个IPv4,免费60Gbps / 48Mpps DDoS防御。C...

sharktech:老牌高防服务器商,跳楼价,1G独享$70、10G共享$240、10G独享$800

不知道大家是否注意到sharktech的所有服务器的带宽价格全部跳楼跳水,降幅简直不忍直视了,还没有见过这么便宜的独立服务器。根据不同的机房,价格也是不一样的。大带宽、不限流量比较适合建站、数据备份、做下载、做流媒体、做CDN等多种业务。 官方网站:https://www.sharktech.net 付款方式:比特币、信用卡、PayPal、支付宝、西联汇款 以最贵的洛杉矶机器为例,配置表如...

企鹅小屋6折年付240元起,美国CN2 GIA VPS促销,独享CPU,三网回程CN2 GIA

企鹅小屋怎么样?企鹅小屋最近针对自己的美国cn2 gia套餐推出了2个优惠码:月付7折和年付6折,独享CPU,100%性能,三网回程CN2 GIA网络,100Mbps峰值带宽,用完优惠码1G内存套餐是年付240元,线路方面三网回程CN2 GIA。如果新购IP不能正常使用,请在开通时间60分钟内工单VPS技术部门更换正常IP;特价主机不支持退款。点击进入:企鹅小屋官网地址企鹅小屋优惠码:年付6折优惠...

outputcache为你推荐
股份一卡通系统urlcss支持ipad支持ipad支持ipad支持ipadwin10关闭445端口win10怎么关闭445的最新相关信息iexplore.exe应用程序错误iexplore.exe应用程序错误127.0.0.1127.0.0.1打不开联通版iphone4s联通版iPhone4s 用联通3G卡好还是移动的好
台湾vps 台湾虚拟主机 二级域名查询 香港机房 cve-2014-6271 香港主机 42u标准机柜尺寸 ibrs 嘟牛 空间论坛 免费申请网站 华为云服务登录 厦门电信 万网空间管理 国外在线代理服务器 服务器防火墙 群英网络 卡巴斯基试用版下载 双11促销 restart 更多