This is the ninth day of my participation in the August More text Challenge. For details, see: August More Text Challenge
Recently, I have been doing a series of summary questions on the front end, interested friends can add attention, welcome correction, communication.
Strive for each knowledge point to be able to sum up some more, at least to do in the interview, for each knowledge point can be kan, not dumb fire.
preface
There are two types of HTTP caches:
- Strong cache
- Negotiate the cache
In the last article, we looked at HTTP strong caching, and today we’ll look at negotiating caching.
Negotiate the cache
The characteristics of
Negotiation cache, also known as comparison cache.
Under the negotiated cache mechanism, the browser needs to send the cache id to the server to verify whether the cache ID is valid, and then determine whether to resend the request, download the complete response, or obtain cached resources from the local.
If the server prompts that the cached resource is Not Modified, the resource is redirected to the browser cache. In this case, the network request corresponds to a status code of 304, such as:
The rules
The overall rules for the negotiated cache are as follows:
As you can see from the figure above, although the client still makes an HTTP request to the server, the server only makes a flag comparison to confirm that the cache is used. If the cache is used, no specific resource is returned. This does not reduce the number of requests, but greatly reduces the request load, which can significantly improve the request speed and reduce the network bandwidth.
The figure above shows the timing diagram of the normal and valid cache identifier, but there are actually two cases of the verification result of the negotiated cache:
- Identify effective
- Id expired
The negotiation Cache needs to be used with the strong Cache. To use the negotiation Cache, you need to set cache-control: no-cache or pragma: no-cache to tell the browser not to strengthen the Cache.
Id expired
attribute
For comparison caches, cache id passing is important to understand. It passes between the request header and the response header. There are two types of id passing:
- Last-Modified / If-Modified-Since
- Etag / If-None-Match
Last-Modified / If-Modified-Since
Last-modified/if-modified-since was introduced in HTTP 1.0, and its value represents the Last time the file was Modified.
For the first request, the server puts the last-modified time of the resource in the last-Modified response header. For the second request, the last-modified time of the Last response header is put in the if-Modified-since request header property. The server compares the last time the file was Modified to the if-modified-since value, returns 304, and loads the browser cache.
Etag / If-None-Match
The value of ETag/ if-none-match is a string of hash codes representing the identifier of a resource. When the server file changes, its hash code changes accordingly. If -none-match in the request header is compared to the hash value of the current file. If they are equal, the negotiation cache is matched.
If the hash code is a string beginning with “W/”, the negotiated cache verification is weak. Resources are requested only when the difference of the files on the server (determined by the calculation method of ETag) reaches the point where the hash value suffix can be changed. Otherwise 304 is returned and the browser cache is loaded.
conclusion
That’s all for HTTP negotiation caching. (The computer is out of power, without the charger, wait for tomorrow to charge up again perfect, sorry!!)
~
Thanks for reading!
~
Learn interesting knowledge, make interesting friends, and create interesting souls!
Hello everyone, I am the author of “programming Samadhi” Hermit King, my public number is “programming Samadhi”, welcome to pay attention, I hope you can give me more advice!
You come, with expectations, I have the ink to greet! You return, regardless of gain and loss, only with aftertaste!
Both knowledge and skill, internal force and external work, both theory and practice should grasp, both hands should be hard!