[Turn] Thoroughly understand the mechanism and principle of HTTP caching

foreword

As an important means of web performance optimization, the Http caching mechanism should be a basic link in the knowledge base for students engaged in web development, and a must-have knowledge and skill for students who are interested in becoming front-end architects.
But for many front-end students, they just know that the browser will cache the requested static files, but it is not very clear why they are cached and how the cache takes effect.
Here, I will try to use simple and clear words to introduce the HTTP caching mechanism systematically, hoping to help you understand the front-end caching correctly.



Before introducing HTTP caching, as a foundation for knowledge, let's briefly introduce HTTP packets

HTTP packets are the data blocks sent and responded to when the browser and the server communicate.
The browser requests data from the server and sends a request (request) message; the server returns data to the browser and returns a response (response) message.
The message information is mainly divided into two parts
1. The header containing the attribute (header) ---------------------------- Additional information (cookie, cache information etc.) cache-related rule information is included in the
header the part you want to transfer



Cache rule parsing

For the convenience of everyone's understanding, we believe that the browser has a cache database for storing cached information.
When the client requests data for the first time, there is no corresponding cached data in the cache database at this time, and the server needs to be requested. After the server returns, the data is stored in the cache database.

 

 

There are various rules for HTTP caching, which are classified according to whether it is necessary to re-initiate a request to the server. I will divide them into two categories ( mandatory caching and comparison caching).
Before introducing these two rules in detail, let the sequence diagram be used first. Everyone has a simple understanding of these two rules.


When cached data already exists, it is only based on forced cache , and the process of requesting data is as follows


. When cached data already exists, it is only based on comparison cache , and the process of requesting data is as follows

Students who don’t know much about the caching mechanism may ask, based on the comparison cache process, regardless of whether the cache is used, it is necessary to send a request to the server, so why use the cache?
Let's put this question aside for the time being. When we introduce each caching rule in detail later, we will give you the answer.

We can see the difference between the two types of caching rules. If the mandatory cache is valid, it does not need to interact with the server, while the comparison cache needs to interact with the server regardless of whether it is valid or not.
Two types of caching rules can exist at the same time, and the mandatory cache has a higher priority than the comparison cache . That is, when the mandatory cache rule is executed, if the cache is valid, the cache is used directly, and the comparison cache rule is no longer executed.



Force cache

从上文我们得知,强制缓存,在缓存数据未失效的情况下,可以直接使用缓存数据,那么浏览器是如何判断缓存数据是否失效呢?
我们知道,在没有缓存数据的时候,浏览器向服务器请求数据时,服务器会将数据和缓存规则一并返回,缓存规则信息包含在响应header中。

对于强制缓存来说,响应header中会有两个字段来标明失效规则(Expires/Cache-Control
使用chrome的开发者工具,可以很明显的看到对于强制缓存生效时,网络请求的情况



Expires
  Expires的值为服务端返回的到期时间,即下一次请求时,请求时间小于服务端返回的到期时间,直接使用缓存数据。
不过Expires 是HTTP 1.0的东西,现在默认浏览器均默认使用HTTP 1.1,所以它的作用基本忽略。
另一个问题是,到期时间是由服务端生成的,但是客户端时间可能跟服务端时间有误差,这就会导致缓存命中的误差。
所以HTTP 1.1 的版本,使用Cache-Control替代。

Cache-Control
Cache-Control 是最重要的规则。常见的取值有private、public、no-cache、max-age,no-store,默认为private。
private:             客户端可以缓存
public:              客户端和代理服务器都可缓存(前端的同学,可以认为public和private是一样的)
max-age=xxx:   缓存的内容将在 xxx 秒后失效
no-cache:          需要使用对比缓存来验证缓存数据(后面介绍)
no-store:           所有内容都不会缓存,强制缓存对比缓存都不会触发(对于前端开发来说,缓存越多越好,so...基本上和它说886)

举个板栗

图中Cache-Control仅指定了max-age,所以默认为private,缓存时间为31536000秒(365天)
也就是说,在365天内再次请求这条数据,都会直接获取缓存数据库中的数据,直接使用。


对比缓存

对比缓存,顾名思义,需要进行比较判断是否可以使用缓存。
浏览器第一次请求数据时,服务器会将缓存标识与数据一起返回给客户端,客户端将二者备份至缓存数据库中。
再次请求数据时,客户端将备份的缓存标识发送给服务器,服务器根据缓存标识进行判断,判断成功后,返回304状态码,通知客户端比较成功,可以使用缓存数据。


第一次访问:

再次访问:

通过两图的对比,我们可以很清楚的发现,在对比缓存生效时,状态码为304,并且报文大小和请求时间大大减少。
原因是,服务端在进行标识比较后,只返回header部分,通过状态码通知客户端使用缓存,不再需要将报文主体部分返回给客户端。

对于对比缓存来说,缓存标识的传递是我们着重需要理解的,它在请求header和响应header间进行传递,
一共分为两种标识传递,接下来,我们分开介绍。


Last-Modified  /  If-Modified-Since
Last-Modified:
服务器在响应请求时,告诉浏览器资源的最后修改时间。

If-Modified-Since:
再次请求服务器时,通过此字段通知服务器上次请求时,服务器返回的资源最后修改时间。
服务器收到请求后发现有头If-Modified-Since 则与被请求资源的最后修改时间进行比对。
若资源的最后修改时间大于If-Modified-Since,说明资源又被改动过,则响应整片资源内容,返回状态码200;
若资源的最后修改时间小于或等于If-Modified-Since,说明资源无新修改,则响应HTTP 304,告知浏览器继续使用所保存的cache。



Etag  /  If-None-Match(优先级高于Last-Modified  /  If-Modified-Since)
Etag:
服务器响应请求时,告诉浏览器当前资源在服务器的唯一标识(生成规则由服务器决定)。


If-None-Match:
再次请求服务器时,通过此字段通知服务器客户段缓存数据的唯一标识。
服务器收到请求后发现有头If-None-Match 则与被请求资源的唯一标识进行比对,
不同,说明资源又被改动过,则响应整片资源内容,返回状态码200;
相同,说明资源无新修改,则响应HTTP 304,告知浏览器继续使用所保存的cache。


总结
对于强制缓存,服务器通知浏览器一个缓存时间,在缓存时间内,下次请求,直接用缓存,不在时间内,执行比较缓存策略。
对于比较缓存,将缓存信息中的Etag和Last-Modified通过请求发送给服务器,由服务器校验,返回304状态码时,浏览器直接使用缓存。


浏览器第一次请求:


浏览器再次请求时:


文中如果出现错误,希望小伙伴们可以谅解,更希望可以给予指正

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326463982&siteId=291194637