跳到主要内容

4. 从缓存构造响应 (Constructing Responses from Caches)

当收到一个 request 时, cache MUST NOT 重用已存储的 response, 除非:

  • 所呈现的 effective request URI (Section 5.5 of [RFC7230]) 与已存储 response 的 effective request URI 匹配, 并且
  • 与已存储 response 关联的 request method 允许它用于所呈现的 request, 并且
  • 已存储 response 指定的 selecting header field (如果有) 与所呈现 request 中的对应 field 匹配 (见 Section 4.1), 并且
  • 所呈现 request 不包含 no-cache pragma (Section 5.4), 也不包含 no-cache cache directive (Section 5.2.1), 除非已存储 response 已成功 validation (Section 4.3), 并且
  • 已存储 response 不包含 no-cache cache directive (Section 5.2.2.2), 除非它已成功 validation (Section 4.3), 并且
  • 已存储 response 满足以下任一条件:
    • fresh (见 Section 4.2), 或
    • 允许以 stale 状态提供 (见 Section 4.2.4), 或
    • 已成功 validation (见 Section 4.3).

注意, 上述任一 requirement 都可以被 cache-control extension 覆盖; 见 Section 5.2.3.

当使用已存储 response 在不进行 validation 的情况下满足 request 时, cache MUST 生成 Age header field (Section 5.1), 并将 response 中已有的任何 Age field 替换为等于该已存储 response 的 current_age 的值; 见 Section 4.2.3.

cache MUST 将使用 unsafe method (Section 4.2.1 of [RFC7231]) 的 request write through 到 origin server; 即 cache 不允许在转发 request 并收到对应 response 之前, 为此类 request 生成 reply.

另外注意, unsafe request 可能会 invalidate 已存储的 response; 见 Section 4.4.

当存储了多个合适的 response 时, cache MUST 使用最新的 response (由 Date header field 确定). 它也可以带着 "Cache-Control: max-age=0" 或 "Cache-Control: no-cache" 转发 request, 以消除应使用哪个 response 的歧义.

没有可用 clock 的 cache MUST NOT 使用已存储 response, 除非每次使用时都重新 validation.

4.1. 使用 Vary 计算 Secondary Key (Calculating Secondary Keys with Vary)

当 cache 收到一个 request, 且该 request 可以由带有 Vary header field (Section 7.1.4 of [RFC7231]) 的已存储 response 满足时, cache MUST NOT 使用该 response, 除非 Vary header field 指定的所有 selecting header field 在 original request (即与已存储 response 关联的 request) 和所呈现 request 中都匹配.

来自两个 request 的 selecting header field 被定义为匹配, 当且仅当第一个 request 中的这些 field 可以通过应用以下任一方式转换为第二个 request 中的对应 field:

  • 在 header field 语法允许的位置添加或删除 whitespace
  • 合并具有相同 field name 的多个 header field (见 Section 3.2 of [RFC7230])
  • 按照该 header field 的 specification, 以已知具有相同 semantics 的方式 normalize 两个 header field value (例如, 当顺序不重要时重新排序 field value; 当 value 被定义为 case-insensitive 时进行 case-normalization)

如果在可能发生的任何 normalization 之后, 某个 header field 在一个 request 中缺失, 则它只有在另一个 request 中也缺失时才能匹配.

Vary header field value 为 "*" 时总是匹配失败.

具有匹配 selecting header field 的已存储 response 称为 selected response.

如果有多个 selected response 可用 (可能包括没有 Vary header field 的 response), cache 需要选择一个使用. 当某个 selecting header field 有已知机制可用于选择 (例如 Accept 和类似 request header field 上的 qvalue) 时, 该机制 MAY 用于选择 preferred response; 对剩余 response, 按 Section 4 使用最新 response (由 Date header field 确定).

如果没有 selected response 可用, cache 无法满足所呈现的 request. 通常会把它转发给 origin server, 作为一个 request (可能是 conditional request; 见 Section 4.3).