Negotiate the cache
So-called “ negotiation ”, It can be understood as ： Both the client and the server discuss to .
The client checks that the resource has expired 、 When a strong cache hit fails , Then make a request “ inquiry ” Whether the server resource is really expired , At the same time, the request header should carry the resource 「 Last updated 」 perhaps 「 Unique entity identification 」（ Different http Coexistence problems caused by version ）.
The server checks the of the resources to be requested by the client 「 Last updated 」 perhaps 「 Unique entity identification 」：
If it's the same , Description hit negotiation cache , Only return 304;
If it's not the same , Indicates that the resource has been updated , Then return to 200、 New resources , At the same time, the response header returns 「 Resource modification time 」 the latter 「 The latest entity ID of the resource 」. meanwhile , After the client gets the new resource and its modification time and identification , Re cache .
The summary is shown in the figure below ：
Negotiation cache is cache verification .
Cache validation starts when the user clicks the refresh button .
If the cached response header contains "Cache-control: must-revalidate” The definition of , Cache validation is also triggered during browsing .
in addition , Set... In browser preferences Advanced->Cache For forced validation, caching can achieve the same effect .
Only when the server returns a strong verifier or a weak verifier will authentication be performed .
Form like If-xxx This style of request header field , Can be called conditional request .
The server is connected to After receiving the conditional request , Only when the specified condition is true , To execute the request .
In the negotiation cache , There are many such conditional requests .
《 The illustration HTTP》
304 Not Modified Status code
Although the status code is 3XX Categories , But with 301、302 Dissimilarity , Not redirection .
304,Not Modified. Indicates that the server resources have not changed , You can directly use the data cached by the client 、 Unexpired resources .
1、 Client side adoption GET Method , And the request message contains “If-Match”、“If-Modified-Since”、“If-None-Match”、“If-Range”、“If-Unmodified-Since” Etc
2、 The server receives the request , Allow requests and access to resources . But because the client's request does not meet the conditions , I'm going straight back 304.
304 When the status code returns , Does not contain the body part of any response .
in other words , If it hits the negotiation cache , When the server responds to the request , Only one... Will return 304 Status code 、 There is no actual file content , therefore The saving in response volume is the optimization point of negotiation cache
HTTP/1.0 Last-Modified Group
Entity header field ：Last-Modified, Indicates when the resource was last modified .
The format is as follows ：
last-modified: Thu, 01 Jan 1970 00:00:00 GMT
This sentence is like the server telling the client , The file you requested is 1970 year 1 month 1 Modified by .
Last-Modified It's a kind of Cache weak validator . It's weak because it's only accurate to one second .
If the response header contains this information , The client can bring... In subsequent requests If-Modified-Since To verify the cache ：
Request first field
He is with Last-Modified Corresponding field , The last cached resource update time is stored , That is, the last time the resource was cached Last-Modified Value .
Used to confirm the proxy server / Validity of local resources owned by the client .
If in If-Modified-Since Field after the specified date time , Resources have changed , The server will accept the request .
Above picture , The server takes its value and the value of the local requested resource on the server Last-Modified Compare ：
If Last-Modified <= If-Modified-Since, Explain that the resources are consistent , Hit negotiation cache , return 304 Status code Not Modified that will do .
If Last-Modified > If-Modified-Since, Indicates that the resource has been modified , You need to return the latest resources to the client .
His format is like ：
if-modified-since: Thu, 01 Jan 1970 00:00:00 GMT
HTTP/1.1 Etag Group
Response first field , One kind of cache Strong calibrator .
Entity tag （Etag） Is a specific value associated with a specific resource , yes Resource unique identifier String . The server allocates the corresponding ETag value . And inform the client of the entity identification of the resource through the header field of the response header .
The format is as follows ：
When resources are updated ,ETag Values also need to be updated .
and , Generate ETag When the value of , and There is no uniform algorithm rule , It's just the server that allocates . So distributed server system , As like as two peas Etag The values may be different .
Besides , Because it is a unique identifier generated according to different contents , Resources in Chinese and English , Although the address is the same , Its Etag Different .
ETag There's a strong side to it ETag Value and weak ETag It's worth it .
strong ETag value
strong ETag value , No matter how subtle the entity changes, it will change its value .
weak ETag value
weak ETag Values are only used to indicate whether the resources are the same . Only resources have changed fundamentally , production It will change when there are differences ETag value .
At this time , Will append... At the beginning of the field value “W/”. as follows ：
Request first field
He is with Etag Corresponding field , Stored is the entity tag value of the last cached resource , That is, the last time the resource was cached Etag Value .
When negotiating cache , The client carries this field with the name of the server resource Etag Compare field values , Only in If-None-Match The field value of and Etag Value does not match 、 When not in agreement , Hit negotiation cache .
stay GET or HEAD Request method in , Use If-None-Match Access to the latest resources .
The format is as follows ：
He and If-Match The opposite is true .
The usage and rules are basically the same If-None-Match, But the judgment logic is completely opposite .
If-Match The judgment logic of this condition is ： Only when If-Match The value of the field follows ETag value Match consistent Will hit the negotiation cache . The server will accept the request And back to 200 And new data .
conversely , The server returns the status code 412 Precondition Failed Response .
You can also use asterisk (*) Appoint If-Match The field values of the .
In this case , The server will ignore ETag Value , Process requests as long as resources exist .
First ,Etag Has a higher priority than Last-Modified.
Last-Modified and Etag The advantages and disadvantages of are analyzed as follows ：
There is no version problem , Every request will go to the server for verification . The server compares the last modified time , If the same returns 304, Different words return 200 And the corresponding data resources
As long as the resource is modified , No matter whether the content has changed substantially , Will return the resource to the client . For example, periodic rewriting , In this case, the data contained in the resource is actually the same ;
Marked by time , Unable to recognize multiple modifications in a second . If the speed of resource update is less than seconds , Then the cache cannot be used , Because the lowest unit of time is seconds ;
Some servers Can't get the last modification time of the file accurately ; If the file is generated dynamically through the server , Then the update time of this method is always the generation time , Although the documents may not have changed , So it doesn't work as a cache
Can be more accurate Judge whether the resource has been modified ,
Can identify Multiple modifications in a second ;
There is no version problem , Each request will go to the server for verification .
Calculation Etag It's worth it Performance loss ;
Distributed servers rely on algorithms ： In the case of distributed server storage , Calculation Etag If the algorithm is not the same , It will cause the browser to get the page content from one server and verify it on another server Etag Mismatches .
The flow of the two groups of fields is summarized as follows
Finally, review the whole 、 Review for a while .
（ Be careful ： actual HTTP1.1 In the request of , Both sets of fields are contained in the request and response headers , I'm here to deepen the group CP The impression of , Explain separately ）
Server pass Last-Modified Field tells the client , When the resource was last modified
The browser records this value with the content in the cache database
The next time you request the same resource , The browser finds out from its own cache " Not sure if it's overdue " cache . So in the request header, the last Last-Modified The value of is written to the If-Modified-since Field
The server will If-Modified-since The value of is equal to If-Modified Field to compare . If equal , It means not modified , Respond to 304; conversely , Indicates the modification response 200 Status code and return data
When the browser makes a request , The server returns the unique identification of the requested resource in the response header . On the next request , Will return the last time Etag The value assigned to If-None-match And add it to the response header . The server sends the message from the browser if-no-matched With their own local resources Etag comparing , If the match , Then return to 304 Notify the browser to read the local cache , Otherwise return to 200 And updated resources .