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

Time:12-23

Quote from 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

If Age isn't used when using Cache-Control: max-age, can browsers know when the cache is outdated? If they can, how?

CodePudding user response:

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

Yes, the Age header is used, if available:

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.

A response delay is also added to it:

response_delay = response_time - request_time;
corrected_age_value = age_value   response_delay;

If no Age header is present, a cache may approximate it with:

apparent_age = max(0, response_time - date_value);

The cache then uses the largest of the two:

corrected_initial_age = max(apparent_age, corrected_age_value);
  • Related