Chinaunix首页 | 论坛 | 博客
  • 博客访问: 239890
  • 博文数量: 19
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 435
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-15 15:40
文章分类

全部博文(19)

文章存档

2011年(1)

2010年(2)

2009年(5)

2008年(11)

我的朋友

分类: 系统运维

2008-12-10 13:42:15

 

当你建立好一个WEB服务后,通常有两个类型的需要配置:

  1. 设置网站有更新的时候html资源马上过期,以便正在浏览的用户可以很快地得到更新.
  2. 设置所有其它资源(例如图片,CSS,javascript脚本)在一定时间后过期.


这个方案涵盖Two Simple Rules for HTTP Caching文章中提到关于如何处理更新的一些思想.

现在HttpWatch 6.0支持Firefox了,我们想探讨一下Firefox在处理缓存上与IE有些什么不同.设置较长过期时间的使用方式(上面第二条)仍可以直接用于Firefox,但配置1在两者之间还是存在细微差别的.

之前的文章 中,我们把第一条划分为:

  • 某些时候动态HTML页面需要即时从服务器更新以备随时显示-甚至是使用后退按钮的时候.例如,显示银行帐号的状态或在线订单.
  • 静态HTML页面,比如联系,FAQs或者站点地图等页面,如果它们设置了Last-Modifd响应头,允许浏览器在需要的时候重新校验,就可以利用到缓存.

本文剩下部分探讨了Firefox中影响HTML页面的两个重要不同点.

1. 使用no-cache防止Firefox无效

你可以简单地设置如下的响应头预防IE任何东西:

Cache-Control: no-cache

使用了这个响应头的页面不会保存在里,IE总会重新从服务器加载;即使你使用后退按钮.下面这个例子使用HttpWatch监听一个网上商店,当我们在提交订单表单后点击后退按钮,结果如下图:

 

然而,这个响应头却不能防止Firefox的.这意味着,Firefox在正常访问的情况下,将一直使用缓存的页面,直到它发送GET请求重新检验.并且,如果是通过后退按钮访问页面,Firefox不会再次访问服务器,而是简单直接地从缓存加载.

那怎样才能关掉Firefox中的呢? 答案很简单,关不了. 因为Firefox依靠缓存中的副本为"文件->另存为","查看源代码"这样的操作服务.但是,你可以控制页面缓存到哪里及那些缓存条目可以用于显示.

 

下面响应头在Firefox中可以防止持久化的,强制页面被缓存到内存中:

Cache-Control:no-store

这个头也可以防止使用后退按钮时访问了页面,它将触发一个HTTP GET请求.

这两个响应头的值组合使用可以在IE与Firefox得到期待的结果:

Cache-Control: no-cache, no-store

如下HttpWatch响应头标签所示:

no-store and no-cache headers

2. 如果没有设置过期时间Firefox会为你设置一个

当IE遇到没有Expires头的http响应时,它就认为永远不能自动使用条目,直到它重新从服务校验.由于IE的临时文件的一个设置项"检查所在网页的较新版本"默认为"自动",所以通常都是一个会话做一次.

这就为控制静态的html内容的提供了一个合理的方式.用户新打开的IE会得到html的最新版本,而缓存的版本就在关闭IE前会一直被使用.

Firefox处理缺失Expires头的方式不同.如果影响中有Last-Modifd头它就会使用HTTP 1.1规范RFC2616中指定的一个尝试性的过期值:

(引用规范:)
并且,如果响应中有Last-Modifd时间值,尝试性的过期值不能超过这个值到现在时间间隔的一个比率,一般设置这个比率为10%.

计算方式如下:

过期时间 = 现在时间 + 0.1 * (Last-Modifd到现在的时间差)

 

例如,如果你的静态HTML文件上次修改时间是100天前,那过期时间就是10天之后.下面的示例是一个没有Expires头页面的HttpWatch标签:

Firefox自动设置了过期时间为8天后,因为这个页面大概80天没有被修改过了.

这意味着,为了保持控制好你的HTML页面,正如我们在 Two Simple Rules for HTTP Caching 文章中讨论过的,你最好为你的静态资源如HTML,图片,CSS文件等,在你的WEB服务器设置一个合适的Expires值.

结论

为了确保IE与Firefox的行为一致,你应该:

  • 总是指定一个Expires头. 一般设置-1使用html页面能即时刷新或者对其它如图片,CSS,javascript脚本资源设置一个特定的过期时间
  • 如果你要强制页面刷新,甚至是点击后台按钮的时候,那就设置 Cache-Control: no-cache, no-store

 

 

Two Important Differences between Firefox and IE Caching

When you setup a web server there are generally two types of caching that you need to configure:

  1. HTML resources are expired immediately so that any changes made to a site are quickly picked up by existing users.
  2. You set everything else (e.g. images, CSS, Javascript) to expire at some distance time in the future.

This caching scheme is covered in Two Simple Rules for HTTP Caching along with some ideas about how to manage changes.

Now that HttpWatch 6.0 supports Firefox we wanted to cover some differences in the way that it handles caching compared to Internet Explorer. The use of long expiration times (item 2 above) still directly applies to Firefox but there are some subtle differences in the configuration of item 1.

In the previous post, we broke item 1) down into:

  • Sometimes dynamic HTML pages need to be fetched from the server whenever they are displayed - even when the back button is used. For example, pages showing the state of a bank account or online order.
  • Static HTML pages, such as contact pages, FAQs or sitemaps, can make use of caching if they have a Last-Modified response header allowing the brower to revalidate them as required

The rest of this post covers two important differences in Firefox that the affect caching of HTML pages.

1. Using no-cache Doesn’t Stop Caching in Firefox

You can prevent any caching in IE by simply setting this response header:

Cache-Control: no-cache

Pages that use this header aren’t stored in the cache and IE will always reload them from the server; even if you use the Back button to reach them. Here’s an example in the HttpWatch online store where we show that an order has already been processed if you click on Submit button followed by the Back button:

 

However, this response header doesn’t prevent caching in Firefox. It just means that Firefox will never read the page from the cache during a normal visit unless it has been re-validated by sending a GET request. Also, if the page is reached using the Back button there’s no round-trip to the server and Firefox simple re-loads the page directly from the cache.

So how can caching be turned off in Firefox? The simple answer is that it cannot. Firefox relies on having a copy of every page in the cache for commands such as File->Save As and View Page Source. However, you can control where the page is cached and whether the cached entry can be used for display purposes.

The following response header in Firefox prevents persistent caching, by forcing the page into the in-memory cache:

Cache-Control: no-store

This header also prevents reuse of the cached version of the page and triggers an HTTP GET if the page is navigated to using the Back button.

These two header values can be combined to get the required effect on both IE and Firefox:

Cache-Control: no-cache, no-store

As shown here in the HttpWatch header tab:

no-store and no-cache headers

2. If You Don’t Specify an Expiration Date Firefox May Set One for You

When IE encounters an HTTP response with no Expires header it just assumes that it can never automatically reuse the cached entry without re-validating it against the server. With the default setting of ‘Check for newer versions of stored pages’ in IE set to ’Automatically’, it will normally do this just once per session.

This provides a reasonable way of controlling the caching of static HTML content. The user will get the latest version if they open a fresh copy of IE and the cached version of the page will be used until they close IE.

Firefox handles the lack of an Expires header differently. If there is a Last-Modified response header it uses a heuristic expiration value as specified in the HTTP 1.1 spec RFC2616:

Also, if the response does have a Last-Modified time, the heuristic
expiration value SHOULD be no more than some fraction of the interval
since that time. A typical setting of this fraction might be 10%.

The calculation is as follows:

Expiration Time = Now + 0.1 * (Time since Last-Modified)

For example, if the last change to your static HTML file was 100 days ago, the expiration date will be set to 10 days in the future. Here’s an example from the Cache tab in HttpWatch of a page that had no Expires header:

Firefox has automatically set an expiration date in 8 days time because the page has not changed for approximately 80 days.

This means that to retain control of your HTML pages, as we discussed in Two Simple Rules for HTTP Caching, you should set up a suitable Expires header value on your web server for your HTML content as well as other resources such as images and CSS files.

Conclusions

In order to ensure consistent caching behaviour with IE and Firefox you should:

  • Always specify an Expires header. It will normally be set to -1 for immediate expiration of HTML pages or a date well into the future for other resources such as images, CSS and Javascript
  • If you want to force a page to be reloaded, even with the Back button, then use Cache-Control: no-cache, no-store

 

 

阅读(1549) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~