Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The principle of http caching technology

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "the principle of http caching technology", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the principle of http caching technology.

The principle of HTTP cache implementation

How the browser knows to use the cache, in fact, this is through the http, the browser will send the last modification time to the web server, the web server receives the request with the server of the document last modified time comparison, if the web server on the latest document modification time less than or equal to the browser sent, then send 304to the browser, using the cached version.

Concept of caching

Caching is really ubiquitous, with browser-side caching, server-side caching, proxy server caching, ASP.NET page caching, object caching. Databases also have caches, and so on.

The caching function in http is browser cache and cache proxy server.

Http caching means that when an Web request arrives at the cache, if there is a "cached" copy locally, the document can be extracted from the local storage device rather than from the original server.

Benefits of caching

The benefits of caching are obvious, including

1. The redundant data transmission is reduced and the network cost is saved.

two。 It reduces the burden on the server and greatly improves the performance of the website.

3. Speed up the loading of web pages by the client

Fiddler can easily view cached header

Header are grouped together in Fiddler to make it easy to view.

How to judge the freshness of cache

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

First, the browser tells the Web server when the cache file was last modified through header "If-Modified-Since".

Second, the browser tells the Web server the ETag of the cached file through header "If-None-Match".

The freshness of cache is judged by the last modification time.

1. If the browser client wants to request a document, first check the local cache, find that there is a cache of the document, get the last modification time of the document in the cache, and send Request to the Web server through: If-Modified-Since.

2. The Web server receives the Request and compares the document modification time (Last-Modified) of the server with the If-Modified-Since in 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. As shown in the following picture.

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

Example: open Fiddler, and then open the home page of the blog park. Then F5 refreshes the browser several times. You will see that the home page of the blog park also uses a cache.

Header related to caching

Let's 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 the last modification time of the GMT cache file. If-None-Match: "0693f67a67cc1:0" the Etag value of the cache file Cache-Control: no-cache does not use cache Pragma: no-cache does not use cache

Response

Cache-Control: public responses are cached and shared among multiple users (the difference between public and private caches, see another section) Cache-Control: private responses can only be used as private caches, cannot be shared among users Cache-Control:no-cache reminders browsers to extract documents from the server for verification Cache-Control:no-store absolutely forbids caching (for confidentiality (sensitive files) Cache-Control: cache expires after max-age=6060 seconds (relative time) Date: Mon, 19 Nov 2012 08:39:00 GMT current response sent time Expires: Mon, 19 Nov 2012 08:40:01 GMT cache expiration time (absolute time) Last-Modified: Mon, 19 Nov 2012 08:38:01 last modification time of GMT server-side files ETag: "20b1add7ec1cd1:0" server-side file Etag value

What if both cache-control and Expires exist?

Browsers always give priority to using cache-control. If there is no cache-control, consider Expires.

ETag

ETag is an abbreviation for entity tag (Entity Tag). A hash string generated based on the content of the entity (similar to the result after MD5 or SHA1) can identify the status of the resource. When a resource is sent to change, so does the ETag.

ETag is generated by the Web server and sent to the browser client. Browser clients don't have to worry about how Etag is generated.

Why use ETag? The main purpose is to solve some problems that cannot be solved by Last-Modified.

1. Some servers cannot accurately get the last modification time of the file, so it is impossible to determine whether the file has been updated by the last modification time.

two。 Some files are modified very frequently and are modified in less than a 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 don't want the client to think that this file has been modified.

Example, open Fiddler, open the home page of the blog park. You can see a lot of pictures, or CSS files are cached. These are to determine whether the file has been updated by comparing the values of ETag.

Browsers do not use caching

CTRL+F5 forces the browser to refresh, or sets IE. You can make browsers not use caching.

1. The browser sends 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 caching.

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

Example:

Open Fiddler, open the home page of the blog garden, then press CTRL+F5 to force the browser to refresh, and you will see

Pragma: no-cache does exactly the same thing as Cache-Control: no-cache. All do not use caching.

Pragma: no-cache is defined in HTTP 1. 0, so to be compatible with HTTP 1. 0. So both Pragma: no-cache and Cache-Control: no-cache are used

Use cache directly without server verification

Press F5 to refresh the browser and enter the URL in the address bar and enter enter. The two behaviors are different.

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

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

Example: compare the first visit to the blog park home page and the second blog park home page

1. Start Fiddler, open the blog garden home page with firefox, and find that there are more than 50 session.

two。 Press CTRL+X to delete all session in Fiddler. Close firefox, reopen a firefox, and open the blog Park home page. It was found that only more than 30 session.

Analysis; the reason for the lack of session is that firefox directly uses caching instead of sending http request.

How to set IE not to use caching

Open IE. Click tools-> Internet options-> General-> Browse History Settings on the toolbar. Choose "never". And save it.

Then click "Delete" to delete all Internet temporary files (the files cached by IE are Internet temporary files).

The difference between public cache and private cache

Cache-Control: public refers to a public cache that can be shared by thousands of users.

Cache-Control: private means that only private caches are supported, which are dedicated to a single user.

At this point, I believe you have a deeper understanding of "the principle of http caching technology". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report