跳到主要内容

4.2.3. 计算 Age (Calculating Age)

Age header field 用于在 response message 从 cache 获得时传达其估计 age. Age field value 是 cache 对自 response 由 origin server 生成或 validation 以来已经过去多少秒的估计. 本质上, Age value 是 response 在从 origin server 出发的路径上驻留于每个 cache 的时间总和, 加上它在网络路径中传输的时间.

age 计算使用以下数据:

age_value: 术语 age_value 表示 Age header field (Section 5.1) 的值, 其形式适合算术运算; 如果不可用, 则为 0.

date_value: 术语 date_value 表示 Date header field 的值, 其形式适合算术运算. Date header field 的定义以及关于缺少该 field 的 response 的 requirement, 见 [RFC7231] Section 7.1.1.2.

now: 术语 now 表示 "执行计算的 host 上时钟的当前值". host ought to 使用 NTP ([RFC5905]) 或类似协议将其时钟同步到 Coordinated Universal Time.

request_time: 发出导致 stored response 的 request 时, host 上时钟的当前值.

response_time: 接收到 response 时, host 上时钟的当前值.

response 的 age 可以用两种完全独立的方式计算:

  1. apparent_age: 如果本地时钟与 origin server 的时钟合理同步, 则为 response_time 减去 date_value. 如果结果为负, 则用零替换该结果.

  2. corrected_age_value: 如果 response path 上的所有 cache 都实现 HTTP/1.1. cache MUST 相对于 request 发起的时间解释该值, 而不是相对于 response 被接收的时间.

apparent_age = max(0, response_time - date_value);

response_delay = response_time - request_time;
corrected_age_value = age_value + response_delay;

二者组合如下:

corrected_initial_age = max(apparent_age, corrected_age_value);

除非 cache 对 Age header field 的值有信心, 例如因为 Via header field 中没有 HTTP/1.0 hop, 在这种情况下 corrected_age_value MAY 用作 corrected_initial_age.

随后, 可以将 stored response 自上次由 origin server validation 以来经过的时间量 (以秒为单位) 加到 corrected_initial_age 上, 计算 stored response 的 current_age.

resident_time = now - response_time;
current_age = corrected_initial_age + resident_time;

4.2.4. 提供 Stale 响应 (Serving Stale Responses)

"stale" response 是这样一种 response: 它具有 explicit expiry information, 或者允许计算 heuristic expiry, 但根据 Section 4.2 的计算并不 fresh.

如果 explicit in-protocol directive 禁止生成 stale response, cache MUST NOT 生成 stale response, 例如被 "no-store" 或 "no-cache" cache directive, "must-revalidate" cache-response-directive, 或适用的 "s-maxage" 或 "proxy-revalidate" cache-response-directive 禁止; 见 Section 5.2.2.

除非 cache 已 disconnected, 即无法联系 origin server 或以其他方式找到 forward path, 或者明确允许这样做, 例如通过 max-stale request directive (见 Section 5.2.1), 否则 cache MUST NOT 发送 stale response.

cache SHOULD 在 stale response 中生成带有 110 warn-code 的 Warning header field (见 Section 5.5.1). 同样, 如果 cache 已 disconnected, cache SHOULD 在 stale response 中生成 112 warn-code (见 Section 5.5.3).

当转发没有 Age header field 的 response 时, 即使该 response 已经 stale, cache SHOULD NOT 生成新的 Warning header field. cache 不需要 validation 仅仅是在传输过程中变 stale 的 response.