页面是否会在同一秒中断 If-Modified-Since 中修改两次?

Would a page modified twice within the same second break If-Modified-Since?

根据我对缓存机制的理解,响应头Last-Modified、请求头If-Modified-Since等都精确到秒,即If-Modified-Since: Wed, 21 Oct 2015 07:28:00 GMT,因此亚秒级修改会破坏失效:

12:00:00.100 /path/to/resource updated to Version 1

12:00:00.200 GET /path/to/resource from client A
12:00:00.300 Response: Version 1 of the page with Last-Modified: 12:00:00

12:00:00.400 /path/to/resource updated to Version 2

12:00:00.500 GET /path/to/resource from client A with If-Modified-Since: 12:00:00
12:00:00.600 Response: 304 Not Modified

# and even after time passes
16:15:00.000 GET /path/to/resource from client A with If-Modified-Since: 12:00:00
16:15:00.100 Response: 304 Not Modified

并且在缓存过期之前,客户端永远不会获得页面的版本 2。

真的是这样吗?页面中存储的版本是否应始终将页面的最后修改日期增加一秒?

是的,Last-Modified 的 one-second 分辨率意味着如果资源在不到一秒内更改,带有 If-Modified-Since 的验证请求可以 return 不适当的值。你的例子是正确的。

规范承认了这一点,并给出了 rules 何时 Last-Modified header 可以被认为是强验证器或弱验证器。您可以在规范中阅读更多关于该区别的信息,但本质上它明确表示验证可能会失败(弱),除非客户端或服务器可以确定它不会(通过比较 Date 和 [=例如 10=] headers).

不过,解决办法不是在 Last-Modified 时间上撒谎,而是使用 ETag 代替。它不受此 sub-second 分辨率问题的影响,在这种情况下 explicitly recommended 作为替代方案:

An entity-tag can be more reliable for validation than a modification date in situations where it is inconvenient to store modification dates, where the one-second resolution of HTTP date values is not sufficient, or where modification dates are not consistently maintained.