Expires 和 Cache-Control 标头有什么区别?

发布于 2024-11-03 16:49:35 字数 67 浏览 0 评论 0原文

ExpiresCache-Control 标头之间有什么区别?

What’s the difference between Expires and Cache-Control headers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

捂风挽笑 2024-11-10 16:49:35

它们都可以用来控制缓存响应的生命周期,但是 HTTP/1.1 中引入的 Cache-Control 提供了更多功能,并且通常应该比 Expires 更受青睐, HTTP/1.0 中定义。

在现代浏览器中, Cache-Control: max-ageCache-Control: s-maxage 指令也会覆盖响应中的任何 Expires 标头

作为一个具体示例,Cache-Control: max-age=604800 指示应考虑给定 HTTP 响应的秒数新鲜。换句话说,在 HTTP 响应生成后 604800 秒(7 天),响应应被视为陈旧且可从缓存中逐出。

Expires 使用显式的日期时间字符串,例如 “Friday, 28 Feb 2020 20:20:20 GMT”,并且显然比简单的数字更难解析(和生成!)秒整数。

其他了解 HTTP 缓存工作原理的好资源:

They can both be used to control the lifetime of a cached response but Cache-Control, introduced in HTTP/1.1, offers many more features and should generally be favored over Expires, defined in HTTP/1.0.

In modern browsers a Cache-Control: max-age or Cache-Control: s-maxage directive will also override any Expires header in the response.

As a concrete example, Cache-Control: max-age=604800 indicates the number of seconds that the given HTTP response should be considered fresh. In other words, the response should be considered stale and evictable from the cache 604800 seconds (7 days) after the HTTP response was generated.

Expires uses explicit datetime strings like "Friday, 28 Feb 2020 20:20:20 GMT" and is obviously more challenging to parse (and generate!) than a simple number of seconds integer.

Other good resources for understanding how HTTP caching works:

挽清梦 2024-11-10 16:49:35

如果您使用 CDN(云交付网络),我建议使用 Cache-Control,最大使用时间以秒为单位。例如缓存控制:max-age=604800。
这可以防止您的原始服务器出现请求高峰:通过“Expires Wed, 30 Oct 20xx 04:37:07 GMT”,所有浏览器都会同时请求您。

If you are using a CDN (Cloud Delivery Network) I recommend to use Cache-Control with a max-age time in seconds. For example Cache-Control: max-age=604800.
This prevents request-peaks to your origin-server: With "Expires Wed, 30 Oct 20xx 04:37:07 GMT" all browsers will request you at the same time.

暗恋未遂 2024-11-10 16:49:35

根据这篇 Google 开发者文章,HTTP 缓存

Cache-Control 标头被定义为 HTTP/1.1 规范的一部分
并取代以前用于定义响应的标头(例如 Expires)
缓存策略。所有现代浏览器都支持缓存控制,因此
这就是我们所需要的。

According to this Google Developers article, HTTP Caching:

Cache-Control header was defined as part of the HTTP/1.1 specification
and supersedes previous headers (e.g. Expires) used to define response
caching policies. All modern browsers support Cache-Control, hence
that is all we will need.

淡写薰衣草的香 2024-11-10 16:49:35

Cache-Control 是在 HTTP/1.1 中定义的,告诉从服务器到客户端的所有缓存机制是否可以缓存该对象。它以秒为单位:Cache-Control: max-age=3600

Expires 标头字段给出了响应被视为过时的日期/时间。 Expires 值是 HTTP 日期时间戳:Expires: Tue, 18 Jul 2017 16:07:23 GMT

如果响应包含带有 max-age 指令的 Cache-Control 字段,则接收者必须忽略 Expires 字段。

Cache-Control was defined in HTTP/1.1, tells all caching mechanisms from server to client whether they may cache this object. It is measured in seconds: Cache-Control: max-age=3600.

The Expires header field gives the date/time after which the response is considered stale. The Expires value is an HTTP-date timestamp: Expires: Tue, 18 Jul 2017 16:07:23 GMT.

If a response includes a Cache-Control field with the max-age directive, a recipient MUST ignore the Expires field.

挽清梦 2024-11-10 16:49:35

Heroku 开发中心有一篇关于此主题的优秀文章

引述其中,

虽然 Cache-Control 标头打开客户端缓存并设置资源的 max-age,但 Expires 标头用于指定资源不再有效的特定时间点。

Heroku devcenter has an excellent article on this subject.

Quoting from it,

While the Cache-Control header turns on client-side caching and sets the max-age of a resource, the Expires header is used to specify a specific point in time the resource is no longer valid.

冰火雁神 2024-11-10 16:49:35

除了CC的私有/公共选项之外,我看不出有什么区别。当使用诸如“访问加 1 年/月/周/日”之类的 Expires 时,它的工作方式与 CC 完全相同。

Except for the private/public options of CC, I can't see any difference. When using Expires like "access plus 1 year/month/week/day", it works in exactly the same way as CC does.

木森分化 2024-11-10 16:49:35

如果你仍然感兴趣,我直接从谷歌的男孩那里留下这个推荐。
https://developers.google.com/speed/docs/insights/LeverageBrowserCaching
他们更喜欢 Expires before 而不是 Cache-Control

If you are still interested, I leave this recommendation directly from google's boys.
https://developers.google.com/speed/docs/insights/LeverageBrowserCaching
They prefer Expires before than Cache-Control

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文