使用 Last-Modified header 使 already-stored 响应无效

Invalidate already-stored responses with Last-Modified header

Section 4 of RFC 7234有这段:

When more than one suitable response is stored, a cache MUST use the most recent response (as determined by the Date header field). It can also forward the request with "Cache-Control: max-age=0" or "Cache-Control: no-cache" to disambiguate which response to use.

它提到使用 DateCache-Control header 使 already-stored 响应无效。

Last-Modified header 是怎么回事?我可以将它用于相同目的吗?

Last-Modified 作为验证者。这些在您引用的部分前面提到过:

When presented with a request, a cache MUST NOT reuse a stored response, unless […] the stored response is either:

  • fresh (see Section 4.2), or
  • allowed to be served stale (see Section 4.2.4), or
  • successfully validated (see Section 4.3).

Section 4.3 explains validation in detail. I think 4.3.2 您一定感兴趣:

[A] request containing an If-Modified-Since header field indicates that the client wants to validate one or more of its own stored responses by modification date. A cache recipient SHOULD generate a 304 (Not Modified) response (using the metadata of the selected stored response) if one of the following cases is true:

  1. the selected stored response has a Last-Modified field-value that is earlier than or equal to the conditional timestamp; 2. no Last-Modified field is present in the selected stored response, but it has a Date field-value that is earlier than or equal to the conditional timestamp; or, 3. neither Last-Modified nor Date is present in the selected stored response, but the cache recorded it as having been received at a time earlier than or equal to the conditional timestamp.

所以是的,这样做完全符合 RFC。由于 Last-Modified/If-Modified-Since 是早在 HTTP/1.0 就引入的相当古老的机制,您可以期望缓存符合要求。