Personal study notes, for recording only
Cache related headers
Expires
: Response header, HTTP 1.0 artifact, representing resource expiration time.Cache-Control
: request/response headers, cache control fields, precise control of cache policiesIf-Modified-Since
: Request header, which is told by the browser to the server when the resource was last modifiedIf-Node-Match
: Request header, cache resource identifierLast-Modified
: Response header. The server tells the browser when the resource was last modifiedEtag
: Response header, resource identifier
Caching strategies
No cache
-
Browser request -> server;
-
Server read -> disk file;
-
Server response -> Browser.
Problem: Waste of user traffic, waste of server resources, impact on user experience (wait every time)
Cache to the local client
After the first request succeeds, the file is copied to the local disk. The next request is directly read from the disk.
Problem: When the server file is updated, the browser cannot get the latest file
The server and browser specify resource expiration time
With response header Expires, the server responds to a file with an example in the response header (Expires: Mon, 26 Sep 2018 05:00:00 GMT) that indicates the local time is less than the specified date, so the browser can always use the local cache.
Problem: The browser still does not know if the resource has been modified
The server tells the browser when the resource is modified
When the server responds to a file, last-Modified: (GMT) is used in the response header to tell the browser when the file was Last Modified, as well as Expires.
When a file Expires: The browser requests the server with if-modified-since (equal to last-modified of the Last request)
The server compares the if-modified-since time on the request header with the actual time when the file was last Modified: – If consistent: the server responds (304), which means that the browser can continue to use the cache – If inconsistent: The server reads the disk file response to the browser, which updates the cached file description based on last-Modified and Expires in the response header.
Problem: Expires controls are unstable because browsers can change the time at will
Continue to improve, introduceCache-control
It is expressed by a relative time (relative to the time of this request), such as cache-control: max-age=10, which means that the Cache is available in 10 seconds. Precedence over Expires.
The advantage is that there is no bias problem with max-age if the local time and the server are already off.
3. Summary
Mandatory cache
When the browser sends a request to the server, the server returns the Cache rule in the HTTP header of the HTTP response packet together with the request result to the browser. The fields that Control the mandatory Cache are Expires and cache-Control, respectively. Cache-control has a higher priority than Expires.
Expires
HTTP/1.0 artifacts, now replaced by HTTP/1.1 cache-control. The reason for this is that Expires caching works by comparing the “client time” to the “server return time,” but once the client and server time are out of sync, enforcing caching doesn’t make sense.
Cache-control
:
This command is used to control the page cache. The value can be:
public
: All content will be cached; (Both client and proxy servers can cache)Private (default)
: All content can only be cached by the client;no-cache
: The client caches the content, but whether to use the cache needs to be determined by the negotiated cache.no-store
: All content is not cached, neither forced cache nor negotiated cache is used;max-age=xxx
: The cache contents will expire in XXX seconds
Note: Cache-Control is a better option than Expires in cases where it is not possible to determine whether the client’s time is synchronized with the server’s. Therefore, only cache-control takes effect when both exist.
from memory cache
和 from disk cache
What do they stand for? When will they be used?
From memory cache
: Memory cache has two characteristics, namely fast access and timeliness:- Fast read: The memory cache directly stores the compiled and parsed files into the memory of the process, occupying certain memory resources of the process, and facilitating the fast read of the next run.
- Timeliness: Once the process is shut down, its memory is emptied.
From disk cache
: Disk cache directly writes the cache to disk files. To read the cache, I/O operations are performed on the disk files stored in the cache and then the cache contents are parsed again. The reading of the cache is complex and slower than that of the memory cache.
Negotiate the cache
Negotiation cache is a process in which the browser sends a request to the server with the cache id after the cache is invalid, and the server decides whether to use the cache based on the cache ID.
- Negotiation cache takes effect, return 304;
- Negotiation cache invalid, return 200 and request result.
The fields that control the negotiation cache are last-modified/if-modified-since and Etag/if-none-match.
Last-Modified / If-Modified-Since
Last-Modified
When the server responds, returns the time when the resource was last modified on the server.If-Modified-Since
: indicates that the server responded to the last request when the client initiates the request againLast-Modified
The value of the.
The server determines whether the time when the resource was last Modified on the server is consistent with the if-modified-since value in the request header. If so, the negotiated cache takes effect; otherwise, the negotiated cache is invalid.
Etag / If-None-Match
Etag
: is a unique identifier of the current resource that is returned when the server responds.If-Node-Match
: indicates that the server responded to the last request when the client initiates the request againEtag
The value of the
The server checks whether the Etag value of the resource on the server is the same as the if-node-match value in the request header. If yes, the negotiation cache takes effect; otherwise, the negotiation cache is invalid.
Note: Etag/if-none-match has a higher priority than last-modified/if-modified-since. If both Etag/if-none-match exist, only Etag/if-none-match takes effect.