当前位置:网站首页>Talk about browser cache control

Talk about browser cache control

2022-04-23 16:42:00 summer·

Cache is a very important concept in computer field , Today, we mainly talk about the caching strategy in browsers

First, let's talk about the process when caching is needed :

Client initiated HTTP request , Server response
The response content contains the required data and cache rules ( Mark ) And the browser will store the data and cache rules in the browser cache
The client initiates the previous request again , You will ask the browser cache first with the cache rules , Whether the cache is out of date
If there is no expiration, the cached content will be returned directly , If it expires, send a request to the server again

The above process is actually divided into two methods : Strong caching and negotiation caching

Force memory and negotiation cache

` Every time the browser makes a request , Both look in the browser cache for the result of the request and the cache identity `

` Each time the browser gets the result of a returned request, it stores the result and the cache id in the browser cache `

Mandatory cache : Search the browser cache for the result of the request , And according to the caching rules of the result to decide whether to use the process of caching the result

There are three types of strong caching :

  1. There is no such cache result and cache identity , Force cache invalidation , Then send a request directly to the server ( Consistent with the first request )
  2. There is the cache result and the cache identity , But the result is no longer valid , Force cache invalidation , Then use negotiation cache ( Don't analyze )
  3. There is the cache result and the cache identity , And this result has not been invalidated , Force cache to take effect , Return the result directly

Cache rules that enforce caching :

When the browser makes a request to the server , The server will put the caching rules into HTTP In response to a message HTTP The header is returned to the browser along with the request result , The fields that control the forced cache are Expires and Cache-Control, among Cache-Control Priority ratio Expires high .

Expires yes HTTP/1.0 Control the fields in the Web Cache , Its value is the expiration time when the server returns the result cache of the request , When the request is initiated again , If the client time is less than Expires The value of , Use cached results directly

If the time between the client and the server is for some reason ( For example, different time zones ; Client and server have one side's time is not accurate ) There is an error , Then the forced cache will fail directly , In this way, the existence of forced cache is meaningless

Negotiate the cache : After the forced cache expires , The browser makes a request to the server with the cache id , The process by which the server determines whether to use the cache based on the cache identity

The difference from strong cache is that whether to use cache is decided by the server , Strong caching only cares about whether the cache in the cache rule expires

summary :
Force the cache to take precedence over the negotiation cache , If cache is forced (Expires and Cache-Control) Take effect and use the cache directly , If it does not take effect, a negotiation cache is performed (Last-Modified / If-Modified-Since and Etag / If-None-Match), The negotiation cache is up to the server to decide whether to use the cache , If the negotiation cache expires , Then represents the cache invalidation of the request , Retrieve request results , This is then stored in the browser cache ; Return in effect 304, Continue using the cache

版权声明
本文为[summer·]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231638221028.html