Browser caching mechanism

Source: Internet
Author: User
Tags current time html page http 200 http request

Reprinted from: http://web.jobbole.com/86957/

Browser caching mechanism, in fact, is mainly the HTTP protocol definition of the caching mechanism (such as: Expires; Cache-control, etc.). But there are also non-HTTP protocol-defined caching mechanisms, such as the use of HTML Meta tags, web developers can be in the HTML page

HTML code
<meta http-equiv= "Pragma" content= "No-cache" >

The purpose of the above code is to tell the browser that the current page is not cached, each access needs to go to the server pull. It is easy to use, but only some browsers can support it, and all cache proxies are not supported because the agent does not parse the HTML content itself.

Here I mainly describe the caching mechanism defined by the HTTP protocol. Expires Strategy

Expires is a Web server response message header field that, in response to an HTTP request, tells the browser that the browser can cache data directly from the browser before the expiration time, without having to request it again.

Below is the Baby PK project, the browser pulls the response header of the Jquery.js Web server:

Note: The Date header field represents the time the message was sent, and the time description format was defined by RFC822. For example, Date:mon,31 DEC 2001 04:25:57gmt.

The Web server tells the browser to use the cache file before this point in time, 2012-11-28 03:30:01. The time to send the request is 2012-11-28 03:25:01, which is 5 minutes cache.

However, expires is the HTTP 1.0 thing, now the default browser is using HTTP 1.1 by default, so its function is basically ignored. Cache-control Strategy (focus on)

Cache-control is consistent with expires, which indicates the validity of the current resource, whether the browser caches data directly from the browser or re-sends the request to the server. But Cache-control more choice, more detailed settings, if set at the same time, its priority is higher than expires.

HTTP protocol Header Cache-control:
Values can be public, private, No-cache, No-store, No-transform, Must-revalidate, Proxy-revalidate, Max-age

The instructions in each message have the following meanings: 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 indicates that a request or response message cannot be cached no-store is used to prevent important information from being inadvertently published. 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.

or the above request, the value of the Cache-control header returned by the Web server is max-age=300, which is 5 minutes (consistent with the expires time above, which is not required).

last-modified/if-modified-since

Last-modified/if-modified-since to be used with Cache-control.

L Last-modified: Indicates the last modification time of this response resource. When the Web server responds to a request, it tells the browser the last modification time of the resource.

L If-modified-since: When a resource expires (using Cache-control-identified max-age) and the discovery resource has a last-modified claim, the request to the Web server takes up If-modified-since, Represents the request time. When the Web server receives the request, it finds that the header if-modified-since is compared to the last modification time of the requested resource. If the last modification time is newer, indicating that the resource has been changed, then respond to the entire resource content (written in the response message packet), HTTP 200, if the last modification time is older, the resource has no new modifications, the response to HTTP 304 (no package, save browsing), tell the browser to continue to use the saved cache. Etag/if-none-match

Etag/if-none-match should also be used in conjunction with Cache-control.

L Etag:web When the server responds to a request, it tells the browser that the current resource is uniquely identified on the server (the build rule is felt by the server). In Apache, the value of the ETag, by default, is obtained by hashing the file's index section (INode), size, and last modified time (MTime).

L If-none-match: When a resource expires (using Cache-control-identified max-age) and the discovery resource has a etage claim, it is requested again with a If-none-match (ETag value) to the Web server. When the Web server receives the request, it finds that the header if-none-match is compared to the corresponding check string for the requested resource and decides to return 200 or 304. Both Raw last-modified Mr He etag.

You might think that using last-modified is enough for the browser to know if the local cache copy is new enough and why the ETag (entity ID) is needed. The appearance of the ETag in HTTP1.1 is mainly to solve several last-modified problems that are more difficult to solve:

L LAST-MODIFIED The last modification of the callout can only be accurate to the second level, if some files within 1 seconds, has been modified multiple times, it will not be able to accurately mark the file modification time

L 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. user behavior and caching

Browser caching behavior is also related to user behavior ...

User actions Expires/cache-control Last-modified/etag
Address Bar Enter Effective Effective
Page link Jump Effective Effective
New open Window Effective Effective
Forward and backward Effective Effective
F5 Refresh Invalid Effective
Ctrl+f5 Refresh Invalid Invalid
Summary

Browser Request for the first time:

When the browser requests again:

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.