3. Storing Responses in Caches

1 阅读4分钟

3. Storing Responses in Caches

rfc 7234 3. Storing Responses in Caches

判断response是否能被存入cache

优先使用1.进行判断,如果没有结果则使用2.进行判断

1.Cache Control Extension

如果cache认识(recognized)这个Cache Control Extension,则reponse是否可以被存进cache只以这个extension定义为准。因为Extension可以override其他条件:

   Note that any of the requirements listed above can be overridden by a
   cache-control extension;

当没Extension或者cache不认识(unrecognized)这个Extension,则使用2.进行判断.

2.其余条件全部满足

注意到每个条件后面使用的是"and",这意味着所有的都得满足,此时的判断逻辑如下

// 1.没有判断出结果时
2. = (条件A) AND (条件B) AND (条件C) AND (条件D) AND (条件E) AND (条件F)
条件"o the response either"的理解

注意到使用的是"either or",所以只需要其中至少一个情况满足此条件就满足。

contains an Expires header field (see Section 5.3), or

只要包含Expires就行,不管值如何

Expires : 0 

上述这种值也是满足条件的,如果一个被存入cache的response的Expires为0,则被判定为stale,cache会去服务器验证,服务器如果返回304,则本地缓存的response也 能发挥作用。

contains a max-age response directive (see Section 5.2.2.8), or

max-age也是类似,只要包含条件就满足,不管值是多少。

这个不懂是什么意思(RFC_TODO):

contains a Cache Control Extension (see Section 5.2.3) that
         allows it to be cached

也许能参考后面code 302的分析?

最终判断逻辑表达

可以存储 = (1.) or (2.)

条件中内容解释

cacheable method

cacheable指method具备了参与"是否被reuse"判断的资格。目前具有cacheable的method如下:

GET        cacheable
HEAD       cacheable
POST       cacheable,当response包含explicit freshness information时
PATCH      cacheable,当response包含explicit freshness information时
GET

rfc 7231 4.3.1. GET

复用场景

1.GET -> GET
2.GET -> HEAD
HEAD

rfc 7231 4.3.2. HEAD

复用场景

HEAD -> HEAD
POST

rfc 7231 4.3.3. POST

复用场景

POST -> GET

cacheable条件:

Responses to POST requests are only cacheable when they include
   explicit freshness information (see Section 4.2.1 of [RFC7234]).
   However, POST caching is not widely implemented.  For cases where an
   origin server wishes the client to be able to cache the result of a
   POST in a way that can be reused by a later GET, the origin server
   MAY send a 200 (OK) response containing the result and a
   Content-Location header field that has the same value as the POST's
   effective request URI (Section 3.1.4.2).

PATCH

rfc 5789 2. The PATCH Method

复用场景

1.PATCH -> GET
2.PATCH -> HEAD

cacheable条件:

   A response to this method is only cacheable if it
   contains explicit freshness information (such as an Expires header or
   "Cache-Control: max-age" directive) as well as the Content-Location
   header matching the Request-URI, indicating that the PATCH response
   body is a resource representation.  A cached PATCH response can only
   be used to respond to subsequent GET and HEAD requests; it MUST NOT
   be used to respond to other methods (in particular, PATCH).

cacheable status code

默认情况下为cacheable的code

200, 203, 204, 206, 300, 301, 404, 405, 410, 414, and 501:

这章中每个code的定义中会标注是否是cacheable rfc 7231 Response Status Codes

308:

308 cacheable的定义在这里rfc 7238 3. 308 Permanent Redirect

  A 308 response is cacheable by default; i.e., unless otherwise
indicated by the method definition or explicit cache controls (see
    [RFC7234], Section 4.2.2)
默认不是cacheable,但是满足特定条件以后是

302:

rfc 1945:302 Moved Temporarily

rfc 2616:302 Found

rfc 9110:302 Found

目前只在rfc 2616中发现了对302的cachealbe定义:

 This response is only cacheable if indicated by a Cache-Control or Expires header
   field.

OkHttp中对应的cacheable判定:

   // These codes can only be cached with the right response headers.
// http://tools.ietf.org/html/rfc7234#section-3
// s-maxage is not checked because OkHttp is a private cache that should ignore s-maxage.

if (
    // Expires
    response.header("Expires") == null &&
    
    // Cache-Control
    response.cacheControl.maxAgeSeconds == -1 &&
    !response.cacheControl.isPublic &&
    !response.cacheControl.isPrivate
) {
    return false
}

所以暂时(RFC_GUSS)将302可以cacheable的条件理解为stored response满足如下任意一个条件:

  • 包含Expires header
  • 包含Cache-Control:s-maxage
  • 包含Cache-Control:max-age
  • 包含Cache-Control:public
  • 包含Cache-Control:private

307:

rfc 2616:307 Temporary Redirect

rfc 9110:307 Temporary Redirect

目前只在rfc 2616中发现了对307的cachealbe定义:

 This response is only cacheable if indicated by a Cache-Control or Expires header
   field.

这句话的意思参考code 302中的分析。

Authorization header

 o  the Authorization header field (see Section 4.2 of [RFC7235]) does
      not appear in the request, if the cache is shared, unless the
      response explicitly allows it (see Section 3.2), and

shared cache 不能(MUST NOT)缓存request(Authorization header)-response,除非response至少携带:

  • public
  • s-maxage
  • must-revalidate