Which one to use : Expire Header, Last Modified Header or ETags

久未见 提交于 2021-02-05 12:43:11

问题


I am running PHP on Apache, and am confused about how to implement server-side caching, in order to make the site load faster.

What is the difference between the Expires, Last-Modified and ETag headers, and which one should be used in what situation?


回答1:


You can use the Expires header in conjunction but regardless of the other two. It's universally supported by proxies and browser caches.

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.

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.

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.

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.

See also:

  • ETag vs Header Expires
  • http://www.mnot.net/blog/2007/08/07/etags



回答2:


Expires and Cache-Control are "strong caching headers"

Last-Modified and ETag are "weak caching headers"

First the browser checks Expires/Cache-Control to determine whether or not to make a request to the servers.

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.

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

See also:

  • Google Web Fundamentals: HTTP-Caching
  • MDN web docs: HTTP caching


来源:https://stackoverflow.com/questions/5321876/which-one-to-use-expire-header-last-modified-header-or-etags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!