Freshness And Stale

定义

从缓存的角度来说,每一个http response可以分为fresh response或stale response。fresh可译为新鲜度,通过freshness lifetime对其进行界定。处于freshness lifetime的请求不需要和服务端进行通话,直接在浏览器取得缓存;相反,超出freshness lifetime的请求需要与服务端通话,或进行有效性验证,或直接重新请求。

freshness lifetime

A response's freshness lifetime is the length of time between its
generation by the origin server and its expiration time. An explicit
expiration time is the time at which the origin server intends that a
stored response can no longer be used by a cache without further
validation, whereas a heuristic expiration time is assigned by a
cache when no explicit expiration time is available.

引用了文档对freshness lifetime的介绍,可知其实际分为显性和隐性两种。

显性(explicit expiration time)

指的是请求头含有指示明确的信息,比如:s-maxage,max-age,Expires等。在这些请求头规定的时间内,就是显性的freshness lifetime;

隐性(heuristic expiration time)
  1. 没有上面提到的任何显性的时间
  2. 比如含有Cache-Control: public和Last-Modified等条件

贴一下文档原话:

heuristics can only be used on responses without explicit freshness whose status codes are defined as cacheable by default , and those responses without explicit freshness that have been marked as explicitly cacheable (e.g., with a "public" response directive).

stale response

一个不属于fresh response就是stale response,通常这时候要进行有效度验证。

小结

判定缓存的新鲜度,实际上有3中情况: 显性新鲜,隐性新鲜,不新鲜。只要是新鲜的,都不需要和服务器对接,直接从缓存中获取资源。

no-cache 和 must-revalidate

这里指的是response 的no-cache。它们有什么区别呢?

  1. no-cache指不需要判断新鲜度,直接从有效度验证开始
  2. must-revalidate指超过freshness lifetime,就必须进行有效度验证
  3. 所以no-cache, 和 max-age=0、must-revalidate是等效的
  4. must-revalidate通常用在对新鲜度要求严格的情况,比如说财务信息;

参考资料

https://stackoverflow.com/questions/18148884/difference-between-no-cache-and-must-revalidate
https://tools.ietf.org/html/rfc7234#section-4.2
https://tools.ietf.org/html/rfc7234#section-5.2.2.1

你可能感兴趣的:(Freshness And Stale)