HTTP:结合过期和验证缓存 [英] HTTP: Combining expiration and validation caching

查看:98
本文介绍了HTTP:结合过期和验证缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下情况下制定HTTP缓存标头时遇到问题。

I'm having trouble formulating HTTP cache headers for the following situation.

我们的服务器有大量数据,每周可能会改变几次。我希望浏览器缓存这些数据。此外,我希望最小化条件获取的延迟,因为网络不可靠。

Our server has large data that changes perhaps a couple times a week. I want browsers to cache this data. Additionally, I want to minimize latency from conditional gets as the network is unreliable.

我所追求的最终行为是:

The end behavior I'm after is this:


  1. 客户请求以前从未见过的资源。

  2. 服务器响应资源以及ETag和 max -age (24小时)。

  3. 直到24小时过去,客户端才会使用缓存资源。

  4. 之后到期日期,客户端将执行验证请求( If-None-Match:[etag]

  5. 如果资源未更改:

    • 服务器响应 304未修改

    • 客户端以某种方式通知现有资源从现在起24小时后有新的到期日期

    • 返回第3步

  1. Client requests a resource it hasn't seen before.
  2. Server responds with resource along with ETag and max-age (24 hours).
  3. Until 24 hours has passed, client will use cached resource.
  4. After the expiration date, client will perform a validate request (If-None-Match: [etag])
  5. If resource has not changed:
    • server responds with 304 Not Modified
    • client is somehow informed that the existing resource has a new expiration date 24 hours from now
    • return to step 3

归结为它的本能... 304响应是否包含新的 max-age ?或者原来的 max-age 是否符合后续请求?

Boiled down to its essense... can a 304 response contain a new max-age? Or is the original max-age honored for subsequent requests?

推荐答案

是的,304响应可以包含新的 max-age (或ETag,或其他响应头)。

Yes, a 304 response can contain a new max-age (or ETag, or other response headers for that matter).

我使用Firefox 4进行了一项实验,以测试原始max-age或新版本是否得到尊重,答案是新的 max-age 得到尊重,所以你应该能够实现你想要做的事情。

I did an experiment using Firefox 4 to test whether the original max-age or the new one is honored, and the answer was that the new max-age is honored, so you should be able to implement what you want to do.

重要的是要记住 max-age 相对于日期响应标头,而不是 Last-Modified ,因此每当您的服务器设置 max-age 24小时指令,它说从现在开始24小时。因此,假设这是您想要的,您根本不必更改 max-age ,只需返回86400。

It's important to remember that max-age is relative to the Date response header, not Last-Modified, so whenever your server sets a max-age directive of 24 hours, it is saying "24 hours from right now." So, assuming that's what you want, you won't have to change your max-age at all, just always return 86400.

无论如何,这里是我的实验的概述和转储。基本上,我点击设置ETag的测试URL并将 max-age 设置为120秒。因此,服务器返回带有这些响应头的页面:

Anyway, here's an overview and dump of my experiment. Basically, I hit a test URL that set an ETag and set max-age to 120 seconds. Accordingly, the server returned the page with these response headers:

HTTP/1.1 200 OK
Date: Tue, 14 Jun 2011 23:48:51 GMT
Cache-Control: max-age=120
Etag: "901ea3d0ac9303ae4855a09676f96701"
Last-Modified: Mon, 13 Jun 2011 22:20:03 GMT

然后我在地址栏中重复点击enter来加载页面(但不强制重新加载)。没有网络流量,因为Firefox反复从缓存重新加载页面。然后,在120秒结束后,下次我点击输入时,Firefox会向服务器发送一个条件GET,正如您所期望的那样。来自服务器的请求和响应是:

I then repeated hitting "enter" in the address bar to load the page (but not force a hard reload). There was no network traffic, since Firefox repeatedly reloaded the page from cache. Then, after 120 seconds were over, the very next time I hit enter, Firefox instead sent a conditional GET to the server, as you would expect. The request and response from the server were:

GET /example HTTP/1.1
If-Modified-Since: Mon, 13 Jun 2011 22:20:03 GMT
If-None-Match: "901ea3d0ac9303ae4855a09676f96701"

HTTP/1.1 304 Not Modified
Date: Tue, 14 Jun 2011 23:50:54 GMT
Etag: "901ea3d0ac9303ae4855a09676f96701"
Cache-Control: max-age=240

请注意,在304响应中,我已将服务器更改 max-age 从120秒更改为240.

Note that in the 304 response, I've had the server change max-age from 120 seconds to 240.

所以,最重要的问题是,120秒后会发生什么? Firefox会尊重新的 max-age 并继续从缓存加载页面,还是会点击服务器?答案是它继续从缓存加载页面,直到达到240秒后才重新请求:

So, the big question is, what would happen after 120 seconds? Would Firefox respect the new max-age and continue loading the page from cache, or would it hit the server? The answer is that it continued loading the page from cache, and did not re-request until after 240 seconds were reached:

GET /example HTTP/1.1
If-Modified-Since: Mon, 13 Jun 2011 22:20:03 GMT
If-None-Match: "901ea3d0ac9303ae4855a09676f96701"

HTTP/1.1 304 Not Modified
Date: Tue, 14 Jun 2011 23:54:56 GMT
Etag: "901ea3d0ac9303ae4855a09676f96701"
Cache-Control: max-age=240

我重复了另外240秒的循环,事情按照你的预期运作。所以,希望能为你解答这个问题。

I repeated through another 240-second cycle and things worked as you'd expect. So, hopefully that answers the question for you.

RFC 解释了应该如何实现年龄计算,以及其他Cache-Control参数如何工作。不能保证每个浏览器和代理都遵循这些规则,但此时HTTP 1.1已经很老了,你可以期待它们中的大多数都能像Firefox一样。

The RFC explains how age computations are supposed to be implemented, and how the other Cache-Control parameters work. There's no guarantee that every browser and proxy will follow the rules, but at this point HTTP 1.1 is pretty old and you'd expect most of them will do as Firefox does.

注意:为了简化这些示例转储,我删除了无关的标题,例如host,connection / keep-alive,content encoding / length / type,user-agent等。)

(Note: For brevity in these example dumps, I've deleted irrelevant headers such as host, connection/keep-alive, content encoding/length/type, user-agent etc.)

这篇关于HTTP:结合过期和验证缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆