Expires 和 Cache-control:max-age 有什么区别?
你能告诉我 Expires 和 Cache-control:max-age 的区别吗?
Could you tell me the difference of Expires and Cache-control:max-age?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Expires
在HTTP/1.0
规范中定义,Cache-Control
在HTTP/1.1
规范中定义。我建议定义两者,以便同时满足仅理解
HTTP/1.0
的旧客户端和新客户端的需求。Expires
was defined in theHTTP/1.0
specifications, andCache-Control
in theHTTP/1.1
specifications.I would suggest defining both so you cater to both, the older clients that only understand
HTTP/1.0
, and the newer ones.与早期 HTTP 1.1 规范中引入的
Cache-Control: max-age
相比,HTTP 1.0 规范中指定了Expires
。Expires
标头的值必须采用非常特定的日期和时间格式,任何错误都将导致您的资源不可缓存。Cache-Control: max-age
标头发送到浏览器时的值以秒为单位,其中发生任何错误的可能性相当小。由于您只能在 web.config 文件中指定两个标头之一,因此我建议使用
Cache-Control: max-age
标头,因为它在设置相对时间跨度方面提供了灵活性从现在的日期到未来的某个日期。与Expires
标头的情况相比,您基本上可以设置并忘记,您必须记住每年至少更新一次其值。如果您在代码中以编程方式设置这两个标头,请注意Cache-Control: max-age
标头的值将优先于Expires
标头。因此,也需要记住一些事情。来自设置过期和缓存控制:max-age ASP.NET 中静态资源的标头
Expires
was specified in HTTP 1.0 specification as compared toCache-Control: max-age
, which was introduced in the early HTTP 1.1 specification. The value of theExpires
header has to be in a very specific date and time format, any error in which will make your resources non-cacheable. TheCache-Control: max-age
header's value when sent to the browser is in seconds, the chances of any error happening in which is quite less.Since you can specify only one of the two headers in your web.config file, I'd suggest going with the
Cache-Control: max-age
header because of the flexibility it offers in setting a relative timespan from the present date to a date in the future. You can basically set and forget, as compared to the case withExpires
header, whose value you will have to remember to update at least once every year. And if you set both headers programmatically from within your code, know that the value ofCache-Control: max-age
header will take precedence overExpires
header. So, something to keep in mind there as well.From Setting Expires and Cache-Control: max-age headers for static resources in ASP.NET