Cache HTTP protocol Header __http Protocol header

Source: Internet
Author: User
Tags browser cache

Introduction

There are many types of caching: Server caching, Third-party caching, browser caching, proxy caching, gateway caching, database caching, and so on. The browser cache is the least expensive because the browser cache relies on the client and consumes almost no server-side resources. Today's summary is also associated with browser caching.

Browser caching must be very familiar, simply that the browser on the disk to the most recent user requested data storage, when we request this page, the browser can be read from the local disk and display.

There are two main types of browser caching:

Cache negotiation: Last-modified,etag

Total cache: Cache-control,expires

Here's a quick look at these important protocol headers (the HTTP date time must be Greenwich Mean Time (GMT), not local. The following are all GMT)

"Last-modified"

Some pages are changed in minutes, some content may take weeks or months to change, but the constant is that the server always knows the changes to the content data, and HTTP provides a way for the server to send the most recently modified data along with the requested data. The most recent changes to the data-checking process are as follows:

The first time a request is made to the server, the server feeds back the response with the Last-modified:gmttime header. When we request the same data again, we send the request with the If-modified-since:gmttimehttp request header, which contains the last date the data was fetched from the server. The server itself has a time comparison with all the last-modified times of the server itself, and if the data on the server has not changed since then, the server will return an HTTP status code 304 indicating that the data has not changed. Use your local cache data directly.

"ETag"

ETag is similar to the recent revision of the principle of data checking: the first time a request is made to the server, the server feeds back the response that contains not only the data you requested, but also the hash of some data (given in the ETAG header information). This hash will depend entirely on the server. When the same data is requested again, the request we send contains If-none-match: header information, which contains Etaghash, and if the data has not changed, the server will return a 304 status code after the decision.

(The difference between Ps:etag and last-modified is that the server is comparing the value of different, last-modified compared to the most recent modified events, ETag compared to the hash value)

"Expires"

Expires refers to the expiration time, especially the cache expiration time on the cached server. A caching server is a server that hosts frequent access to content. The Expires property indicates how long the cached data on the cache is up to date, the expiration time of the expires setting, and the expiration of the cache within the cache, and the need to resend the request to the source server to check whether the data has been modified or to obtain the latest data.

"Cache-control"

The value of expires is an absolute time, generated by the server, but there is a potential problem: The source server and the cache server may not be the same time. To solve this problem, use Cache-control, which saves a time relative to the browser. Cache-control has the highest priority. Let's take a look at the commonly used cache-directive values of Cache-control:

①max-age=***: Indicates that the contents of the cache will expire after * * seconds, in seconds.

②public: means all content will be cached. (because HTTP authenticated content can be cached by default)

③private: means that the content is cached only in the private cache, that is, only the client can cache, and the cache server is not available.

④no-cache: Indicates that each request is forced to be sent directly to the source server without a local cached version of the checksum. That is, you must confirm with the server that the returned response was changed before you can use the response to satisfy subsequent requests for the same URL. Therefore, if there is an appropriate authentication token (ETAG), No-cache initiates round-trip traffic to verify the cached response and, if the resource has not been changed, to avoid downloading.

⑤no-store: Indicates that no content is forced to be cached and not cached.

⑥must-revalidate and Proxy-revalidate: Indicates that if the cached content is invalidated, the request must be sent to the server or the proxy server for verification. Proxy-revalidate only works on the caching proxy server.

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.