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/
腾讯云双十一活动已于今天正式开启了,多重优惠享不停,首购服务器低至0.4折,比如1C2G5M轻量应用服务器仅48元/年起,2C4G8M也仅70元/年起;个人及企业用户还可以一键领取3500-7000元满减券,用于支付新购、续费、升级等各项账单;企业用户还可以以首年1年的价格注册.COM域名。活动页面:https://cloud.tencent.com/act/double11我们分享的信息仍然以秒...
Webhosting24宣布自7月1日起开始对日本机房的VPS进行NVMe和流量大升级,几乎是翻倍了硬盘和流量,价格依旧不变。目前来看,日本VPS国内过去走的是NTT直连,服务器托管机房应该是CDN77*(也就是datapacket.com),加上高性能平台(AMD Ryzen 9 3900X+NVMe),还是有相当大的性价比的。此外在6月30日,又新增了洛杉矶机房,CPU为AMD Ryzen 9...
便宜的香港vps多少钱?现在国外VPS主机的价格已经很便宜了,美国VPS主机最低一个月只要十几元,但同样免备案的香港VPS价格贵不贵呢?或者说便宜的香港VPS多少钱?香港vps主机价格要比美国机房的贵一些,但比国内的又便宜不少,所以目前情况是同等配置下,美国VPS比香港的便宜,香港VPS比国内(指大陆地区)的便宜。目前,最便宜香港vps低至3元/首月、18元/月起,今天云服务器网(www.yunt...