HTML - 缓存控制最大年龄
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6486805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML - Cache control max age
提问by Uli
I'ld like to present always the latest website content to the user but also have it fast loaded. By researching I came across postings people suggesting to use the cache for speeding up loading.
我想始终向用户展示最新的网站内容,但也希望能够快速加载。通过研究,我发现有人建议使用缓存来加快加载速度。
So what do I need to add to my website to "overwrite" the cache after 3 days to display the latest content?
那么我需要在我的网站上添加什么以在 3 天后“覆盖”缓存以显示最新内容?
采纳答案by calumbrodie
There is more than one way to do this - but you need to consider exactly what you need to cache and what you don't. The biggest speed increases will likely come from making sure your assets (css, images, javascript) are cached, rather than the html itself. You then need to look at various factors (how often do these assets change, how will you force a user to download a new version of the file of you do change it?).
有不止一种方法可以做到这一点 - 但您需要准确考虑需要缓存的内容以及不需要缓存的内容。最大的速度提升可能来自确保您的资产(css、图像、javascript)被缓存,而不是 html 本身。然后,您需要查看各种因素(这些资产多久更改一次,您将如何强制用户下载您更改的文件的新版本?)。
Often as part of a sites release process, new files (updated files) are given a new filename to force the users browser to redownload the file, but this is only one approach.
通常作为站点发布过程的一部分,新文件(更新文件)被赋予一个新文件名以强制用户浏览器重新下载文件,但这只是一种方法。
You should take a look at apache mod_expire, and the ability to set expiry times for assets using the .htaccess file.
您应该查看 apache mod_expire,以及使用 .htaccess 文件设置资产到期时间的能力。
http://www.google.com/?q=apache+cache+control+htaccess#q=apache+cache+control+htaccess
http://www.google.com/?q=apache+cache+control+htaccess#q=apache+cache+control+htaccess
回答by james.garriss
The Cache-Control header is used in HTTP 1.1 to control the behavior of caches. The max-age directive is used to specify (in seconds) the maximum age of the content before it becomes stale (i.e., the content will not change for some period of time). So if you know that your content will not change for 3 days, you want your server to add the following HTTP header:
Cache-Control 标头在 HTTP 1.1 中用于控制缓存的行为。max-age 指令用于指定(以秒为单位)内容在变得陈旧之前的最大年龄(即,内容在一段时间内不会改变)。因此,如果您知道您的内容在 3 天内不会更改,您希望您的服务器添加以下 HTTP 标头:
Cache-Control: max-age=259200
(259200 = 60s x 60m x 24h x 3d)
(259200 = 60s x 60m x 24h x 3d)
To do that in PHP, add this line to your output:
要在 PHP 中执行此操作,请将此行添加到您的输出中:
header('Cache-Control: max-age=259200');
Read here for more info on the header function:
阅读此处了解有关标头功能的更多信息:
回答by fyr
As mentioned Expires and Cache-Control Headers are usually the best way to incorporate information about information lifetime.
如前所述,Expires 和 Cache-Control Headers 通常是合并信息生命周期信息的最佳方式。
Because clients are not very reliable on interpreting these informations proxies with caching capabilities like squid, varnish or such solutions are preferred by most people. You also need to consider if you want to cache only static content (like images, stylesheets, ..) or dynamically generated content as well.
由于客户端在解释这些信息时不是很可靠,因此大多数人更喜欢具有缓存功能(如鱿鱼、清漆或此类解决方案)的代理。您还需要考虑是否只想缓存静态内容(如图像、样式表等)或动态生成的内容。
回答by Darin Dimitrov
As per the YSlow recommendationsyou could configure your web server to add an Expires
or a Cache-Control
HTTP header to the response which will result in user agents caching the response for the specified duration.
根据YSlow 建议,您可以配置您的 Web 服务器以向响应添加一个Expires
或一个Cache-Control
HTTP 标头,这将导致用户代理在指定的持续时间内缓存响应。