'cache-control:public'实际上有什么作用吗? [英] Does 'cache-control: public' actually have any effect?

查看:258
本文介绍了'cache-control:public'实际上有什么作用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与已知的缓存相比, cache-control:max-age = 60 是否与public缓存控制不同?

Is cache-control: public, max-age=60 handled any differently by any known caches than cache-control: max-age=60?

我一直在努力验证它,但是我假设如果响应中存在任何 cache-control 指令,则假定该响应可由浏览器和任何中间高速缓存除非 cache-control:private 已设置.

I've struggled to verify it, but I assume that if any cache-control instructions exist on a response, then it is assumed that that response is cacheable by the browser and any intermediate caches unless cache-control: private is set.

这是否意味着 cache-control:public 是多余的?反正这不是你的行为吗?

Does this mean that cache-control: public is redundant? Isn't this the behaviour you'd get anyway?

推荐答案

在更仔细地阅读MDN时,我认为我已经找到了自己问题的答案.

On more careful reading of MDN, I think I've found the answer to my own question.

TL; DR: cache-control:public 将显式覆盖默认的规则,这些响应的默认规则被认为是可缓存的,因此不应轻易使用.通常不应该缓存许多响应-例如 POST 302 重定向.有关完整的规则,请参见下文.

TL;DR: cache-control: public will explicitly override the default rules for which sort of responses are considered cacheable, so shouldn't be used lightly. Many responses normally shouldn't be cached - e.g. POSTs or 302 redirects. See below for the full set of rules.

来自缓存控制页面:

公共响应可以由任何缓存存储,即使响应通常是不可缓存的 (强调我的).

那么可缓存"是什么?吝啬的?来自可缓存"MDN词汇表上的页面:

So what does "cacheable" mean? From the "cacheable" page on the MDN glossary:

可缓存的响应是可以缓存的HTTP响应,该HTTP响应被存储以供以后检索和以后使用,从而将新请求保存到服务器.并非所有HTTP响应都可以缓存,以下是要缓存HTTP响应的以下约束:

A cacheable response is an HTTP response that can be cached, that is stored to be retrieved and used later, saving a new request to the server. Not all HTTP responses can be cached, these are the following constraints for an HTTP response to be cached:

  • 请求中使用的方法本身是可缓存的,即GET或HEAD方法.如果指示了新鲜度并且设置了Content-Location标头,则也可以缓存对POST或PATCH请求的响应,但这很少实现.(例如,Firefox不支持 https://bugzilla.mozilla.org/show_bug.cgi?id=109553.)其他方法(例如PUT或DELETE)不可缓存,其结果也无法缓存.
  • 应用程序缓存知道响应的状态码,并且将其视为可缓存的.以下状态代码是可缓存的:200、203、204、206、300、301、404、405、410、414和501.
  • 响应中有(我认为这应该是不是)标头,例如Cache-Control,可以防止缓存.
  • The method used in the request is itself cacheable, that is either a GET or a HEAD method. A response to a POST or PATCH request can also be cached if freshness is indicated and the Content-Location header is set, but this is rarely implemented. (For example, Firefox does not support it per https://bugzilla.mozilla.org/show_bug.cgi?id=109553.) Other methods, like PUT or DELETE are not cacheable and their result cannot be cached.
  • The status code of the response is known by the application caching, and it is considered cacheable. The following status code are cacheable: 200, 203, 204, 206, 300, 301, 404, 405, 410, 414, and 501.
  • There are (I assume this should be aren't) specific headers in the response, like Cache-Control, that prevents caching.

因此,当他们明确希望覆盖这些规则以提高可缓存性时,似乎应该只使用 cache-control:public ,这通常不是一个好主意.

So it looks like one should only use cache-control: public when they explicitly want to override these rules for cacheability, which in general is probably not a good idea.

这篇关于'cache-control:public'实际上有什么作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆