如果未设置过期和/或无缓存标头,谷歌浏览器会缓存资源多长时间? [英] How long does Google Chrome cache a resource if expires and/or no-cache headers are not set?

查看:125
本文介绍了如果未设置过期和/或无缓存标头,谷歌浏览器会缓存资源多长时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Chrome 在我们的 Glassfish 服务器上缓存资源时遇到问题.尽管存在 Last-Modified 标头,但不会发送 expires 和 no-cache 标头,并且 Chrome 正在缓存资源(一个大约 4 MB 的 SWF 文件).

We have been having a problem with Chrome caching a resource on our Glassfish server. The expires and no-cache headers are not being sent and the resource (an approximately 4 MB SWF file) is being cached by Chrome -- despite the presence of the Last-Modified header.

有时 Chrome 会得到一个 304 代码,而其他时候它只会得到一个 200(来自缓存).我了解 304 -- Chrome 可能会使用缓存版本检查最近的 Last-Modified 日期来决定.但其他时候它会执行 200(来自缓存),它不返回任何标题信息,并且 Chrome 只是假设文件没有被修改而不是检查.

Sometimes Chrome will get a 304 code, and other times it simply does a 200 (from cache). I understand the 304 -- Chrome is likely checking the most recent Last-Modified date with the cached version to decide. But other times it does the 200 (from cache), which does not return any header information and appears that Chrome is simply assuming the file hasn't been modified instead of checking.

Google 自己的网站声明如下:

HTTP/S 支持浏览器本地缓存静态资源.一些最新的浏览器(例如 IE 7、Chrome)使用启发式来决定缓存所有没有显式缓存的资源需要多长时间标题.

HTTP/S supports local caching of static resources by the browser. Some of the newest browsers (e.g. IE 7, Chrome) use a heuristic to decide how long to cache all resources that don't have explicit caching headers.

但这并没有提供明确的答案.这个启发式是否在任何地方发布?我意识到可能没有一个固定的答案(比如 30 天),但一些通用的指导方针会很有用.此外,如果设置了 Last-Modified,我不明白 Chrome 为什么不先检查一下.

But this does not provide a definitive answer. Is this heuristic published anywhere? I realize there may not be a fixed answer (like 30 days), but some general guidelines would be useful. Furthermore, if Last-Modified is being set, I don't understand why Chrome isn't bothering to check that first.

推荐答案

浏览器认为缓存响应新鲜的时间通常与上次修改的时间相关:

The time the browser considers a cached response fresh is usually relative to when it was last modified:

由于源服务器并不总是提供明确的过期时间,缓存可以在未指定明确时间时分配启发式过期时间,采用使用其他标头字段值(例如上次修改时间)的算法......如果响应具有 Last-Modified 标头字段([RFC7232] 的第 2.2 节),则鼓励缓存使用启发式过期值,该值不超过该时间间隔的一部分.该分数的典型设置可能是 10%.[https://www.rfc-editor.org/rfc/rfc7234#section-4.2.2]

Since origin servers do not always provide explicit expiration times, a cache MAY assign a heuristic expiration time when an explicit time is not specified, employing algorithms that use other header field values (such as the Last-Modified time)... If the response has a Last-Modified header field (Section 2.2 of [RFC7232]), caches are encouraged to use a heuristic expiration value that is no more than some fraction of the interval since that time. A typical setting of this fraction might be 10%. [https://www.rfc-editor.org/rfc/rfc7234#section-4.2.2]

有关 Chrome(和其他浏览器)如何计算该值的详细信息,请参阅源代码 (Chrome v49 的示例).看起来 Chrome 也会计算相对于 Last-Modified 标头的值.

The details of how Chrome (and other browsers) calculate that value, can be found in the source code (An example from Chrome v49). It would appear that Chrome also calculates the value relative to the Last-Modified header.

(感谢这篇文章)

这篇关于如果未设置过期和/或无缓存标头,谷歌浏览器会缓存资源多长时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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