1. Why do browsers cache
Think of it this way: the purpose of a browser’s cache is to make it faster the next time you use it, so the user can get to the page faster. Confusing: How do browsers determine whether or not to cache a file, and for how long
2. HTTP cache classification
According to whether to send a resource request to the server, it can be divided into mandatory cache and negotiated cache
Mandatory cache (control field) :
HTTP1.0: Expires
HTTP1.1: Cache-Control
If the Expires and cache-Control fields are cache-control
The diagram below:
Procedure: The second request is made —-> The browser determines whether the target resource matches the “mandatory Cache” —-> If the target resource matches the “mandatory Cache” —-> The browser directly obtains the resource from the Cache and does not request the resource from the server
In HTTP/1.1, the main values of cache-control are:
Public: All content will be cached (both client and proxy can be cached)
Private: All content can be cached only by the client. The default value of cache-control
No-cache: indicates the contents of the client cache, but whether to use the cache is verified by the negotiated cache
No-store: All content is not cached, that is, neither mandatory cache nor negotiated cache is used
Max-age = XXX (XXX is numeric) : The cache contents will expire after XXX seconds
Must-revalidate: Forces the browser to adhere to the cache rules you set
Proxy-revalidate: forces the proxy to adhere to the cache rules you set
Negotiation cache:
The browser sends a request to the server with the cache id. The server decides whether to use the cache based on the cache ID. There are two situations: 1. Negotiation cache failure returns 200 and request result.
Negotiate cached control fields
HTTP1.1: Last-Modified Returns the time when the resource file was last modified on the server when the server requests it
To summarize, see the logic for mandatory and negotiated caching