Web cache talk--etag & If-none-match

Source: Internet
Author: User
Tags browser cache

I. Overview

Cache popular Point, is the "East" has been obtained in a relative to their own, as close as possible, so that the next time, will not be two strokes to the starting point (very far away) to obtain, but the nearest solution, so as to shorten the time and save money (car for money). Web caching is the same thing, that is, when you first visit the URL, this stuff (representations), such as HTML pages, pictures, JavaScript files, etc., there is a closer to you, when you need it next time, No more traveling to the server (Origin servers) to get it. Then, the advantages of Web caching are obvious:

 1, reduce the network delay, speed up the page response speed, enhance the user experience. (because I was nearby, the distance shortened, so the response speed of course than to the remote server to get fast OH);

  2, reduce the network bandwidth consumption. (nearest access);

 3, through the cache, we do not have to the server (Origin servers) to request, thereby reducing the pressure on the server accordingly.

Where does the Web cache put these stuff? I'll look at what kinds of caches I have, so I can see where to put them.

Ii. Types of Web caches

--Database Cache--:

When the Web application relationship is complex, the data table rub against the rise, you can put the query data into memory for caching, the next time you query, directly from the memory cache, thereby improving the response speed.

--CDN Cache--:

CDN Popular Point, that is, when we send a Web request, will go through a hand, and then it helps us calculate the path, where to get these things (representations) path is short and fast. This is a site administrator deployment, so they can also put the representations you often visit in the CDN, so that the response is faster.

--Proxy Server Cache--:

Proxy Server cache, in fact, with the next browser cache nature is similar, the difference is that the proxy server cache-oriented group of more extensive, larger scale. That is, it is not only for a user service, generally for a large number of users to provide services, the same copy will be reused multiple times, it is effective in reducing the corresponding time and bandwidth usage.

--Browser Cache--:

In short, each browser implements the HTTP cache, and when we interact with the server using the HTTP protocol through the browser, the browser is cached based on a set of rules that are agreed with the server. This is especially useful when we click the ' back ' or ' forward ' button on our browser.

Third, the implementation mechanism of Web cache

The so-called mechanism is the agreement between the two sides, clearly tell each other, when to do something. Web caching is the same, you have to tell me when to go to the cache to get, what to the server to get representations it. So, there is a corresponding mechanism, the Web caching mechanism is divided into two large portions of the HTTP protocol (HTTP1.0 and HTTP1.1) and the site manager developed the protocol. Let's take a look at the caching mechanisms defined in the HTTP protocol, leaving behind the protocols developed within the site.

By the means, we can cache by <meta> in the

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

However, it is only part of the browser that can be used, and the proxy server does not bird it. (because meta is in HTML, the proxy server hardly goes back to read it).

--http caching Mechanism--

1. Expires

HTTP caching mechanism is mainly set in the HTTP response header, the relevant fields in the response header are expires, Cache-control, last-modified, If-modified-since, Etag.

In the HTTP 1.0 protocol. In short, it tells the browser to get the resource (representations) directly from the cache before the agreed time, without having to run to the server to get it.

another: Expires is required for time because it is set to time and the time is Greenwich Mean time (GMT), not local.

2. Cache-control

HTTP1.1 protocol, because of it, so you can ignore the above mentioned expires. Because Cache-control is more specific and meticulous than expires.

And, even if the Cache-control and Expires,cache-control are set at the same time , the priority is higher than expires.

Here's a look at the specific meaning of the characters commonly used segment in the Cache-control response header:

(1), Max-age: Used to set the resources (representations) can be cached for how long, in seconds;

(2), S-maxage: And Max-age is the same, but it is only for proxy server cache;

(3), public: Indicates that the response can be cached by any buffer;

(4), private: Only for individual users, but not the proxy server cache;

(5), No-cache: Force the client to send the request directly to the server, that is, each request must be sent to the server. The server receives the request and then determines whether the resource is changed, returns the new content, or returns 304 without changing. This is easy to misunderstand, making people think that responses are not cached. The Cache-control:no-cache is actually cached, but each time the cache responds to the client (the browser), it evaluates the validity of the cached response to the server.

(6), No-store: Prohibit all cache (this is the response is not cached meaning).

3. Etag & If-none-match

http/1.1 Okdate:fri, OCT 1998 13:19:41 gmtserver:apache/1.3.3 (Unix) cache-control:max-age=3600, Must-revalidateE Xpires:fri, OCT 1998 14:19:41 Gmtlast-modified:mon, June 1998 02:28:12 GMTETag: "3E86-410-3596FBBC"Content -length:1040content-type:text/html

The etag belongs to the HTTP 1.1 property, which is returned by the server to the front end,

When you first initiate an HTTP request, the server returns an Etag,


And when you launch the same request for the second time , the client sends a if-none-match at the same time, and its value is the Etag (This is set by the client initiating the request).


The server will then be the same as the server that sends the ETag to the client.


If the same , the value of If-none-match is set to false, the return status is 304, and the client continues using a local cache , the data returned by the server is not parsed (this scenario does not return data because the server's data does not change)

If it is not the same , set the value of If-none-match to true, return a status of 200, and the client re-resolves the data returned by the server

In plain parlance, the etag is a token generated by the server .

And the ETag has a higher priority than last-modified.

4. Last-modified & If-modified-since

The last-modified is similar to the etag. However, last-modified indicates that the response resource was last modified at the server time. Compared to the etag, the insufficiency is:

(1), the last modification of last-modified annotations can only be accurate to the second level, if some files within 1 seconds, has been modified many times, it will not be able to accurately mark the file modification time;

(2), if some files will be generated periodically, when the content does not change, but last-modified changed, resulting in the file can not use the cache;

(3), there is a possibility that the server did not accurately obtain the file modification time, or inconsistent with the proxy server time and other situations.

However, the ETag is a unique identifier on the server side of the server that is automatically generated or generated by the developer, allowing more accurate control of the cache.

Iv. Expansion of Reading

[1], "Caching Tutorial"

Web cache talk--etag & If-none-match

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.