Browser HTTP cache mechanism and cache rule parsing, cache mechanism rule Parsing
When a static file (image, css, js) is requested, these files are stored as files that do not change frequently, the client is a method to optimize user browsing experience. So this is the significance of the client cache.
The Http cache mechanism is an important means to optimize web performance. Many users only know why the browser caches the requested static files, and how the cache takes effect, but it is not very clear.
1. cache-related header fields in the http message
Let's take a look at the cache-related fields in the 47 types of http packet header fields specified by RFC2616, so that we can have a bottom point in our hearts:
Common header field (that is, fields that can be used in both request messages and response messages)
Request Header field
Response Header field
Object header field
There are several methods to set browser cache:
Expires: The file expiration time in the local cache directory (specified by the server)
Cache-control: the file expiration time in the local Cache directory (the expiration interval specified by the server, because the browser generates the specific time according to the interval)
Last-Modified: Last modification time of the file on the server
Etag: File ID
2. cache rule Parsing
There are multiple HTTP cache rules, which are categorized based on whether or not requests need to be initiated to the server. I divide them into two categories (mandatory cache, comparison cache)
Before introducing these two rules in detail, we will first use the time sequence diagram to give you a simple understanding of these two rules.
When cached data already exists, it is only based on forced caching. The request data process is as follows:
When cache data already exists, it is only based on comparison cache. The request data process is as follows:
We can see that the two types of cache rules are different. If the forced cache takes effect, you do not need to interact with the server. However, whether the cache is effective or not, you need to interact with the server.
The two types of cache rules can exist at the same time. The priority of the mandatory cache is higher than that of the comparative cache. That is to say, when the mandatory cache rule is executed, if the cache takes effect, the cache is used directly and the comparison cache rule is no longer executed.