Home >Backend Development >PHP Tutorial >PHP Development Guide: Detailed Caching_PHP Tutorial
We can use meta tags in HTML pages and headers in PHP programs to control. For example:
The code is as follows
Header('Cache-Control:max-age=86400, must-revalidate');//24 hours
Header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
Header('Expires:'.gmdate('D, d M Y H:i:s', time() + '86400').'GMT');
echo 'I don't refresh'; write another HTML file c.htm:
Haha, go
We request 127.1/c.htm, click the link, then use the browser's back button to return to c.htm, and then click the link, as shown on the left side of the figure below. We found that after adding the cache directive, we How to go back and forth and click on the link? The following network request URL is always gray, which means that the browser does not initiate an actual network request, but directly calls the cached page stored in the user's computer, unless the cache time expires. Here During this period, even if the actual content changes, the browser will not re-read our resources on the server. You can modify the echo sentence and click again. You will find that the network request is still gray. In this case, the browser will update the cache only in the following three situations:
(1) Cache expiration
(2) The cache is cleared;
(3)F5 or ctrl+F5 forces a refresh. (There may be differences in the processing of various browsers. My test environment is Firefox 4)
When we block the header directive above or use the following code instead // tell the client browser not to use caching, HTTP 1.1 protocol
The code is as follows
header("Cache-Control: no-cache, must-revalidate");
// Tell the client browser not to use cache and be compatible with HTTP 1.0 protocol
The code is as follows
header("Pragma: no-cache");
The browser will read the resource from the server every time it is requested. As shown in the picture on the right below. Here, we can have a general understanding of the browser's caching of pages. It's easy for us to think of its usage scenarios. Sometimes we need it to be cached, and sometimes we don't. You can use it according to the scenario. However, the role of browser caching is very small, but it can save you a lot. Sometimes, the browser's cache is very powerful and requires us to force refresh to take effect, such as JS files. Sometimes if you change it, it will not change even if you refresh it. In addition, in addition to setting the page cache in the code, it can also be configured on servers such as APACHE, especially for static resources. Caching can effectively reduce unnecessary requests.
Sometimes, we usually see code like this. The string of question marks after the JS file has no practical meaning. It is just to avoid JS being cached and add a version number to the JS file, usually with a timestamp. Make a mark. This way the browser will not cache our JS files.
Since the purpose of front-end page caching is to reduce requests, I immediately thought of another idea, which is to compress data. By using some tools, CSS and JS code can be compressed. For example, the user version of jquery is compressed, and the compression ratio is often very large. Usually jsmin and jspacker are used for compression. You can search online for specific tools.