HTTP please ask the browser's cache system

Source: Internet
Author: User
Tags browser cache

Download from: http://kb.cnblogs.com/page/73901/process

When the resource is accessed for the first time, the HTTP header is as follows

(Request-line) Get/a.html http/1.1
Host127.0.0.1
User-agent mozilla/5.0(X11;U Linux i686; ZH-CN; rv:1.9.0.15) gecko/2009102815 ubuntu/9.04 (jaunty) firefox/3.0.15
Accept text/html,Application/xhtml+xml,application/xml; Q=0.9,*/*;q=0.8
accept-language Zh-cn,zh; Q=0.5
accept-encoding gzip ,deflate
Accept-charset gb2312,utf-8;q=0.7,;q=0.7
keep-alive 300
Connection keep-alive

HTTP returns headers as follows

(Status-line) http/1.1 200Ok
Date Thu, 26Nov2009 13:50:54Gmt
Server apache/2.2.11(Unix) php/5.2.9
Last-modified Thu, 26 Nov 2009 13:50:19 GMT
Etag " "
accept-ranges bytes
Content-length 20
Keep-alive Timeout=5, Max=100
Connection keep-alive
Content-type text/ HTML

When the resource is first accessed, HTTP returns a status code of 200 and carries some descriptive information about the current resource on the head, such as

last-modified//Indicates the last modified time
Etag//Indicates the status of the resource uniquely identified
Expires//Indicates the expiration time of the resource in the browser cache

The browser then caches the file to the cache directory and saves the file's above information

When the file is requested for the second time, the browser checks to see if the file is contained in the cache directory, and if it does not have the time set by the expires, that is, the file has not expired, then the browser will read the file directly from the cache directory and no longer send the request

If the file has expired at this time, the browser sends an HTTP request to webserver and carries the following information about the current file on the head

If-modified-since Thu, from : GMT
If-none-match "8fb8b-14-4794674acdcc0"

That is, the time of the last modification, along with the ETag value returned by the last request, is sent to the server. When the server receives this request, it parses the information in the header and verifies the header information.

If the file has not been modified from the last time until now or the ETag information has not changed, then the server will return a state of 304 directly, and no longer return the file resource, the status header is as follows

(Status-line) http/1.1 304Not Modified
Date Thu, 26Nov2009 14:09:07 GMT
Server Apache/2.2.11 (Unix) php/ 5.2.9
Connection keep-alive
keep-alive Timeout =5, Max=100
Etag Span style= "color: #000000;" > "8fb8b-14-4794674acdcc0"
/span>

This can greatly reduce network bandwidth and enhance the user's browser experience.

Of course, if the server is modified by a matching discovery file, the file resource is returned with the new file status information.

Basic Field Pragma

The pragma header domain is used to contain implementation-specific instructions, most commonly pragma:no-cache. In the http/1.1 protocol, it has the same meaning as Cache-control:no-cache.

Expires

When the file expires in the local cache, the request is not sent if the browser discovers that the file in the cache is not expired (with exceptions, described later)

Cache-control

CACHE-CONTROL Specifies the caching mechanism that requests and responses follow.

Setting Cache-control in a request message or response message does not modify the caching process in another message processing process.

Etag/if-none-match

A pair of authentication file entities marked "Entity tag" in response/request header Apache, the value of the ETag, by default is the index section (INode) of the file, the size and last modified time (MTime) are hashed after the resulting

Last-modified/if-modified-since

Response/Request header for modified time of a pair of validation files

Expires, Cache-control, last-modified, and ETag are several fields related to the page cache in the RFC 2616 (http/1.1) protocol.

The first two to control the expiration date of the cache, the browser can be determined by it, need not send an HTTP request;

The latter two are used to verify the validity of the Web page, which is used by the server to verify that the file needs to be re-returned

Last-modified VS Etag

Now that you have last-modified, why use the ETag field? Because if you make two changes to a file within a second, the last-modified will be incorrect. As a result, http/1.1 uses the entity tag header to provide more rigorous validation.

A different situation

Described above is a normal browser cache state, in the actual application, such as page jump (click the page link jump, window.open, in the address bar hit enter, refresh the page) and other operations, there will be some differences

Normal page Jump

Normal page jump including link Click to jump, with JS script to open a new page (window.open)

In the absence of a cache, the request returns all resource results

The browser will not issue an HTTP request when the expires is set and not expired

If expires expires, a corresponding request is sent with information such as last-modifed for the server to verify

Page Refresh (F5)

In this case, you will generally see a lot of 304 of requests, that is, even if the resource is set to expires and not expired, the browser will send the corresponding request

HTTP please ask the browser's cache system

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.