In-depth analysis of cache and in-depth analysis of Cache

Source: Internet
Author: User

In-depth analysis of cache and in-depth analysis of Cache
Three things about cache

This article focuses on the detailed process of caching. To make it easy for everyone to understand and selectively understand the entire cache mechanism, I will start with the problem here, step by step, we will analyze the Cache principles and operations performed by the user proxy (which can be simply considered as a browser) here.

Problem:

If you have no doubt about the above questions, you can skip this article ~

The difference between cache and 304

To understand this problem, we first need to figure out the entire process of processing cache by the browser. See:

When a request arrives, the user agent first checks whether there is a cache and whether the cache is fresh. If there is a cache and it is fresh, it is directly provided to the client. If not, this will trigger a re-verification process with the server. If the file is not changed during the verification process, a 304 error is returned. At this point, you should understand the difference between cache and 304. In fact, they are two independent processes. The first is cache check, and then server-side verification, 304 is one of the return values of server-side verification. If the server-side verification finds that the file has been updated, it will be returned directly with 200.

Now that the server is verified, let's take a look at the process of server verification in detail.

HTTP defines five conditions and then verifies the header. The most important header for cache verification is

If-modified-since and if-none-match

Other condition headers are: if-unmodified-since, if-range, and if-match.

If-modified-since and last-modified are used together. The time returned by the server is in last-modified. When verification is performed, use if-modified-since to bring the time back to the server.

If-none-match and e-tag are used together. The tag returned by the server is in the e-tag. During verification, if-none-match is used to bring the tag back to the server for verification.

The verification process is as follows:

In most cases, we use if-modified-since, that is, it is enough to verify with the modification time, but in some cases, therefore, the HTTP protocol is similar to e-tag. Simply put, if the file needs to use the same file periodically, the modification time cannot meet the requirements.

The following describes the situations where last-modified cannot be used for verification,

  • Some documents may be repeated periodically. The content has not changed, but the modification date has changed.
  • Some documents have been modified, but the modifications are not important and do not need to be reloaded.
  • Some servers cannot accurately determine the last modification time of their pages.
  • Documents provided by some servers will change between sub-seconds, so that the last-modified provides inaccurate verification in seconds.
Differences and connections between Age and Max-Age in the http Response Header

Speaking of this, we need to start with the origin of Age header, and then have to talk about the method of using the user proxy to calculate the cache.

First, whether the cache expires depends on two values:Life CycleAndFreshness Value

Age comes from the calculation of life cycle. Let's first look at the calculation method of life cycle:

The details of HTTP computing during use are tricky, but the basic concept is simple. When the response arrives at the cache, you can use the Date header or Age header to check the time used for the response. The cache can also record the time when the document stays in the local cache. Adding these values is the total validity period of the cache.

1. Surface Use Period

$apparent_age = max(0,$time_got_response - $Date_header_value);

2. Skip-to-use period

Because the local time is not necessarily accurate, there will certainly be errors in the use period. HTTP/1.1 will allow each device to accumulate the relative validity period to the Age header, so as to solve the problem of lack of universal synchronous clock. This method does not require cross-server, end-to-end clock comparison.

In addition to the Age calculated based on Date, the relative Age value is also used, and the calculated Age value may be underestimated, regardless of the Date value across servers, therefore, the most conservative two Age values will be used.

$apparent_age = max(0,$time_got_response - $Date_header_value);$corrected_apprent_age = max($apparent_age, $Age_header_value);$response_delay_estimate = $time_got_response - $time_issued_request;$age_when_document_arrived_at_our_cache = $corrected_apparent_age + $response_delay_estimate;

3. Compensation for network latency

The browser calculates the difference between the cache time of the browser request and the time when the browser receives the cache to compensate for the network latency.

$apparent_age = max(0,$time_got_response - $Date_header_value);$corrected_apprent_age = max($apparent_age, $Age_header_value);$age_when_document_arrived_at_our_cache = $corrected_apparent_age;

4. complete Use Period Calculation

Focus on 2nd points, Skip-to-use period computing,Age is used to accumulate the time spent in each device.This value only appears in requests with multi-level caching. You have to mention it. The cache also has a topological structure and may have hierarchical caching and sibling caching, but this is not the focus of this article, if necessary, learn about it on your own.

While max-age, this value is used in cache-control: max-age: 600

In seconds, this parameter is used to control the cache freshness. When cache-control: max-age: 600 is set, the cache can have a s freshness period. In general, if the cache is used directly within this period of time, the server needs to initiate a verification request beyond this period of time.

By the way, this value is not recommended for expires, because the expires value is a specific time point, because the client time may be inconsistent with the server, which may lead to inaccurate caching.

Will the cache be verified after expiration?

To learn about this problem, we need to understand the cache-control values.

  • Cache-control: max-stale

Expired files can be used at will in the cache.

  • Cache-control: max-stale = <s>

An expired cache can be used in seconds at most.

  • Cache-control: min-refresh = <s>

Keeping documents fresh in at least the next second will tighten the cache limit.

  • Cache-control: no-store

No-store will directly Delete the modified cache object.

  • Cache-control: no-cache

No-cache indicates that the cache is usable, but it must be verified before use.

  • Cache-control: must-revalidate

Must-revalidate indicates that each time the cache is used, verification is required.

  • Cache-control: max-age

It is recommended to set the cache freshness in seconds.

  • Expires

An absolute time. Set the cache to expire at that time. Expires is not recommended because many servers do not synchronize time.

As you can see, even if the cache expires, you can use max-stale to forcibly use the expired cache. Of course, even if the cache does not expire, you can also use min-refresh to forcibly disable the use.

Cache-Control: no-cache; and Cache-Control: max-age: 0;

With the foreshadowing of the previous question, this is a good answer. Cache-Control: max-age: 0; only indicates that the current Cache has expired. By default, the browser will verify it again. But this is not absolute. In some cases, an expired Cache may be used instead of verification. You can write Cache-Control: max-age: 0, max-stale = 50 ;, the expired cache will be used at this time. The Cache-Control: no-cache indicates that the Cache is not allowed unless the resource is verified. So strictly speaking, Cache-Control: max-age: 0, must-revalidate; means the same as Cache-Control: no-cache.

Well, here are some of the items about caching. Of course it's not all about.Cache Topology,Cache freshness Value Calculation,Weak cache verification and cache tentative expirationIf you are interested, you can continue to read other documents.


In-depth analysis of Baidu Post bars should be objective

What does it mean? In-depth analysis !!!
 
NOD32 deep analysis

I also use
In-depth analysis seems to be a comprehensive inspection of viruses, including cache areas.
An error occurred while enabling PAGEFILE. SYS.
It should be the system cache file, it is a virtual file, so an error occurs
It's okay.

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.