php 使用哪个:Expire Header、Last Modified Header 或 ETags
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5321876/
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
Which one to use : Expire Header, Last Modified Header or ETags
提问by Avinash
I am running PHP on Apache, and am confused about how to implement server-side caching, in order to make the site load faster.
我在 Apache 上运行 PHP,并且对如何实现服务器端缓存以加快站点加载速度感到困惑。
What is the difference between the Expires
, Last-Modified
and ETag
headers, and which one should be used in what situation?
Expires
,Last-Modified
和ETag
headers 有什么区别,在什么情况下应该使用哪一个?
采纳答案by mario
You can use the Expires
header in conjunction but regardless of the other two. It's universally supported by proxies and browser caches.
您可以Expires
结合使用标题,但不考虑其他两个。它受到代理和浏览器缓存的普遍支持。
The difference between ETag
and Last-Modified
stamps is more semantic. ETags are opaque to clients. It's usually a checksum. Whereas a Last-Modified header can be interpreted by clients. It's understood that the last modified timestamp works linearly.
ETag
和Last-Modified
邮票之间的区别更具语义。ETag 对客户端是不透明的。它通常是一个校验和。而客户端可以解释 Last-Modified 标头。据了解,最后修改的时间戳是线性工作的。
If a browser requests a resource with If-Unmodified-Since
, then a wide range of timestamps in the past can match such a condition. If your pages change frequently then a Last-Modified timestamp might be advantageous.
如果浏览器使用 请求资源If-Unmodified-Since
,那么过去的各种时间戳都可以匹配这样的条件。如果您的页面经常更改,则 Last-Modified 时间戳可能会更有利。
The ETag approach, on the other hand, leads to clients that save one last fingerprint per resource. (I'm not sure if browser caches remember multiple ETags). On requests, only one or a few possible If-None-Match
tokens are listed. This might mean more misses. Also, you have to compare multiple checksums, whereas with a Last-Modified timestamp you could have an arithmetic comparison.
另一方面,ETag 方法导致客户端为每个资源保存最后一个指纹。(我不确定浏览器缓存是否记得多个 ETag)。对于请求,仅If-None-Match
列出一个或几个可能的令牌。这可能意味着更多的失误。此外,您必须比较多个校验和,而使用 Last-Modified 时间戳您可以进行算术比较。
The real advantage of ETags is that you can reliably compare fingerprints. The Last-Modified timestamps are a bit more vague, as they don't verify if the actual page content changed.
ETags 的真正优势在于您可以可靠地比较指纹。Last-Modified 时间戳有点模糊,因为它们不验证实际页面内容是否更改。
See also:
也可以看看:
回答by hienbt88
Expires
and Cache-Control
are "strong caching headers"
Expires
并且Cache-Control
是“强缓存头”
Last-Modified
and ETag
are "weak caching headers"
Last-Modified
并且ETag
是“弱缓存头”
First the browser checks Expires/Cache-Control
to determine whether or not to make a request to the servers.
首先浏览器检查Expires/Cache-Control
以确定是否向服务器发出请求。
If it has to make a request, it will send Last-Modified/ETag
in the HTTP request. If the Etag
value of the document matches that, the server will send a 304 code instead of 200, and no content. The browser will load the contents from its cache.
如果它必须发出请求,它将发送Last-Modified/ETag
HTTP 请求。如果Etag
文档的值匹配,服务器将发送 304 代码而不是 200,并且没有内容。浏览器将从其缓存中加载内容。
I recommend using one of the strong caching headers, along with one of the weak caching headers.
我建议使用强缓存标头之一,以及弱缓存标头之一。
See also:
也可以看看: