简体   繁体   English

如果未设置过期和/或无缓存标头,Google Chrome会缓存资源多长时间?

[英]How long does Google Chrome cache a resource if expires and/or no-cache headers are not set?

We have been having a problem with Chrome caching a resource on our Glassfish server. 我们在使用Chrome缓存Glassfish服务器上的资源时出现问题。 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. 即使存在Last-Modified标头,Chrome也不会发送过期和无缓存标头,并且资源(大约4 MB SWF文件)正在被缓存。

Sometimes Chrome will get a 304 code, and other times it simply does a 200 (from cache). 有时Chrome会获得304代码,有时它只会获得200代码(来自缓存)。 I understand the 304 -- Chrome is likely checking the most recent Last-Modified date with the cached version to decide. 我了解304 - Chrome可能会使用缓存版本来确定最近的Last-Modified日期。 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. 但有时它会执行200(来自缓存),它不返回任何标题信息,并且看起来Chrome只是假设文件未被修改而不是检查。

Google's own site states the following: 谷歌自己的网站声明如下:

HTTP/S supports local caching of static resources by the browser. HTTP / S支持浏览器对静态资源的本地缓存。 Some of the newest browsers (eg IE 7, Chrome) use a heuristic to decide how long to cache all resources that don't have explicit caching headers. 一些最新的浏览器(例如IE 7,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. 我意识到可能没有一个固定的答案(如30天),但一些通用指南会很有用。 Furthermore, if Last-Modified is being set, I don't understand why Chrome isn't bothering to check that first. 此外,如果设置了Last-Modified,我不明白为什么Chrome没有先烦回来检查。

DEFAULT_CACHE_TIME = 300

I found the above by searching at http://code.google.com/p/chromium/source/search?q=DEFAULT_CACHE_TIME&origq=DEFAULT_CACHE_TIME&btnG=Search+Trunk for "DEFAULT_CACHE_TIME". 我通过在http://code.google.com/p/chromium/source/search?q=DEFAULT_CACHE_TIME&origq=DEFAULT_CACHE_TIME&btnG=Search+Trunk上搜索“DEFAULT_CACHE_TIME”找到了上述内容。

There's a file called "chromeextensionsdocs.py" that contains the DEFAULT_CACHE_TIME. 有一个名为“chromeextensionsdocs.py”的文件,其中包含DEFAULT_CACHE_TIME。

I believe this is seconds based on the example given at http://code.google.com/appengine/docs/python/memcache/overview.html 相信这是秒,基于http://code.google.com/appengine/docs/python/memcache/overview.html上给出的示例
In "chromeextensionsdocs.py", the DEFAULT_CACHE_TIME is sent as the last param in memcache.add 在“chromeextensionsdocs.py”中,DEFAULT_CACHE_TIME作为memcache.add的最后一个参数发送

I'm not totally sure if this is the right value or not but seems likely that it is when putting the pieces together. 我不完全确定这是否是正确的值,但似乎很可能是将它们组合在一起。

The time the browser considers a cached response fresh is usually relative to when it was last modified: 浏览器认为缓存响应的时间通常与上次修改时相关:

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. 由于源服务器并不总是提供明确的到期时间,因此缓存可以在未指定显式时间时指定启发式到期时间,使用使用其他头字段值的算法(例如Last-Modified时间)...如果响应具有Last-Modified头字段([RFC7232]的第2.2节),鼓励缓存使用启发式到期值,该值不超过自那时起的间隔的某个部分。 A typical setting of this fraction might be 10%. 此分数的典型设置可能是10%。 [ https://tools.ietf.org/html/rfc7234#section-4.2.2] [ https://tools.ietf.org/html/rfc7234#section-4.2.2]

The details of how Chrome (and other browsers) calculate that value, can be found in the source code ( An example from Chrome v49 ). 有关Chrome(和其他浏览器)如何计算该值的详细信息,请参阅源代码( Chrome v49中的示例 )。 It would appear that Chrome also calculates the value relative to the Last-Modified header. 看起来Chrome还会计算相对于Last-Modified标头的值。

( Credit to this post ) 感谢这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM