Mandatory caching and negotiation caching are available on the Internet when I was a senior, but now I don’t know what the difference is, so I just rearrange it.
The most intuitive difference between a mandatory cache and a negotiated cache is that a mandatory cache does not send a request to the server, whereas a negotiated cache sends a request to the server to determine whether or not the cache is hit.
Force the cache to read resources directly from the client cache without asking the server.
Negotiation cache needs to communicate with the server to determine whether to use cache resources, negotiation.
The two caches also differ in the status code after hitting. The mandatory cache is 200, and the negotiated cache is 304
The cache | Form of resource acquisition | Status code | Send the request to the server |
---|---|---|---|
Mandatory cache | From the slow access | 200 (from cache) | No, cache access directly |
Negotiate the cache | From the slow access | 304 (not modified) | If yes, the server tells the browser whether the cache is available |
How does it work? What parameters are needed in the request header?
Mandatory cache
You need to set max-age in the cache-control field. The value of max-age indicates the validity period of the resource. For example, if max-age=300, the validity period is 5 minutes.
Cache-control also has four common arguments:
- Public: indicates that resources can be cached by terminal users and intermediate proxy servers such as CDN
- Private: Resources cannot be cached by intermediate proxy servers, but only by end users
- No-cache: the mandatory cache is skipped and the negotiated cache is used. Normally, a negotiated cache is invoked only if the cache is forced to miss.
- No-store: no cache is required.
Negotiate the cache
Negotiation cache does not have to set the expiration time manually, but only has several parameters to determine whether the cache is expired.
Last-modify: This parameter is added to the header returned by the server when the resource is first requested. Last-modify is the time when the resource was Last modified.
If-modify-since: If the client requests it again, if-modify-since will be added. The value of if-modify-since is the last-modify value returned by the server. The server will compare the last modification time to determine whether the cache hit.
Etag: Unique identifier of a resource on the server. It is also used to determine whether the resource has changed. As for why Etag is used when there is last-modify, it is because last-modify can only be accurate to the second, and Etag is needed to accurately judge multiple modifications within a second.
Etag is more accurate than last-modify, and last-modify performance is better than Etag. Etag takes precedence in judgment priority.
In addition to the parameter to determine whether the cache is hit, there is also a parameter to set if-none-match to enforce the negotiation cache If the cache is missed
** if-none-match: **If the resource expires (using cache-control max-age) and the resource is found to have an Etage declaration, the web server requests the resource again with if-none-match (Etag value). After receiving the request, the Web server sends if-none-match header and compares it with the corresponding checksum string of the requested resource to determine whether the negotiated cache is matched.