Browser cache: Expires Cache-control last-modified Etag

Source: Internet
Author: User
Tags current time http 200 browser cache
Caching mechanismCheck the Expires expiration time (in http1.0),
(in http1.1) check Cache-control for Max-age, higher priority
Pragma (compatible with http1.0, which has the same meaning as Cache-control in http/1.1 protocol)
Whether the cache resource can be used directly.
You can use: Returns a (from cache) when no interaction between the browser and the server has expired:
(1) Using last-modified
If-modified-since: Last Modified time
The server determines whether the resource is modified or not:
Response to the entire resource content (written in the response message pack), HTTP 200
No modification responds to HTTP 304 (no package, save browsing), telling the browser to continue using the saved cache.
(2) using ETag
If-none-match: Unique identifier of the current resource on the server
The server determines whether the resource is modified or not:
There are modifications, responses 200
No modification, Response 304

Note: last-modified and etag can be used together, the server will first validate the ETag, and verify the last-modified Cache-control to indicate the current resource's validity period . Public indicates that the response can be cached by any buffer. Private indicates that the entire or partial response message for a single user cannot be shared with the cache. This allows the server to simply describe a partial response message for the user, and this response message is not valid for another user's request. No-cache:no-cache are cached, but each time the cache responds to the client (the browser), it evaluates the validity of the cached response to the server. No-store is used to prevent the inadvertent release of important information. Sending in the request message will make the request and response messages do not use the cache. Max-age indicates that the client can receive a response that is not longer than the specified time (in seconds). Min-fresh indicates that the client can receive a response that is less than the current time plus a specified time. Max-stale indicates that the client can receive a response message that exceeds the timeout period. If you specify a value for the Max-stale message, the client can receive a response message that exceeds the specified value for the timeout period. Pragma:no-cache

Same as the Cache-control:no-cache.
Pragma:no-cache is compatible with HTTP 1.0, Cache-control:no-cache is provided with HTTP 1.1.
Therefore, Pragma:no-cache can be applied to HTTP 1.0 and HTTP 1.1, while Cache-control:no-cache can only be applied to HTTP 1.1. Expires and Cache-control

The expires requires that the clocks of the client and the server are strictly synchronized.
HTTP1.1 introduces Cache-control to overcome the limitations of expires head. If Max-age and expires are present at the same time, the max-age has a higher priority.

    Cache-control:no-cache, Private, max-age=0

    etag:abcde

    expires:thu, April 20:00:00 GMT

    Pragma:privat E

    last-modified: $now//RFC1123 format
ETag

The etag is generated by the server side, and the client verifies that the resource is modified by if-match or if-none-match this condition to determine the request. It is common to use If-none-match. The process for requesting a file might look like this:

= = = = First request = = =

1. The client initiates an HTTP GET request for a file;

2. The server processes the request, returns the file contents and a bunch of headers, including, of course, the etag (e.g. "2e681a-6-5d044840") (assuming the server supports ETag generation and the ETag has been turned on). Status Code 200

= = = Second Request = = =

Client initiates an HTTP GET request for a file, note that this time the client sends a If-none-match header at the same time, The content of this header is the etag:2e681a-6-5d0448402 returned by the server on the first request. The server determines the etag sent over and computes the ETag match, so If-none-match is false, returns 200, returns 304, the client continues to use the local cache; Simple, the question is, what if the server is set up with Cache-control:max-age and expires?

The answer is to use it at the same time, that is, after checking the modification time and the ETag exactly as if-modified-since and If-none-match are done

The server can return 304. (Don't get caught up in the problem with who you're using) Why use an ETag request header?

Etag is mainly to solve some problems that last-modified cannot solve.

The last modification of the last-modified callout can only be accurate to the second level, and if some files are modified multiple times within 1 seconds, it will not be able to accurately label the file modification time.

If some files are generated on a regular basis, and sometimes the content does not change, but the last-modified changes, causing the file to be unable to use the cache

There may be situations where the server is not getting the file modification time accurately or inconsistent with the proxy server time.

An etag is a unique identifier on the server side of a server that is automatically generated or generated by the developer, allowing more accurate control of the cache. Last-modified and ETag can be used together, the server will first verify the etag, consistent with the case, will continue to compare to last-modified, and finally decide whether to return 304. IE Caching issues

In IE, if the requested method is get, and the requested URL is not changed, then the result of the request will be cached. The solution to this problem can be changed by changing the URL of the request in real time, as long as the URL changes, it will not be cached, you can add the URL at the end of the random timestamp parameter (' t ' = + new Date (). GetTime ())

Or:

Open (' GET ', ' demo.php?rand= ' +math.random (), true);//

Reference:
http://blog.csdn.net/eroswang/article/details/8302191
Https://developers.google.cn/web/fundamentals/performance/optimizing-content-efficiency/http-caching?hl=zh-cn
http://uule.iteye.com/blog/2212906

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.