[HTTP] The difference between no-cache and no-store

I have been studying the book "Illustrated HTTP" recently. In the introduction to the common header fields of HTTP, there are several places where the explanation is not very clear. Here is a brief summary.
Recommended link to Yuque to read my article. The format is more friendly than CSDN and creation is easier. Yuque - Common header fields - The difference between no-cache and no-store

no-cache directive:
Insert image description here

Cache-Control: no-cache

no-store directive:
Insert image description here

Cache-Control: no-store

no-cache
● The purpose of using the no-cache directive is to prevent expired resources from being returned from the cache. The "no-cache" directive tells browsers and proxy servers not to cache response data. This means that the cache will store the response data in the cache, but that response must be submitted to the server for validation before it is returned to the client. The server checks the validity of the response and if it finds that the response is still valid, the server returns a new validation token (ETag) to notify the cache that it can use it, otherwise the server returns new response data. This approach is called "validation caching".

no-store
● The “no-store” directive is more strict and requires browsers and proxy servers not to cache response data under any circumstances. This means that every request requires re-fetching the data from the origin server, without any caching. This can be used for sensitive data or in situations where response data changes frequently to ensure that sensitive information or out-of-date data is not stored on the client or proxy server.


To summarize:
the "no-cache" directive asks the cache to cache the response data, but needs to authenticate with the origin server before using the cached data.
The "no-store" directive requires that the response data not be cached in any form and that the data needs to be re-fetched from the origin server for each request.
Choosing which directive to use depends on your application scenario and security requirements. If you need to reduce the number of network requests while ensuring data validity, you can use the "no-cache" directive. If you need to ensure that response data is never cached, you can use the "no-store" directive.


But there is this passage in the book:

如果服务器返回的响应中包含no-cache指令,那么缓存服务器不能对资源进行缓存。源服务器以后也
将不再对缓存服务器请求中提出的资源有效性进行确认,且禁止其对响应资源进行缓存操作。

Cache-Control: no-cache=Location

由服务器返回的响应中,若报文首部字段Cache-Control中对no-cache字段名名具体指定参数值,
那么客户端在接收到这个被指定参数值的首部字段对应的响应报文后,就不能使用缓存。

Obviously, this passage conflicts with the explanation of "validate cache", but in fact this passage is explaining the "no-cache" directive in the Cache-Control header field, and mentions the parameter values ​​under specific circumstances.

First of all, the Cache-Control header field is used to control the behavior of HTTP caching, where "no-cache" is a commonly used directive to indicate that the cache should not use stored response data, but should verify with the server on each request Response effectiveness.
The "Cache-Control: no-cache=Location" mentioned in this paragraph is a specific case, which specifies that the parameter value of the "no-cache" directive is "Location". This means that after receiving a response with a "Cache-Control" header field with this parameter value, the client MUST NOT use the cache to store the response.

Normally, the "no-cache" directive requires no argument value; its presence is enough to tell the cache not to use the stored response data. However, in some special cases, the server may wish to have more specific control over the "no-cache" directive to specify which parts of the response cannot be cached. In this case, the server can append a parameter value after "no-cache", e.g. "no-cache=Location", to specify which specific header field (here "Location") should not be cached.

Overall, "Cache-Control: no-cache" means that response data from the cache should not be used, but if the server wants to be more specific about which parts cannot be cached, it can add parameter values ​​to further restrict the caching behavior. In this example, the server specifies that responses with a "Location" header field should not be cached.

ps: The response in the "Location" header field specified here should not be cached, which means that the entire response data should not be cached. When the server returns a response with "Cache-Control: no-cache=Location", this means that the entire response data, including all response headers (including the "Location" header field) and the response body, should not be cached.


In fact, the behavior of the "no-cache" directive may have different meanings in different contexts, depending on how it is used and what other headers are associated with it.
In some cases, the "no-cache" directive does cause the cache server to store the response data, but the response must be submitted to the origin server for validation before returning it to the client. This is called "validation caching".
However, in other cases, especially when "no-cache" carries some parameter values ​​(such as "no-cache=Location" mentioned in the book), it can be more specific to control which parts should not be cached . In this case, the server may ask the cache to not cache certain parts of the response, such as specific response header fields (such as "Location").
So, to understand the exact behavior of "no-cache", you need to look at its context and other headers associated with it. Typically it indicates that the cache needs to submit the response to the server for validation, but in some special cases it may be more specific to indicate that certain parts should not be cached.


Guess you like

Origin blog.csdn.net/Your_Boy_Tt/article/details/133501661