2. 定向 Cache-Control 头字段 (Targeted Cache-Control Header Fields)
Targeted Cache-Control Header Field (下文称为 "targeted field") 是一个 HTTP 响应头字段, 其语义与 Cache-Control 响应头字段相同 ([HTTP-CACHING] 第 5.2 节). 但是, 它有一个不同的字段名, 用于指示其 cache directives 的目标.
例如:
CDN-Cache-Control: max-age=60
这是一个适用于 CDN 的 targeted field, 其定义见 第 3 节.
2.1. 语法
targeted fields 是 Dictionary Structured Fields ([STRUCTURED-FIELDS] 第 3.2 节). Dictionary 的每个成员都是一个 HTTP cache response directive ([HTTP-CACHING] 第 5.2.2 节), 包括 extension response directives (依据 [HTTP-CACHING] 第 5.2.3 节). 请注意, targeted fields 通常与 Cache-Control 字段具有相同语法, 但错误处理差异意味着使用 Cache-Control parser 而不是 Structured Fields parser 可能引入互操作性问题.
由于 cache directives 并不是按结构化数据类型定义的, 因此有必要将其值映射到适当类型. [HTTP-CACHING] 第 5.2 节将 cache directive values 定义为不存在, quoted-string 或 token.
这意味着没有值的 cache directives 将映射到 Boolean ([STRUCTURED-FIELDS] 第 3.3.6 节). 当值为 quoted-string 时, 它会映射到 String ([STRUCTURED-FIELDS] 第 3.3.3 节). 当值为 token 时, 它会根据值的内容映射到 Token ([STRUCTURED-FIELDS] 第 3.3.4 节), Integer ([STRUCTURED-FIELDS] 第 3.3.1 节) 或 Decimal ([STRUCTURED-FIELDS] 第 3.3.2 节).
例如, max-age directive ([HTTP-CACHING] 第 5.2.2.1 节) 具有整数值; no-store ([HTTP-CACHING] 第 5.2.2.5 节) 始终具有 Boolean true 值; no-cache ([HTTP-CACHING] 第 5.2.2.4 节) 的值可以是 Boolean true, 也可以是包含以逗号分隔字段名列表的字符串.
实现MUST NOT生成违反这些对 cache directive 值推断约束的值. 特别是, 第一个字符不是字母或 "*" 的字符串值MUST生成为 Strings, 以免被误认为其他类型.
实现SHOULD NOT消费违反这些推断约束的值. 例如, 消费实现如果将带 decimal 值的 max-age 强制转换为整数, 其行为将不同于其他实现, 可能造成互操作性问题.
除非明确规定其他处理方式, 否则在 cache directives 上收到的参数会被忽略.
如果给定响应中的 targeted field 为空, 或遇到解析错误, cache MUST忽略该字段, 即其行为如同该字段不存在, 很可能回退到存在的其他 cache-control 机制.
2.2. 缓存行为
实现本规范的 cache 维护一个 target list. target list 是该 cache 用于缓存策略的 targeted field names 的有序列表, 其顺序反映从最适用到最不适用的优先级. 根据实现不同, target list 可以是固定的, 用户可配置的, 或按请求生成的.
例如, CDN cache 可能同时支持 CDN-Cache-Control 和该 CDN 专用的头 ExampleCDN-Cache-Control, 后者覆盖前者. 它的 target list 将是:
[ExampleCDN-Cache-Control, CDN-Cache-Control]
当实现本规范的 cache 收到一个响应, 且该响应包含其 target list 上的一个或多个头字段名时, 该 cache MUST选择按 target-list 顺序第一个具有有效非空值的字段, 并使用其值确定该响应的缓存策略. 除非列出的头字段都没有可用的有效非空值, 否则它MUST忽略该响应中的 Cache-Control 和 Expires 头字段.
请注意, 这是逐响应发生的. 如果 cache 的 target list 中没有任何成员存在, 有效且非空, cache 会按 HTTP [HTTP-CACHING] 的要求回退到其他 cache control 机制.
不在某个 cache 的 target list 上的 targeted fields MUST NOT改变该 cache 的行为, 并且MUST被透传.
使用 targeted field 的 caches MUST实现以下 cache directives 的语义:
max-agemust-revalidateno-storeno-cacheprivate
此外, 它们SHOULD实现其在 Cache-Control 响应头字段中支持的其他 cache directives, 包括 extension cache directives.
targeted field 中 cache directives 的语义和优先级与 Cache-Control 中相同. 特别是, no-store 和 no-cache 会使 max-age 失效, 未识别的 extension directives 会被忽略.
2.3. 与 HTTP Freshness 的交互
HTTP caching 有一个单一的端到端 freshness 模型, 定义见 [HTTP-CACHING] 第 4.2 节. 当额外的 freshness 机制只对请求路径上的部分 caches 可用时, 例如使用 targeted fields, 需要仔细考虑它们之间的交互. 特别是, targeted cache 可能具有比其他 caches 更长的 freshness lifetimes, 导致它向这些其他 caches 提供看似过早变 stale, 甚至立即 stale 的响应, 从而对缓存效率产生负面影响.
例如, CDN cache 存储的响应可能带有以下头:
Age: 1800
Cache-Control: max-age=600
CDN-Cache-Control: max-age=3600
从 CDN 的角度看, 该响应在缓存 30 分钟后仍然 fresh; 而从其他 caches 的角度看, 该响应已经 stale. 更多讨论见 [AGE-PENALTY].
当 targeted cache 具有强一致性机制时, 例如 origin server 能够主动使缓存响应失效, 通常希望缓解这些影响. 部署中出现的一些技术包括:
- 移除
Age头字段 - 将
Date头字段值更新为当前时间 - 将
Expires头字段值更新为当前时间加上任何Cache-Control: max-age值
本规范不对实现提出任何缓解这些影响的具体要求, 但 targeted fields 的定义可以提出这类要求.
2.4. 定义 Targeted Fields
可以通过请求在 "Hypertext Transfer Protocol (HTTP) Field Name Registry" (<https://www.iana.org/assignments/http-fields/>) 中注册, 为特定 cache 类别定义 targeted field.
注册请求可以使用本文档作为规范文档. 在这种情况下, Comments 字段应清楚定义该 targeted field 适用的 cache 类别. 或者, 如果已经为该字段创建了其他文档, 也可以将其用作规范文档.
按照约定, targeted fields 具有后缀 "-Cache-Control", 例如 "ExampleCDN-Cache-Control". 但是, 该后缀MUST NOT单独用于识别 targeted fields; 它只是一个约定.