HTTP 缓存控制 max-age,必须重新验证
我有几个与缓存控制相关的疑问。
如果我为静态 html/js/images/css 文件指定 Cache-Control max-age=3600, Must-revalidate
,并在 HTTP 标头中定义 Last Modified 标头:
- 浏览器/代理是否缓存(例如Squid/Akamai)是否在 max-age 过期之前一直到源服务器进行验证?或者它会从缓存中提供内容直到 max-age 过期吗?
- max-age 过期(即缓存过期)后,是否存在 If-Modified-Since 检查,或者是否从源服务器重新下载内容而不进行 If-Modified-Since 检查?
I have a couple of queries related to Cache-Control.
If I specify Cache-Control max-age=3600, must-revalidate
for a static html/js/images/css file, with Last Modified Header defined in HTTP header:
- Does browser/proxy cache(like Squid/Akamai) go all the way to origin server to validate before max-age expires? Or will it serve content from cache till max-age expires?
- After max-age expiry (that is expiry from cache), is there a If-Modified-Since check or is content re-downloaded from origin server w/o If-Modified-Since check?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
a) 如果服务器包含此标头:
它告诉客户端缓存和代理缓存,一旦内容过时(超过 3600 秒),它们必须在源服务器上重新验证,然后才能提供内容。这应该是缓存系统的默认行为,但是
must-revalidate
指令使这一要求变得明确。b) 客户应重新验证。它可能会使用带有 ETag 的
If-Match
或If-None-Match
标头重新验证,也可能使用If-Modified-Since
或带有日期的If-Unmodified-Since
标头。a) If the server includes this header:
it is telling both client caches and proxy caches that once the content is stale (older than 3600 seconds) they must revalidate at the origin server before they can serve the content. This should be the default behavior of caching systems, but the
must-revalidate
directive makes this requirement unambiguous.b) The client should revalidate. It might revalidate using the
If-Match
orIf-None-Match
headers with an ETag, or it might use theIf-Modified-Since
orIf-Unmodified-Since
headers with a date.一个。查看 此页面,看看会发生什么。
b.过期后,浏览器将在服务器上检查文件是否已更新。如果没有,服务器将使用
304 Not Modified
标头进行响应,并且不会下载任何内容。您可以通过查看 Firebug 或类似工具中的“网络”面板自行检查此行为。只需在地址栏中重新输入 URL,然后将 HTTP 请求数与缓存为空时的请求数进行比较即可。
a. Look at the ‘Stats’ tab on this page and see what happens.
b. After expiration the browser will check at the server if the file is updated. If not, the server will respond with a
304 Not Modified
header and nothing is downloaded.You can check this behaviour yourself by looking at the ‘Net’ panel in Firebug or similar tools. Just re-enter the URL in the address bar and compare the number of HTTP requests with the number of requests when your cache is empty.
给出的答案是不正确的,至少对于 2019 年的网络浏览器来说是
这样。“过期后,浏览器将在服务器上检查文件是否已更新”<- 不正确
我有一个静态文件,其中包含“Cache-Control: public,must” -revalidate,max-age=864000" 并且 Chrome 和 Firefox 每次都会发出请求(并且每次都会收到 304 Not Modified)。
The given answers are incorrect, at least for web browsers in 2019.
"After expiration the browser will check at the server if the file is updated" <- not true
I have a static file served with "Cache-Control: public,must-revalidate,max-age=864000" and both Chrome and Firefox do a request every time (and get a 304 Not Modified back every time).