使用 `Cache-Control: max-age` 时是否必须使用 `Age`?

Does `Age` have to be used when using `Cache-Control: max-age`?

引自Cache-Control

max-age

The max-age=N response directive indicates that the response remains fresh until N seconds after the response is generated.

Cache-Control: max-age=604800

Indicates that caches can store this response and reuse it for subsequent requests while it's fresh.

Note that max-age is not the elapsed time since the response was received, but instead the elapsed time since the response was generated on the origin server. So if the other cache(s) on the path the response takes store it for 100 seconds (indicated using the Age response header field), the browser cache would deduct 100 seconds from its freshness lifetime. [emphasis added]

Cache-Control: max-age=604800
Age: 100

如果在使用Cache-Control: max-age时不使用Age,浏览器是否可以知道缓存何时过时?如果可以,怎么做?

RFC 7234 explains how a cache can estimate the age of a response message.

是的,使用 Age header,如果可用:

The term "age_value" denotes the value of the Age header field (Section 5.1), in a form appropriate for arithmetic operation; or 0, if not available.

还加了响应延迟:

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

如果不存在 Age header,缓存可能会近似为:

apparent_age = max(0, response_time - date_value);

然后缓存使用两者中最大的一个:

corrected_initial_age = max(apparent_age, corrected_age_value);