I have introduced browser caching to you before, and also introduced HTML offline caching. It is easy to get confused if I talk too much about it. Today I will tell you the difference between these caches and how to use the cache.
200 from memory cache does not access the server, directly reads the cache, and reads the cache from the memory. The data at this time is cached in memory. After the process is killed, that is, after the browser is closed, the data will no longer exist. However, this method can only cache derived resources
200 from disk cache without accessing the server, reading the cache directly, and reading the cache from the disk. When the process is killed, the data still exists. This method can only cache derived resources.
304 Not Modified Access the server and find that the data has not been updated. The server returns this status code. Then read the data from the cache.
Principle of three-level cache
Go to the memory first, if there is it, load it directly
If there is no memory, select the hard disk to get it, if there is it, load it directly
If there is no hard disk, then make a network request
The loaded resources are cached on the hard disk and memory
General browsingPictures, the process is as follows:
Access-> 200 -> Exit the browser
Come in again-> 200(from disk cache) -> Refresh-> 200(from memory cache)
application cache It is a little different from the cache above. It is offline cache, which means that resources can be read from the hard disk without being connected to the Internet. Even if the Internet is disconnected, users can browse.
Set browser cache
304 Is it necessary to negotiate the cache or communicate with the server once? If you want to cut off server communication, you must force the browser to use local cache (cache-control/expires),
Generally, there are several ways to set browser cache.
1. Set expires and cache-control through HTTP META
<meta http-equiv="Cache-Control" content="max-age=7200" /> <meta http-equiv="Expires" content="Sun Oct 15 2017 20:39:53 GMT+0800 (CST)" />
Written in this way, it is only valid for the web page, and is invalid for pictures or other requests in the web page.
2. Apache server configuration pictures, css, js, flash cache
This technology is mainly implemented through server configuration. If you use an apache server, you can use the mod_expires module to implement:
Compile the mod_expires module:
Cd /root/httpd-2.2.3/modules/metadata/usr/local/apache/bin/apxs -i -a -c mod_expires.c //编译
First open the httpd.conf file, and then search for the expires module. After finding it, delete the # sign on the left to indicate that this module is enabled and restart the apache server
Edit httpd.conf configuration: add the following content
<IfModule mod_expires.c>ExpiresActive on ExpiresDefault "access plus 1 month" ExpiresByType text/html "access plus 1 months" ExpiresByType text/css "access plus 1 months" ExpiresByType image/gif "access plus 1 months" ExpiresByType image/jpeg "access plus 1 months" ExpiresByType image/jpg "access plus 1 months" ExpiresByType image/png "access plus 1 months" EXpiresByType application/x-shockwave-flash "access plus 1 months" EXpiresByType application/x-javascript "access plus 1 months" #ExpiresByType video/x-flv "access plus 1 months"</IfModule>
3, php and other settings
<?php header("Cache-Control: public"); header("Pragma: cache"); $offset = 30*60*60*24; // cache 1 month $ExpStr = "Expires: ".gmdate("D, d M Y H:i:s", time() + $offset)." GMT"; header($ExpStr);?>
or
$seconds_to_cache = 3600;$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";header("Expires: $ts"); header("Pragma: cache");header("Cache-Control: max-age=$seconds_to_cache");
Front-end code deployment in cache condition
Question 1: With cache, how to update the front-end code?
We can add the version number after the resource file or picture, as shown below.
Question 2: But after all files have version numbers added, we only changed one file. Isn’t the cache of other files wasted?
To solve this problem, we can use the data Summary algorithm to obtain summary information for the file. The summary information corresponds to the file content one-to-one. As shown below:
This solves the problem.
Question 3: A new problem has arisen again, what should I do when publishing files?
1. Deploy the page first, then deploy the resources: During the time interval between the two deployments, if a user accesses the page, the old resources will be loaded in the new page structure, and the old version will be The resource is cached as a new version. The result is that the user accesses a page with a disordered style. Unless refreshed manually, the page will continue to execute errors until the resource cache expires.
2. Deploy resources first, then deploy pages: Within the deployment time interval, users with local cache of old version resources visit the website. Since the requested page is an old version, the resource citationNo change, the browser will directly use the local cache. In this case, the page will display normally; but if users without local cache or cache expired visit the website, the old version page will load the new version resource, resulting in page execution error. But when the page is deployed, these users will return to normal when they visit the page again.
Okay, what I want to say from the above analysis is: No one can deploy first! This will lead to page confusion during the deployment process. Therefore, for projects that do not have a large number of visits, the R&D students can work hard and secretly go online in the middle of the night. First upload the static resources and then deploy the page, which seems to have fewer problems.
How to solve these problems?
This problem originates from the overlay release of resources. This problem occurs when resources to be released are used to cover published resources. It is easy to solve it, that is, to implement non-coverage publishing
There are so many aspects about caching. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Related reading:
How to implement digital focus chart carousel code in HTML
In HTML How to deal with the incomplete display of the last line of text
How to use canvas to create the effect of particle fountain animation
The above is the detailed content of How to use front-end browser cache. For more information, please follow other related articles on the PHP Chinese website!

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor