gpt4 book ai didi

http - http缓存实际上应该如何工作?

转载 作者:可可西里 更新时间:2023-11-01 17:02:36 24 4
gpt4 key购买 nike

我知道这实际上是一个古老的东西,但我真的对它有疑问,以及它在开始时应该如何工作。所以我至少得到了这四个重要的缓存 header ,我可以将它们发送回客户端(Last-ModifiedCache-ControlExpiresETag)

问题 2.2 的示例情况:

假设我有一些文章网站。可能每 15 分钟到 7 天就有一篇新文章。所以我将 Cache-ControlExpires 设置为缓存 15 分钟,以便客户端始终获得最新版本。

目前我对 ETagLast-Modified 的看法:

我只是将一些内容的哈希值放在那里,如果 If-None-Match == ETag,我可以向客户端发送 304 .

如果 If-Modified-Since >= Last-Modified,我可以向客户端发送 304

问题

  1. 我是否需要 Cache-Control Expires 来支持所有浏览器?
  2. 看起来 Cache-ControlExpires 只是告诉我的浏览器内容应该在计算机上缓存多长时间,对吗?
    • 所以我只能使用 ETagLast-Modified 来确定何时应该发送 304 对吗?
    • 所以我可以将 Cache-ControlExpires 设置为永远,如果 ETag 就向客户端发送新版本Last-Modified 改变了那个?
      • 因为这适用于我的浏览器,但它适用于所有浏览器吗?
  3. 我是否需要 ETag Last-Modified 来支持所有浏览器?
  4. Pragma 看起来像另一个类似于 Cache-Control 的缓存 header ,哪些浏览器正在使用 Pragma 我需要它吗?

最佳答案

Do I need Cache-Control and Expires to support all browsers or not ?

Cache-Control 是在 HTTP 1.1 中引入的,因此仅在支持 HTTP 1.1 的客户端上受支持。自 HTTP 1.0 起支持 Expires。如果两者都指定,则 Cache-Control 优先,Expires 将被完全忽略。


It looks like Cache-Control and Expires only tells my browser how long the content should be cached on the computer right?

基本上,是的。如果 Cache-Control 不存在(因此将使用 Expires)或使用 max-age 进行设置。


So I can only use ETag and Last-Modified to find out when I should send 304 right?

在这里,您的问题开始令人困惑。您实际上需要为此检查 If-None-MatchIf-Modified-Since header 。通常,浏览器在条件 GET 请求中只发送其中一个,如果它与服务器中 ETagLast-Modified 的当前值匹配,则返回 304 以及ETag header 。请注意,如果浏览器出于某种原因同时发送它们,那么您可以忽略 If-Modified-Since


So I could just set Cache-Control and Expires to forever and just send the client the new version if ETag or Last-Modified changed that?

没有。如果将其设置为永远,则在最终用户按下 (Ctrl)+F5 之前,客户端永远不会发送(有条件的)请求。您需要将 Cache-Control: max-ageExpires 设置为所需的缓存时间(也许是将来的 15 分钟?)。


Because this works in my browser but will this work in all browsers?

注意您的测试方式:按 F5 将始终发送有条件的 GET 请求。但是简单的页面到页面导航和表单提交(就像在现实世界中一样)不会发送有条件的 GET 请求!按 Ctrl+F5 将忽略缓存并发送全新的 GET 请求。


Do I need ETag and Last-Modified to support all browsers or not ?

所有浏览器都支持。如果已从服务器接收到 ETag,并且浏览器需要执行条件 GET 请求,则浏览器需要通过 If-None-Match 发送回 ETag。 Last-Modified 对他们来说是可选的,通常只有在资源上没有 ETag 时才会发送。另见 RFC 2616 section 13.3.4 .


Pragma looks like another cache header similar to Cache-Control, which browsers are using Pragma and do I need it?

如果你想缓存,你不需要它。只有当您不想缓存时才需要它(为了覆盖 HTTP 1.0 客户端/代理,尽管其数量目前正在稳步减少)。


如果您熟悉 Java,或者至少可以破译它,那么您可能会发现这篇文章很有帮助:FileServiet supporting resume and caching and GZIP .

另见:

关于http - http缓存实际上应该如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18251975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com