The concept of caching
caching This thing is really everywhere, There is a browser-side cache, There is a server-side cache, a proxy server cache, have a Asp. NET page cache, object cache. The database also has caches, and so on.
The caching feature in HTTP is the browser cache , as well as the cache proxy Server .
HTTP caching means that when a Web request arrives at the cache, it can be extracted from the local storage device instead of from the original server if there is a locally cached copy.
Benefits of Caching
The benefits of caching are obvious, the benefits are,
1. Reduce the redundant data transfer and save the network fee.
2. Reduce the burden on the server, greatly improve the performance of the website
3. Speed up the loading of Web pages for clients
Fiddler can easily view the cached header
Fiddler the headers are grouped together so that they can be easily viewed.
How to determine the freshness of a cache
The Web server determines whether the browser cache is up-to-date in 2 ways.
the first kind, The browser tells the Web server the last modification time of the cache file through the header "If-modified-since".
the second kind, The browser puts the cache file ETag, through the header "If-none-match", to tell the Web server.
The cache freshness is determined by the last modification time
1. The browser client wants to request a document, first checks the local cache, discovers the existence of this document cache, gets the last modification time of the cached document, by: If-modified-since, sends the request to the Web server.
2. The Web server receives the request to change the server's document modification time (last-modified): Compared to the if-modified-since in the request header, if the time is the same, the cache is still up to date, The Web server will send 304 not modified to the browser client, telling the client to use the version in the cache directly. Such as.
3. If the document has been updated. The Web server sends the latest version of the document to the browser client, such as.
Example: Open Fiddler, then open the blog home page. Then F5 refreshes the browser several times. You will see the blog home page also uses the cache.
Cache-related headers
let 's take a look at the specific meanings of each header.
Request
Cache-control:max-age=0 |
In units of seconds |
If-modified-since:mon, 08:38:01 GMT |
The last modified time of the cached file. |
If-none-match: "0693f67a67cc1:0" |
The ETag value of the cache file |
Cache-control:no-cache |
Do not use cache |
Pragma:no-cache |
Do not use cache |
|
|
Cache-control:public |
The response is cached and shared among multiple users (the difference between public and private caches, see another section) |
Cache-control:private |
The response can only be used as a private cache and cannot be shared among users |
Cache-control:no-cache |
Remind the browser to extract documents from the server for validation |
Cache-control:no-store |
Absolute Forbidden Cache (for confidential, sensitive files) |
Cache-control:max-age=60 |
Cache expires after 60 seconds (relative time) |
Date:mon, 08:39:00 GMT |
The current time response sent |
Expires:mon, 08:40:01 GMT |
Time the cache expires (absolute time) |
Last-modified:mon, 08:38:01 GMT |
Last modified time for server-side files |
ETag: "20b1add7ec1cd1:0" |
ETag value for server-side files |
Response
What if there are cache-control and expires at the same time ?
browsers always prefer to use Cache-control, if there is no cache-control to consider expires
ETag
An etag is an abbreviation for an entity tag that identifies the state of a resource, based on a hash string generated from the entity's content (similar to MD5 or SHA1 results). The ETag also changes as the resource sends changes.
The etag is generated by the Web server and then sent to the browser client. The browser client does not care about how the ETag is generated.
Why use Where's the etag? Mainly to solve some problems that last-modified cannot solve.
1. Some servers do not accurately obtain the last modification time of the file, so that the last modification time will not be able to determine whether the file is updated.
2. Some files are modified very frequently, and are modified within the time of the second. Last-modified can only be accurate to seconds.
3. The last modification time of some files has changed, but the content has not changed. We do not want the client to think that this file has been modified.
instance, Open Fiddler, open the blog Garden home. You can see a lot of pictures, or the CSS files are cached. This is done by comparing the ETag value to determine if the file is updated.
Browser does not use cache
Ctrl+f5 force the browser to refresh, or set ie. You can let the browser not use the cache.
1. The browser sends an HTTP request to the Web server with Cache-control:no-cache in the header. Explicitly tell the Web server that the client does not use the cache.
2. The Web server will send the latest documents to the browser client.
Example :
Open Fiddler, open the blog home page, and press Ctrl+f5 to force refresh the browser, you will see
The role of Pragma:no-cache is identical to that of Cache-control:no-cache. are not using the cache.
Pragma:no-cache is defined in HTTP 1.0, so in order to be compatible with HTTP 1.0. So Pragma:no-cache and Cache-control:no-cache are used at the same time.
Use cache directly, do not go to server authentication
Press F5 Refresh the browser and enter the URL in the Address bar and then enter. These two behaviors are not the same.
Press F5 refreshes the browser, the browser will go to the Web server to verify the cache.
If you enter a URL in the Address bar and return, the browser will "directly use a valid cache" without sending an HTTP request to the server to verify the cache, which is called a cache hit, such as
Example: Compare the first visit to the blog Park home page and the second Blog Park homepage
1. Start fiddler, use Firefox to open the blog Park home page, found that there are more than 50 session.
2. Press Ctrl+x to delete all sessions in the Fiddler. Close Firefox, reopen a Firefox, open the blog home page. Found only more than 30 sessions.
analysis; the missing session is because Firefox uses the cache directly, without sending an HTTP request.
How to set IE does not use caching
Open Ie. Click on the toolbar, Tools->internet options, general, and browse history settings. Select "Never". and then save.
then click "Delete" to delete the temporary Internet files (ie cached files are temporary Internet files).
The difference between public and private caches
Cache-control:public refers to a public cache, which can be shared by thousands of users.
Cache-control:private means that only private caches are supported, and private caches are private to individual users.
HTTP protocol (iv)---cache