当前位置: 代码迷 >> Web前端 >> What's the difference between Cache-Control: max-age=零 and no-cache
  详细解决方案

What's the difference between Cache-Control: max-age=零 and no-cache

热度:1052   发布时间:2013-12-15 22:17:18.0
What's the difference between Cache-Control: max-age=0 and no-cache?

Below from?http://stackoverflow.com/questions/1046966/whats-the-difference-between-cache-control-max-age-0-and-no-cache/1046987

?

?

?

When sent by the origin server

I believe?max-age=0?simply tells caches (and user agents) the response is stale from the get-go and so theySHOULD?revalidate the response (eg. with the?If-Not-Modified?header) before using a cached copy, whereas,?no-cache?tells them they?MUST?revalidate before using a cached copy. From?14.9.1 What is Cacheable:

no-cache

...a cache MUST NOT use the response to satisfy a subsequent request without successful revalidation with the origin server. This allows an origin server to prevent caching even by caches that have been configured to return stale responses to client requests.

In other words, caches may sometimes choose to use a stale response (although I believe they have to then add a?Warning?header), but?no-cache?says they're not allowed to use a stale response no matter what. Maybe you'd want the?SHOULD-revalidate behavior when baseball stats are generated in a page, but you'd want the?MUST-revalidate behavior when you've generated the response to an e-commerce purchase.

Although you're correct in your comment when you say?no-cache?is not supposed to prevent storage, it might actually be another difference when using?no-cache. I came across a page,?Cache Control Directives Demystified, that says (I can't vouch for its correctness):

In practice, IE and Firefox have started treating the no-cache directive as if it instructs the browser not to even cache the page. We started observing this behavior about a year ago. We suspect that this change was prompted by the widespread (and incorrect) use of this directive to prevent caching.

...

Notice that of late, "cache-control: no-cache" has also started behaving like the "no-store" directive.

As an aside, it appears to me that?Cache-Control: max-age=0, must-revalidate?should basically mean the same thing as?Cache-Control: no-cache. So maybe that's a way to get the?MUST-revalidate behavior ofno-cache, while avoiding the apparent migration of?no-cache?to doing the same thing as?no-store?(ie. no caching whatsoever)?


When sent by the user agent

I believe?shahkalpesh's answer?applies to the user agent side. You can also look at?13.2.6 Disambiguating Multiple Responses.

If a user agent sends a request with?Cache-Control: max-age=0?(aka. "end-to-end revalidation"), then each cache along the way will revalidate its cache entry (eg. with the?If-Not-Modified?header) all the way to the origin server. If the reply is then 304 (Not Modified), the cached entity can be used.

On the other hand, sending a request with?Cache-Control: no-cache?(aka. "end-to-end reload") doesn't revalidate and the server?MUST NOT?use a cached copy when responding.

  相关解决方案