3

关于缓存失效,HTTP 规范说:

某些 HTTP 方法必须导致缓存使实体无效。这是 Request-URI 或 Location 或 Content-Location 标头(如果存在)引用的实体。

我试图通过使用Location标头使缓存中的条目无效,但它似乎不起作用。这是我的用例:

  1. 15:13:23.9988 | GET | folders/folder.34/contents - 200 (OK)
  2. 15:13:24.1318 | PUT | folders/folder.34/contents/test.docx - 201 (Created)
  3. 15:13:24.1548 | GET | folders/folder.34/contents - 200 (OK) (cached)

(2) 的响应包含一个Location标头,其中包含请求 (1) 和 (3) 中使用的 URI。我相信这应该使folder/folder.34/contents的缓存条目无效,但是根据HttpWebResponse.IsFromCache属性,(3)中的响应似乎来自缓存。

我在Location标头中尝试了多种 URI 格式,包括:

  • Location: ../../../folders/folder.34/contents (以及其他各种“../”计数)
  • Location: folders/folder.34/contents
  • Location: /folders/folder.34/contents
  • Location: http://myhostname/folders/folder.34/contents

但仍然(3)似乎总是来自缓存。我在这里做错了什么?

4

1 回答 1

4

HTTPBis 更加清晰:

https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-p6-cache-22#section-6

Because unsafe request methods (Section 4.2.1 of [Part2]) such as
PUT, POST or DELETE have the potential for changing state on the
origin server, intervening caches can use them to keep their contents
up-to-date.

A cache MUST invalidate the effective Request URI (Section 5.5 of
[Part1]) as well as the URI(s) in the Location and Content-Location
response header fields (if present) when a non-error response to a
request with an unsafe method is received.

因此,如果这不是您看到的行为,我的假设只是您使用的特定 HTTP 客户端没有正确的行为。

我特别期待:

Location: /folders/folder.34/contents

要有正确的行为。

于 2013-04-12T18:44:13.630 回答