Etag:弱与强的例子

Etag: weak vs strong example

我一直在阅读有关 Etag 的信息,我知道有两种生成 Etag 的方法,弱的和强的。弱 Etags 在计算上比强 Etags 更容易生成。我还了解到,对于大多数用例来说,弱 Etags 实际上已经足够了。

来自 MDN

Weak validators are easy to generate but are far less useful for comparisons. Strong validators are ideal for comparisons but can be very difficult to generate efficiently.

另一个片段:

Weak Etag values of two representations of the same resources might be semantically equivalent, but not byte-for-byte identical.

我发现很难理解资源在语义上相似但不是逐字节相同是什么意思?很高兴看到一些例子。

编辑:找到一个示例 here,但我不明白:

Weak Validation: The two resource representations are semantically equivalent, e.g. some of the content differences are not important from the business logic perspective e.g. current date displayed on the page might not be important for updating the entire resource for it.

是否像在生成 Etag 时一样,您可以决定内容的更改对于功能而言并不重要(例如 css 属性 字体大小的更改)并响应304 ?如果是,那么资源在浏览器上什么时候更新,我猜只要Etag相同,浏览器就不会获取最新版本。在这种情况下,这可能意味着当发生重大更改并创建新的 Etag 时,css 属性 更改只会随主要更改一起发送到浏览器。

我的建议是查看规范,RFC 7232, section 2.1。它只有几页长,可能会回答你所有的问题。

您要求提供示例,以下是规范中的一些示例:

  • For example, the representation of a weather report that changes in content every second, based on dynamic measurements, might be grouped into sets of equivalent representations (from the origin server's perspective) with the same weak validator in order to allow cached representations to be valid for a reasonable period of time.

  • A representation's modification time, if defined with only one-second resolution, might be a weak validator if it is possible for the representation to be modified twice during a single second and retrieved between those modifications.

  • If the origin server sends the same validator for a representation with a gzip content coding applied as it does for a representation with no content coding, then that validator is weak.

最后一个可能是弱 ETag 最常见的用途:服务器在 gzip 内容时将强 ETag 转换为弱 ETag。 ,例如

规范还解释了何时更改弱 ETag:

An origin server SHOULD change a weak entity-tag whenever it considers prior representations to be unacceptable as a substitute for the current representation.

换句话说,由您决定资源的两种表示是否是可接受的替换。如果是,您可以通过为它们提供相同的弱 ETag 来提高缓存性能。