不同类型资源的理想 HTTP 缓存控制标头 [英] Ideal HTTP cache control headers for different types of resources

查看:24
本文介绍了不同类型资源的理想 HTTP 缓存控制标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到一组最小的标头,它适用于所有"缓存和浏览器(也在使用 HTTPS 时!)

在我的网站上,我将拥有三种资源:

(1) 永远可缓存(对所有用户公开/平等)

示例:0A470E87CC58EE133616F402B5DDFE1C.cache.html(自动生成GWT)

  • 这些文件在更改内容时会自动分配一个新名称(基于 MD5).

  • 它们应该尽可能多地缓存,即使在使用 HTTPS 时也是如此(所以我假设,我应该设置 Cache-Control: public,尤其是对于 Firefox?)

  • 如果内容发生更改,他们不应该要求客户端往返于服务器进行验证.

(2) 不定期更改(公开/对所有用户均等)

示例:index.html、mymodule.nocache.js

  • 在部署新版本的网站时,这些文件会在不更改 URL 的情况下更改其内容.

  • 它们可以被缓存,但可能每次都需要一个往返来重新验证.

(3) 每个请求的个人(私人/用户特定)

示例:JSON 响应

  • 在任何情况下都不应该将这些资源以未加密的方式缓存到磁盘.(除了我可能会有一些可以缓存的特定请求.)
<小时>

对于每种类型我可能会使用哪些标头,我有一个大致的想法,但总有一些我可能会遗漏的地方.

解决方案

我可能会使用这些设置:

  1. Cache-Control: max-age=31556926 – 表示可以被任何缓存缓存.缓存的表示将被视为新鲜的 1 年:<块引用>

    要将响应标记为永不过期",源服务器发送一个到期 大约一年后收到回复发送.HTTP/1.1 服务器不应该发送多个 Expires 日期未来一年.

  2. Cache-Control: no-cache – 表示可以被任何缓存缓存.但是缓存必须在发布缓存副本之前将请求提交给源服务器进行验证.
  3. Cache-Control: no-store – 缓存不得在任何情况下缓存表示.

有关详细信息,请参阅Mark Nottingham 的缓存教程.

I want to find a minimal set of headers, that work with "all" caches and browsers (also when using HTTPS!)

On my web site, I'll have three kinds of resources:

(1) Forever cacheable (public / equal for all users)

Example: 0A470E87CC58EE133616F402B5DDFE1C.cache.html (auto generated by GWT)

  • These files are automatically assigned a new name, when they change content (based on the MD5).

  • They should get cached as much as possible, even when using HTTPS (so I assume, I should set Cache-Control: public, especially for Firefox?)

  • They shouldn't require the client to make a round-trip to the server to validate, if the content has changed.

(2) Changing occasionally (public / equal for all users)

Examples: index.html, mymodule.nocache.js

  • These files change their content without changing the URL, when a new version of the site is deployed.

  • They can be cached, but probably need a round-trip to be revalidated every time.

(3) Individual for each request (private / user specific)

Example: JSON responses

  • These resources should never be cached unencrypted to disk under no circumstances. (Except maybe I'll have a few specific requests that could be cached.)

I have a general idea on which headers I would probably use for each type, but there's always something I could be missing.

解决方案

I would probably use these settings:

  1. Cache-Control: max-age=31556926 – Representations may be cached by any cache. The cached representation is to be considered fresh for 1 year:

    To mark a response as "never expires," an origin server sends an Expires date approximately one year from the time the response is sent. HTTP/1.1 servers SHOULD NOT send Expires dates more than one year in the future.

  2. Cache-Control: no-cache – Representations are allowed to be cached by any cache. But caches must submit the request to the origin server for validation before releasing a cached copy.
  3. Cache-Control: no-store – Caches must not cache the representation under any condition.

See Mark Nottingham’s Caching Tutorial for further information.

这篇关于不同类型资源的理想 HTTP 缓存控制标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆