使用哪一个:Expire Header,Last Modified Header或ETags

时间:2022-10-06 07:54:00

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标题之间的区别是什么,应该在什么情况下使用哪个?

2 个解决方案

#1


21  

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:

也可以看看:

#2


31  

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.

如果必须发出请求,它将在HTTP请求中发送Last-Modified / ETag。如果文档的Etag值与之匹配,则服务器将发送304代码而不是200,而不是内容。浏览器将从其缓存加载内容。

I recommend using one of the strong caching headers, along with one of the weak caching headers.

我建议使用其中一个强大的缓存标头,以及一个弱缓存标头。

#1


21  

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:

也可以看看:

#2


31  

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.

如果必须发出请求,它将在HTTP请求中发送Last-Modified / ETag。如果文档的Etag值与之匹配,则服务器将发送304代码而不是200,而不是内容。浏览器将从其缓存加载内容。

I recommend using one of the strong caching headers, along with one of the weak caching headers.

我建议使用其中一个强大的缓存标头,以及一个弱缓存标头。