HTTP protocol 4 Cache--Go

Source: Internet
Author: User
Tags browser cache

The HTTP protocol provides a very powerful caching mechanism, and understanding of these caching mechanisms is very helpful for improving the performance of your Web site.

The concept of caching

Caching this thing is really ubiquitous, there are browser-side caches, server-side caches, proxy server caches, ASP. NET page cache, object cache. The database also has caches and so on.

The caching feature in HTTP is the browser cache and the cache proxy server.

http caching means that when a Web request arrives at the cache, it can be extracted from the local storage device instead of from the original server if there is a locally cached copy.

Benefits of Caching

The benefits of caching are obvious, the benefits are,

1. Reduce the redundant data transfer and save the network fee.

2. Reduce the burden on the server, greatly improve the performance of the website

3. Speed up the loading of Web pages for clients

Fiddler can easily view the cached header

Fiddler the headers are grouped together so that they can be easily viewed.

Request

Cache-control:max-age=0 In units of seconds
If-modified-since:mon, 08:38:01 GMT The last modified time of the cached file.
If-none-match: "0693f67a67cc1:0" The ETag value of the cache file
Cache-control:no-cache Do not use cache
Pragma:no-cache Do not use cache

Response

Cache-control:public The response is cached and shared among multiple users (the difference between public and private caches, see another section)
Cache-control:private The response can only be used as a private cache and cannot be shared among users
Cache-control:no-cache Remind the browser to extract documents from the server for validation
Cache-control:no-store Absolute Forbidden Cache (for confidential, sensitive files)
Cache-control:max-age=60 Cache expires after 60 seconds (relative time)
Date:mon, 08:39:00 GMT The current time response sent
Expires:mon, 08:40:01 GMT Time the cache expires (absolute time)
Last-modified:mon, 08:38:01 GMT Last modified time for server-side files
ETag: "20b1add7ec1cd1:0" ETag value for server-side files

What if there are cache-control and expires at the same time?
Browsers always prefer to use Cache-control, if there is no cache-control to consider expires

How to determine the freshness of a cache

The Web server determines whether the browser cache is up-to-date in 2 ways.

First, the browser tells the Web server the last modification time of the cache file through the header "If-modified-since".

Second, the browser will cache the file's ETag, through the header "If-none-match", to tell the Web server.

Determine cache freshness by last modified time

1. The browser client wants to request a document, first checks the local cache, discovers the existence of this document cache, gets the last modification time of the cached document, by: If-modified-since, sends the request to the Web server.

2. The Web server receives the request to change the server's document modification time (last-modified): Compared to the if-modified-since in the request header, if the time is the same, the cache is still up to date, The Web server will send 304 not modified to the browser client, telling the client to use the version in the cache directly. Such as.

3. If the document has been updated. The Web server sends the latest version of the document to the browser client, such as.

Example: Open fiddler, then open the blog home page. Then F5 refreshes the browser several times. You will see the blog home page also uses the cache.

ETag

An etag is an abbreviation for an entity tag that identifies the state of a resource based on a hash string generated by the entity content. The ETag also changes as the resource sends changes.

The etag is generated by the Web server and then sent to the browser client. The browser client does not care about how the ETag is generated.

Why use the ETag? Mainly to solve some problems that last-modified cannot solve.

1. Some servers do not accurately obtain the last modification time of the file, so that the last modification time will not be able to determine whether the file is updated.

2. Some files are modified very frequently, and are modified within the time of the second. Last-modified can only be accurate to seconds.

3. The last modification time of some files has changed, but the content has not changed. We do not want the client to think that this file has been modified.

instance, open fiddler, open the blog home page. You can see a lot of pictures, or the CSS files are cached. This is done by comparing the ETag value to determine if the file is updated.

Browser does not use cache

Ctrl+f5 force the browser to refresh, or set ie. You can let the browser not use the cache.

1. The browser sends an HTTP request to the Web server with Cache-control:no-cache in the header. Explicitly tell the Web server that the client does not use the cache.

2. The Web server will send the latest documents to the browser client.

Instance:

Open fiddler, open the blog home page, and press Ctrl+f5 to force refresh the browser, you will see

The role of Pragma:no-cache is identical to that of Cache-control:no-cache. are not using the cache.

Pragma:no-cache is defined in HTTP 1.0, so in order to be compatible with HTTP 1.0. So Pragma:no-cache and Cache-control:no-cache are used at the same time.

use cache directly, do not go to server authentication

Press F5 to refresh the browser and enter the URL in the Address bar and then enter. These two behaviors are not the same.

Press F5 to refresh the browser, and the browser will go to the Web server to verify the cache.

If you enter a URL in the address bar and then enter, the browser will "directly use a valid cache" without sending an HTTP request to the server to verify the cache, which is called a cache hit

Example: Compare the first visit to the blog Park home page and the second blog Park home page

1. Start fiddler, use Firefox to open the blog Park home page, found that there are more than 50 session.

2. Press Ctrl+x to delete all sessions in the Fiddler.   Close Firefox, reopen a Firefox, open the blog home page. Found only more than 30 sessions.

The missing session is because Firefox uses the cache directly, without sending an HTTP request.

how to set IE not to use cache

Open ie. Click on the toolbar, Tools->internet options, general, and browse history settings. Select "Never". and then save.

Then click "Delete" to delete the temporary Internet files (ie cached files are temporary Internet files).

the difference between public and private caches

Cache-control:public refers to a public cache, which can be shared by thousands of users.

Cache-control:private means that only private caches are supported, and private caches are private to individual users.

HTTP protocol 4 Cache--Go

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.