[Go] Browser caching mechanism

Source: Internet
Author: User
Tags http 200

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 <meta> tag, the code is as follows:

<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: TheDate 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:

Value 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:

  1. public indicates that the response can be cached by any buffer. The
  2. 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.
  3. no-cache indicates that the request or response message cannot be cached
  4. 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. The
  5. max-age indicates that the client can receive a response that is not longer than the specified time (in seconds). The
  6. min-fresh indicates that the client can receive a response that is less than the current time plus a specified time. The
  7. max-stale indicates that the client can receive a response message that exceeds the timeout period. If you specify the value of the max-stale message, the client can receive a response message that falls outside the specified value for the timeout period.

or the above request, thevalue 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.

    • Last-modified: Indicates the last modification time for this response resource. when the Web server responds to a request, it tells the browser the last modification time of the resource.
    • 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 If-modified-since that 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 response 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 (without the package body, save browsing ), Tell the browser to continue using the saved cache.
Etag/if-none-match

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

    • Etag:When the Web server responds to a request, it tells the browser that the current resource is uniquely identified by the server (the build rule is felt by the server). in Apache, the value of theETag, by default, is obtained by hashing the file's index section (INode), size,and last modified time (MTime) .
    • If-none-match: When a resource expires (using Cache-control-identified max-age) and the discovery resource has a etage claim, the request to the Web server If-none-match (The value of the ETag). 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 bornlast-modified Mr He Etag?

You might think that using last-modified is enough to let the browser know if the local cache copy is new enough, why do you need an Etag (entity identity)? the appearance of the ETag in HTTP1.1 is mainly to solve several last-modified problems that are more difficult to 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.

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

Summarize

Browser Request for the first time:

When the browser requests again:

Original:

http://kb.cnblogs.com/page/165307/

[Go] Browser caching mechanism

Related Article

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.