跳到主要内容

3. 在缓存中存储响应 (Storing Responses in Caches)

cache MUST NOT 存储任何 request 的 response, 除非满足以下所有条件:

  • request method 被 cache 理解, 并且被定义为可缓存, 并且
  • response status code 被 cache 理解, 并且
  • "no-store" cache directive (见 Section 5.2) 没有出现在 request 或 response header field 中, 并且
  • 如果 cache 是 shared cache, response 中没有出现 "private" response directive (见 Section 5.2.2.6), 并且
  • 如果 cache 是 shared cache, request 中没有出现 Authorization header field (见 [RFC7235] Section 4.2), 除非 response 明确允许这样做 (见 Section 3.2), 并且
  • response 满足以下任一条件:
    • 包含 Expires header field (见 Section 5.3), 或
    • 包含 max-age response directive (见 Section 5.2.2.8), 或
    • 包含 s-maxage response directive (见 Section 5.2.2.9) 且 cache 是 shared cache, 或
    • 包含允许其被缓存的 Cache Control Extension (见 Section 5.2.3), 或
    • 具有被默认定义为可缓存的 status code (见 Section 4.2.2), 或
    • 包含 public response directive (见 Section 5.2.2.5).

注意, 上面列出的任何 requirement 都可以被 cache-control extension 覆盖; 见 Section 5.2.3.

在此上下文中, 如果 cache 识别某个 request method 或 response status code, 并实现所有已指定的缓存相关行为, 则称 cache 已经 "understood" 该方法或状态码.

注意, 在正常运行中, 某些 cache 不会存储既没有 cache validator 也没有显式 expiration time 的 response, 因为此类 response 通常不值得存储. 但是, cache 并未被禁止存储此类 response.

3.1. 存储不完整响应 (Storing Incomplete Responses)

当 message framing ([RFC7230]) 指示的所有 octet 都在连接关闭前收到时, response message 被认为是完整的. 如果 request method 是 GET, response status code 是 200 (OK), 并且已经收到整个 response header section, 则 cache MAY 存储不完整的 response message body, 前提是 cache entry 被记录为 incomplete. 同样, 206 (Partial Content) response MAY 像不完整的 200 (OK) cache entry 一样被存储. 但是, 如果 cache 不支持 Range 和 Content-Range header field, 或者不理解这些 field 中使用的 range unit, cache MUST NOT 存储 incomplete response 或 partial-content response.

cache MAY 通过发起后续 range request ([RFC7233]) 并按 Section 3.3 的定义将 successful response 与 stored entry 组合, 来补全已存储的不完整 response. cache MUST NOT 使用不完整 response 来回答 request, 除非该 response 已经被补全, 或者该 request 是 partial request 且指定的 range 完全位于该不完整 response 内. cache MUST NOT 在未使用 206 (Partial Content) status code 明确标记的情况下向 client 发送 partial response.

3.2. 存储已认证请求的响应 (Storing Responses to Authenticated Requests)

对于带有 Authorization header field ([RFC7235] Section 4.2) 的 request, shared cache MUST NOT 使用其 cached response 来满足任何后续 request, 除非 response 中存在允许存储此类 response 的 cache directive.

在本规范中, 以下 Cache-Control response directive (Section 5.2.2) 具有这种效果: must-revalidate, public 和 s-maxage.

注意, 包含 "must-revalidate" 和/或 "s-maxage" response directive 的 cached response 不允许由 shared cache 以 stale 形式提供 (Section 4.2.4). 特别是, 带有 "max-age=0, must-revalidate" 或 "s-maxage=0" 的 response 不能在未向 origin server 重新验证的情况下用于满足后续 request.

3.3. 合并部分内容 (Combining Partial Content)

如果连接过早关闭, 或者 request 使用了一个或多个 Range specifier ([RFC7233]), response 可能只传输 partial representation. 多次此类传输之后, cache 可能已经收到同一 representation 的多个 range. 如果这些 range 全部共享同一个 strong validator, 且 cache 遵守 [RFC7233] Section 4.3 中的 client requirement, cache MAY 将这些 range 合并成一个 stored response, 并重用该 response 来满足后续 request.

将新 response 与一个或多个 stored response 合并时, cache MUST:

  • 删除 stored response 中 warn-code 为 1xx 的任何 Warning header field (见 Section 5.5);
  • 保留 stored response 中 warn-code 为 2xx 的任何 Warning header field; 并且
  • 使用新 response 中提供的其他 header field, 除 Content-Range 之外, 替换 stored response 中对应 header field 的所有实例.