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 :

Cache validation

Negotiation cache is cache verification .

trigger :

  • 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 .

The trigger condition :

Only when the server returns a strong verifier or a weak verifier will authentication be performed .

Conditional request

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》

Negotiate the cache characteristic :

  • Server-side validation
  • 304 Not Modified Status code
  • Last-Modified
  • Etag

304(Not Modified)

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 .

His trigger condition is :

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

Last-Modified ( Last modified date and time of resource )

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 :

If-Modified-Since ( Compare the update time of resources )

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

Etag ( Matching information of resources )

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 :

etag: f7b80870fbcd8f9da18ab22d2ef1932c

characteristic :

  • 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 .

Strong and weak Etag:

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 .

ETag: "usagi-1234"

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 :

ETag: W/"usagi-1234" 

If-None-Match ( Compare entity tags )

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 :

if-none-match: f7b80870fbcd8f9da18ab22d2ef1932c

He and If-Match The opposite is true .

If-Match And 412 Status code

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 .

if-match: f7b80870fbcd8f9da18ab22d2ef1932c


if-match: *


First ,Etag Has a higher priority than Last-Modified.

Last-Modified and Etag The advantages and disadvantages of are analyzed as follows :

Last-Modified advantage

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

Last-Modified shortcoming

  1. 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 ;
  2. 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 ;
  3. 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

Etag advantage

  • 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 .

Etag shortcoming

  • 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 )

1、Last-Modified The overall process of the group is as follows :

  • 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

2、Etag The overall process of the group is as follows :

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 .

HTTP cache —— Negotiate the cache ( Cache validation ) More articles about

  1. http Negotiate the cache VS Strong cache

    Previously, only a general description of browser caching can be given , Deep principles cannot be described : Finally in the front of the two interview process was asked down , For the sake of revenge , After consulting some materials, I finally have a deeper understanding of it , I don't say much nonsense , Let's take a look at the browser cache , Yes ...

  2. Browser cache _HTTP Strong caching and negotiation caching

    Browser cache Browser cache is a way for the browser to store the recently requested documents on the local disk , When visitors visit the same page again , The browser can load the document directly from the local disk . So according to the above characteristics , Browser caching has the following advantages : Reduce redundant data transmission ...

  3. web Cache of --http Caching mechanisms

    One .web Cache can be divided into database cache . Proxy cache . Browser cache . The browser cache contains a lot of content :http cache .indexDb.cookie.localStorage etc. . This film only discusses http Cache related content . ...

  4. http Caching and offline caching

    One .http The protocol implements caching 1. Cache header Universal cache . Conditional cache . Cache control falls into three categories Head name explain request / Respond to General cache header Controls whether the client sends a request to the server or the server responds to the request   cache-contro ...

  5. 【HTTP cache 】 Browser cache theory

    Time :2016-12-12 17:51:30 author : zhongxia zero . Preface What is mainly written here is the theory , There are few specific practices , Write a practical tutorial later , The content is basically copied from the reference articles [ After reading the article , By the way, take notes ...

  6. 005- Optimize web Request a -gzip Compress 、http Cache control and cache verification [Pragma、Expires、Cache-Control、max-age、Last-Modified、 User refresh access 、 Avoid over 304]

    Optimize Web Typical technology applied : Cache control header information .Gzip. Application cache .ETag. Reactive Technology [ Asynchronous method calls and WebSocket] One . Template cache spring.thymeleaf.cache=true sprin ...

  7. cache (CDN cache , browser ( client ) cache )

    1. What is caching ? Caching is a data structure , The results of operations used to quickly find and execute . therefore , If an operation is slow , For common input data, you can cache the results of the operation , And use the cached data the next time the operation is called . Cache is a memory that is stored everywhere ...

  8. host cache , Browser cache --- solve host The damage caused by cache

    1. cache cache , It's too familiar for Engineers , It's so convenient , Omit the acquisition method of resources or data , Cache directly to the place where users can access the fastest , It also reduces the pressure on the server , such as : (1) Static file acquisition The server ->cdn-> Local magnetism ...

  9. Web Front and back end caching technology ( What is the main function of cache )

    Web Front and back end caching technology Web Front and back end caching technology ( What is the main function of cache ) One . summary One sentence summary : Speed up the page opening Reduce network bandwidth consumption Reduce server stress 1. stay Web Application , Where to apply cache ? There are mainly browsers ...

  10. 100 million level traffic client cache Http Cache vs. local cache

    The client cache is divided into Http Caching and local caching , There are many benefits to using caching , For example, reducing duplicate transmission of the same data , Save network bandwidth resources and ease network bottlenecks , Reduced the requirements for the original server , Avoid overloading , So the server can respond to other requests faster Http cache ...

Random recommendation

  1. Use delphi+intraweb Wechat development 1~4 Code example

    The previous lectures focused on the use of iw Technical difficulties and solutions of wechat development , It's all code snippets ( Wechat message encryption and decryption is the complete code ), In fact, I always feel that implementation according to the tutorial is the most important way to master a technology ! But for those who have just come into contact with this kind of development ...

  2. jquery practice ( Assign multiple attribute values at once )

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  3. Java character string split Function considerations

    Java A string of split Method to split a string , But unlike other languages ,split The argument to the method is not a single character , It's regular expressions , If you enter a vertical line (|) Such characters are used as split strings , There will be unexpected results , Such as , Str ...

  4. Container adapter priority_queue

    template <class T, class Container = vector<T>,                class Compare = less<type ...

  5. css reset And part of layout style

    /*! normalize.css v3.0.3 | MIT License | */html { font-family: sans ...

  6. lintcode: Search for a two-dimensional matrix II

    subject Search for a two-dimensional matrix II Write an efficient algorithm to search m×n Values in a matrix , Returns the number of occurrences of this value . This matrix has the following properties : The integers in each row are sorted from left to right . The integers in each column are sorted from top to bottom . In each row or column there is no ...

  7. Student achievement management C Language version

    [ title ] Design and implementation of student achievement management [ development language ]C Language [ Outline design ] Use the structure to store the student's student number . Name and grade information , Realize the basic operation of students' grades : increase . Delete . Inquire about . Sort [ Test data ] Press the prompt to enter 5 The correct number of groups ...

  8. hadoop from rookie to ninja - 1. Basic Architecture( Infrastructure )

    1. Daemons( Daemon ) New and old architecture old : Apache Hadoop 1.x (MRv1)   new : Apache Hadoop 2.x (YARN)-Yet Another Resour ...

  9. S1 Commodity information management system

    #include <iostream> #include <cstdio> #include <cstdlib> #include <iomanip> ...

  10. Enrich your design 10 individual CSS3 Effect Library

    Magic CSS3 Animations Magic CSS3 Animations It's a CSS3 Animation package , With some special effects you can Web Free to use in the project . Having something like glittering , angle , rotate , Bombs and other special effects . Using Jane ...