HTTP 缓存控制
我是 HTTP 缓存新手,确实需要一些帮助...
我看到很多人在将 XHR 发送到服务器时设置
"Cache-Control"
标头。 这样做的目的是什么?请求头也需要缓存吗?我已经为 XHR 设置了这个标头:
Cache-Control max-age=30, Must-revalidate
,但是当我在 30 秒后刷新(甚至强制刷新)时,Firefox 3.6.13 似乎不会重新验证文件。服务器也没有访问登录。这是因为我没有为 Apache 中的文件设置过期标头吗?来自 Firebug 日志:
<块引用>响应标头
日期 2011 年 1 月 22 日星期六 09:59:21 GMT
最后修改时间 2011 年 1 月 20 日星期四 09:05:11 GMT
Etag“3e000000023ca7-20d-49a436e5b3413”请求标头
保持活动 115
连接保持活动
内容类型 application/xml
Cache-Control max-age=30,必须重新验证文件已被修改,但 Firefox 甚至不执行条件获取。
响应日期
和Last-Modified
根本不更新。
I'm new to HTTP caching and really need some help...
I saw many people set the
"Cache-Control"
header when sending XHR to server.
What is the purpose of doing that? Does request header needs caching too?I've set this header for a XHR:
Cache-Control max-age=30, must-revalidate
, but Firefox 3.6.13 seems do not re-validate the file when I refresh(even force refresh) after 30 seconds. No access-log in server either. Is this because I haven't set expire header to the file in Apache?From Firebug log:
Response Headers
Date Sat, 22 Jan 2011 09:59:21 GMT
Last-Modified Thu, 20 Jan 2011 09:05:11 GMT
Etag "3e000000023ca7-20d-49a436e5b3413"Request Headers
Keep-Alive 115
Connection keep-alive
Content-Type application/xml
Cache-Control max-age=30, must-revalidateThe file have been modified, but Firefox does not even do conditional get.
Response Date
andLast-Modified
not update at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
客户端也可以发送缓存控制指令。看:
https://datatracker.ietf。 org/doc/html/draft-ietf-httpbis-p6-cache-12#section-3.2
不幸的是,浏览器在 XHR 设置时不会(还)尊重它们,但干预代理缓存通常会。
Clients can send Cache-Control directives too. See:
https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-p6-cache-12#section-3.2
Unfortunately, browsers won't (yet) honour them when set by XHR, but intervening proxy caches generally will.
服务器需要设置重新验证指令,而不是客户端。
源控制是否以及如何缓存内容,客户端决定是否遵守它。
Must-revalidate 只是意味着发送头请求,如果 etag 不同,则刷新资源。对于大文件来说,这是一种非常常见且有意义的做法,因为一旦内容发生变化,电子标签通常也会发生变化。
然而,对于小文件,您可能希望根本没有任何请求,但希望更短的超时时间。那么 Must-revalidate 是错误的。
对 7kb 图像的头请求的服务器负载和消耗的时间并不比服务完整图像少很多。
the server needs to set the re-validate directive, not the client.
the source controls if and how the content should be cacehd or not cached, the clients decides whether to honor it or not.
must-revalidate just means that a head request is sent and if the etag differes, the resources is refreshed. this is a very common an dsenseful practice for big files, because the e-tag generally changes once the content changes.
for small files however it may be your desire to not have any request at all, but a shorter time out. then must-revalidate is wrong.
the server load and the time it consumes of a head request to a 7kb image is not much less than serving the complete image.