Caching mechanisms for HTTP

Source: Internet
Author: User
Tags time zones set time

http The caching mechanism

Objective

Understand http caching mechanism is beneficial to improve the performance of the website, reduce the pressure on the server and speed up the response to client requests.

What is http the cache?

httpd the cache is in the browser and Web Server or Web between the proxy servers. When a client sends a request, if the local browser caches a "copy" (that is, the requested data), and the copy is in the valid time, then the data will be returned directly from the browser cache, so that it does not need to be loaded on the backend original server to reduce network bandwidth, improve The purpose of Web server performance.

Benefits of Caching

1 , reducing network bandwidth

2 , reduce the pressure on the server, improve the Web performance of the server

3 , you can speed up requests that respond to customer satisfaction

Public cache and private key cache

Public cache: That is, data can be shared to multiple users

private Key cache: Cached data can only be used by itself (for example, Cookies information cannot be cached on the public cache)

Freshness of cached data

the freshness of the cached data indicates whether the cached information is up to date. the Web Server uses two ways to determine the freshness of the cache:

I. The browser takes the last modification time of the cached data by requesting header " if-modified-since "To tell web server, thus web The server can use this to interpret the freshness of the cache.

second, the browser to the cache data ETag Information by Request Header " If-none-match "Tell Web

server, thereby Web The server can use this to interpret the freshness of the cache.

by requesting Header "If-modified-since" The way to determine the freshness of the cached data is implemented:

1 , when the client requests a page, first in the local browser to check whether there is a cache, if not, directly to the local server, if there is this page, see the last modification of this page, and will HTTP Header " if-modified-since "parameter is added to the request message and sent to the server side.

2 , when the local server receives this request, the last modification time of the page in the request message is compared to the last modification time of the page on the local server, and if the time is the same, the cache is up-to-date (that is, the cache is not updated), the server returns a 304 Not Modified information to the client, telling the client that the page is not updated, that you can use it, and that if the time is different, the cache has been updated.

3 , if the time is inconsistent, the cache has been updated. At this point the server returns the most recent page to the client.

4 , if the client requests data that the local server does not have, the local server requests the original server back, caches the results of the request on the local server, and then returns the results of the response to the client. This allows the client to cache the returned data on the client browser.

by the time the request was carried Header " If-none-match "A mechanism for determining the freshness of cached data:

This is actually the same way as the above-mentioned principle, except that the two of them carryHeaderparameter is different, check theHeaderThe parameters are not the same. The principle is that when a client initiates a request for the first time,WebThe server generates one for the data generated by the requestEtagheader, and whenever the data changes,Webthe server will generate aETagheader. And then put thisEtagThe header is returned to the client and is saved locally by the client. When the client sends the request the next time, it adds the request headerEtagheader,Webafter the server receives the request, checkWebOf the content on the server.ETagand in the request message.ETagConsistency indicates that the requested content has not been modified, theWebthe server will return a "304 Not Modified "information to the client, telling the client that the data you requested has not been modified, you can use your own local data. IfETagvalue is inconsistent, the requested data is updated and the client's cache content is invalidated. The server then returns the latest content to the client.

From the above, both methods can determine the freshness of the cached data, but which way is more accurate?

In fact the second way by comparing etag web If the data modifications on the server are completed in seconds, this method of judging will result in an error. In this way, the client locally caches the data and web etag The header is different, a little web The data on the server has changed, etag

http the request Flow

The client's first request process is as follows:

650) this.width=650; "title=" Http1.png "src=" http://s3.51cto.com/wyfs02/M00/5E/13/ Wkiom1uphxxrzeddaacpcfxw1sq751.jpg "alt=" Wkiom1uphxxrzeddaacpcfxw1sq751.jpg "/>

The next request process for the client is:

650) this.width=650; "title=" Http2.png "src=" http://s3.51cto.com/wyfs02/M01/5E/13/ Wkiom1uphykxlqwtaafeadqjni8186.jpg "alt=" Wkiom1uphykxlqwtaafeadqjni8186.jpg "/>

http the related cache header parameters

1,Expires: Used to defineWebthe expiration date or time of the object, typicallyGMTformat. Expiresis aHTTP 1.0the things that the browser now uses by default isHTTP 1.1, all its functions are largely ignored. ButExpiresOne drawback is that its expiration time uses absolute time, which can cause errors if the client and server times are not synchronized (or across time zones). In theHTTP 1.1used inCache-control:max-ageinstead of.

2 , Cache-control : This is the cache indication that all caching mechanisms must follow. It has a number of subcommands, respectively, as follows:

Public : The response is cached as a public cache that can be shared among multiple users

Private : The response is cached, except that the cache can only be used as a private cache and cannot be shared by multiple users

No-cache: indicates that the browser cache is not used, but can be stored and cannot be used to respond to client requests until the validation data is validated, and the request data is returned from the server )

No-store: indicates that data cannot be stored (storage and caching is not meant) and is generally used for sensitive files.

Max-age: defines the length of time that the cache is valid. It has a higher priority than the Expires, which means that the max-age set time ignores the time set in Expires

must-revalidate : If the buffered data expires, it must be sent to the server side for re-authentication

3 , Cache-control is the most important setting for browser caching because it overrides other settings, such as Expires and the last-modified . Also, this property is the most effective way to handle cross-browser caching issues because the browser behaves essentially the same.

4 , Etag : A response header that is used in response messages for a Web resource definition version identifier;

5 last-mofified : Response header for responding to client about last-modified-since web

6 , if-modified-since : The conditional request header, if it is requested after the time specified in this request header Web when the content has changed, the server responds to the changed content, otherwise the response 304 (not modified) ;

7,If-none-match: Conditional request Header,Webthe server will provide aWebobject defines aETagheader, and returns it to the client, where the client saves the header locally. When a request is received, the client carries the request header, and when the server receives it, it compares theWebobject in the request message.ETagand the server-sideETagIs the same, if the same, indicates that the cache is not updated, returns304 (not modified)Otherwise, the latest data on the server side will be returned to the client.

8 vary : Response header, Tells the proxy server to cache two versions of the resource: compressed and non-compressed, which helps to prevent some public agents from correctly detecting content-encoding header problem.

9 , Age : An additional response header that the cache server can send to specify the validity period of the response; The browser usually determines the cache duration of the content based on this header, and if the response message header uses the Max-age command, then the cache is valid for the duration " Max-age minus Age "the result;

This article is from the "Linux Learning path" blog, so be sure to keep this source http://xslwahaha.blog.51cto.com/4738972/1631348

Caching mechanisms for HTTP

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.