HTTP protocol cache)

Source: Internet
Author: User

I have written an article [http protocol details]. This time I will continue to introduce the caching mechanism in HTTP. The HTTP Protocol provides a very powerful caching mechanism. Understanding these caching mechanisms is very helpful for improving website performance. This article describes how to deal with the problem between a browser and a Web server"Browser cache", AndControl the cached HTTP Header.

This document uses Fiddler to view HTTP request and response. If you are not familiar with this tool, refer to [fiddler tutorial]. When reading this article, be sure to open fiddler for practice.

Reading directory

    1. Concept of Cache
    2. Benefits of caching
    3. Fiddler allows you to conveniently view cached headers.
    4. How to determine cache freshness
    5. Determine the cache freshness through the last modification time
    6. Cache-related Headers
    7. Etag
    8. Browser does not use Cache
    9. Directly use the cache without server Verification
    10. How to Set IE to not use Cache
    11. Differences between public cache and private Cache

 

Concept of Cache

Caching is really everywhere. There are browser-side caching, server-side caching, proxy server Caching, ASP. NET page caching, and Object Caching. The database also has a cache, and so on.

Which of the following functions is available in HTTP?Browser cache, AndCache Proxy Server.

 

HTTP cache means that when a Web request arrives at the cache, if there is a "cached" Copy locally, the file can be extracted from the local storage device rather than from the original server.

Benefits of caching

The benefits of caching are obvious,

1. Reduced redundant data transmission and reduced network fees.

2. reduces the burden on servers and greatly improves website performance

3. Accelerated loading of web pages on the client

 

Fiddler allows you to conveniently view cached headers.

In Fiddler, all headers are put together in different categories to facilitate viewing.

 

How to determine cache freshness

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

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

Second, the browser tells the Web server the etag of the cached file through Header "If-None-match.

 

Determine the cache freshness through the last modification time

1. the browser client wants to request a document. First, check the local cache and find that the file is cached. Obtain the last modification time of the document in the cache, and use: If-modified-since, send a request to the web server.

2. when the Web server receives a request, the server's document modification time (last-modified) is compared with if-modified-since in the request header. If the time is the same, indicates that the cache is still up to date. The web server will send 304 not modified to the browser client, telling the client to directly use the version in the cache. For example.

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

 

Instance: Open Fiddler, and then open the blog homepage. Then F5 refreshes the browser several times. You will see that the blog garden homepage is also cached.

 

Cache-related Headers

Let's take a look at the specific meaning of each header.

Request

Cache-control: Max-age = 0 In seconds
If-modified-since: Mon, 19 Nov 2012 08:38:01 GMT The last modification time of the cached file.
If-None-Match: "0693f67a67cc0: 0" The etag value of the cached file.
Cache-control: No-Cache No Cache
Pragma: No-Cache No Cache
   

 

 

 

 

 

Response

Cache-control: Public The response is cached and shared among multiple users., (For the difference between public cache and private cache, see another section)
Cache-control: Private Responses can only be used as private caches and cannot be shared among users.
Cache-control: No-Cache Remind the browser to extract documents from the server for verification
Cache-control: No-store Never cache (for confidential and sensitive files)
Cache-control: Max-age = 60 Cache expiration after 60 seconds (relative time)
Date: Mon, 19 Nov 2012 08:39:00 GMT Current response sending time
Expires: Mon, 19 Nov 2012 08:40:01 GMT Cache expiration time (absolute time)
Last-modified: Mon, 19 Nov 2012 08:38:01 GMT Last modification time of server files
Etag: "20b1add7ec1cd1: 0" Etag value of the Server File

 

 

 

 

 

 

 

 

 

 

What if both cache-control and expires exist?
The browser always takes precedence over cache-control. expires is considered only when cache-control is not available.

 

Etag

Etag is the abbreviation of an entity tag. It identifies the status of a resource based on a hash string generated by the entity content (similar to the result after MD5 or sha1. When the resource sends a change, the etag also changes.

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

Why is etag used? It is mainly used to solve some problems that last-modified cannot solve.

1. Some servers cannot accurately obtain the last modification time of the file. In this way, the last modification time cannot be used to determine whether the file is updated.

2. Some files are frequently modified and can be modified in seconds or less. 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 this file has been modified.

 

Instance, open Fiddler, and open the blog home page. You can see that many images or CSS files are cached. You can compare the values of etag to determine whether the file is updated.

 

Browser does not use Cache

CTRL + F5 force refresh the browser or set IE. Allows the browser not to use the cache.

1. the browser sends an HTTP request to the Web server, with the header containing cache-control: No-Cache. It explicitly tells 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 the same as that of cache-control: No-cache. Cache is not used.

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

 

Directly use the cache without server Verification

Press F5 to refresh the browser, enter the URL in the address bar, and press Enter. These two actions are different.

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

If you enter the URL in the address bar and press enter, the browser will "directly use the valid cache" instead of sending an HTTP request to the server to verify the cache. This is called cache hit, as shown in

 

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

1. Start fiddler and use Firefox to open the blog homepage. More than 50 sessions are found.

2. Press Ctrl + X to delete all sessions in Fiddler. Close Firefox, re-open a Firefox, and open the blog homepage. Only over 30 sessions are found.

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

 

 

How to Set IE to not use Cache

Open IE. On the toolbar, choose tools> Internet Options> General> browsing history settings. Select "never ". Save the settings.

Click "delete" to delete all Temporary Internet Files (ie cached files are Temporary Internet Files ).

 

Differences between public cache and private Cache

Cache-control: Public indicates that a public cache can be shared by thousands of users.

Cache-control: Private indicates that only private cache is supported. Private cache is dedicated to a single user.

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.