PHP Development Guide: Caching detailed

Source: Internet
Author: User

We can use the header to control the HTML page using the META tag and the PHP program. For example:

The code is as follows

Header (' cache-control:max-age=86400, Must-revalidate ');//24 hour

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 an HTML file c.htm:

Haha,go

We request 127.1/c.htm, click on the link, and then use the browser's fallback button, Return to C.htm, then click on the link, as shown on the left of the following figure, and we find that when caching instructions are added, we back and click on the link anyway, the following network request URL This is always gray, indicating that the browser did not initiate the actual network request, but directly to the user's computer stored in the cache page, unless the cache time expired, in this issue , even if the actual content changes, the browser will not be able to re-read our resources on the server. You can change the echo of the sentence, and then click, you will find that the network request is still gray. In this case, only the following three scenarios are available for browsers to update the cache:

(1) Cache expiration

(2) cache is cleared;

(3) F5 or ctrl+f5 force refresh. (This can be a different browser processing, my test environment is Firefox 4)

HTTP 1.1 protocol when we block the header command above or use the following code to tell the client that the browser is not using caching

The code is as follows

Header ("Cache-control:no-cache, must-revalidate");

Tells the client that the browser does not use caching, which is compatible with the HTTP 1.0 protocol

The code is as follows

Header ("Pragma:no-cache");

The browser reads the resources on the server on every request. As shown in the figure on the right of the following surface. Here, we can have a general understanding of the browser on the page cache processing. It's easy for us to think about the use of the scene, sometimes we need it to cache, sometimes it doesn't, you can use it according to the scene. However, the caching effect of the browser is very small, but it can save a little bit. Sometimes, the browser's cache is very powerful, we need to force the refresh to be effective, such as JS files, sometimes you change, even if the refresh will not change. In addition, you can set the page cache in code, you can also configure on servers such as Apache, especially static resources, plus caching can effectively reduce unnecessary requests.

And sometimes, we usually see such a code, in the JS file with the question mark of that string has no practical significance, just to avoid JS is cached, to the JS file plus a version number, usually marked with timestamp. So the browser will not cache our JS file.

Since the purpose of the front-end page caching is to reduce the request, I immediately think of another way to compress the data. By using some tools, you can compress CSS and JS code. For example, the use of jquery version is compressed, often this compression ratio is still very large. Usually use Jsmin,jspacker for compression, the specific tools can be searched online.

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.