Introduction
Getting content over the web is slow and expensive. Large counterparts require multiple round-trip communication between the client and the server, which delays the time the browser can consume and process content, and increases the cost of communication. Therefore, the ability to cache and reuse previously acquired resources becomes a critical aspect of performance optimization.
What are the main HTTP header fields related to cache
(1) Generic header field (meaning that it can be used for both request messages and response messages)
| Field name |
Description |
| Cache-control |
Controlling the behavior of the cache |
| Pragma |
http1.0 legacy, Cache is disabled when the value is "No-cache" |
(2) Request header field
(3) Response header field
(4) Entity header field
The connections and differences between these HTTP header fields
Pargram: Set No-cache Disable cache
Expires: Time Control tells the browser that the resource cache expires and does not send the request if it has not expired.
Priority of HTTP Cache header fields
Priority: Pragram>cache Control>expires
Features and limitations of HTTP cache header fields
The first field above allows the client to decide whether to send a request to the server. In order to make more efficient use of the cache between the client and the server, HTTP1.1 has added several header fields to do the job.
(1) last-modified
When the server passes a resource to the client, the last time the resource is changed is loaded with the entity header as "LAST-MODIFIED:GMT" back to the client. The client will mark the information on the resource, and then the next request, the "LAST-MODIFIED:GMT" This information will be added to the request, the server to do the check, if the resource has not been modified (this place itself experience), then return 304 status code, if the resource has been modified, The 200 status code is returned.
(2) ETag
In order to solve the problem that "last-modified:gmt" may be inaccurate, HTTP1.1 also introduced the ETag field. The server uses an algorithm that adds a unique identifier to the resource and returns the resource to the client, which is returned to the client with an "ETag: identifier" in the entity header, and the client retains the resource's ETag field. Of course, when the client requests again, the ETag is taken, and the server simply compares the etag to the server (unless the resource on the server has been updated, the ETag on the server will not change).
Differences in user refresh behavior on different pages
There are three types of ways that users can refresh the Access interface:
(1) Enter address in the browser address bar
(2) F5, click the Refresh button in the browser toolbar, right-select reload
(3) Ctl+f5
Different means page loading speed is not the same. What is the difference between them? Suppose that the browser does not have any access to an address, and there is no cache file. Prepare work, Address bar Enter the URL, the first time to visit the page, to view the request and response information can see the request header does not have any information about the HTTP cache, the response header contains:
cache-control:max-age=31104000
expires:thu,20 Jul 02:18:41 GMT
last-modified:fri,15 Jul 04:11:51 GMT
At this point, the browser caches the information until the file expires, the user empties the cache, or the user forces the resource to refresh.
When the preparation is complete, it is time to compare the differences between the three:
(1) Input address browser found that the resource has been cached cut not expired (through Cache-control or expires head), not with the server confirmed, directly use the browser cache.
(2) F5 will let the browser send an HTTP request to the server anyway, but his head will be added to the browser cache last-modified, the server after the judgment, the discovery has not expired, so it returned a 304 status code. This information is very small, so the page is refreshed quickly.
(3) Ctl+f5 is completely from the server to a point resources come over.
Which headers should be used in practice to control caching
Most of the header fields are still useful.
Although not very understand, but the unknown, practitioners will understand!
HTTP Cache Control Summary