Php Development Guide: cache detail_php tutorial

Source: Internet
Author: User
Php Development Guide: cache details. We can use the header in the HTML page to control it by using the metatag and PHP programs. for example, the code is as follows header (Cache-Control: max-age86400, must-revalidate ); 24-hour header (Last-Mo, we can use the meta tag and PHP program in the HTML page to control through the header. 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 () + '000000'). 'gmt ');

Echo 'I will not refresh 'require and then write another HTML file c.htm:

Haha, go

We requested 127.1/c.htm, clicked the link, and then used the back-to-back button of the browser to return to c.htm, and then click the link, as shown on the left. we found that when the cache command is added, we will go back and click the link no matter how we do it, the following network request URL is always gray, indicating that the browser did not initiate the actual network request, but directly called the cache page stored in the user's computer, unless the cache time expires, 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 then click it to find that the network request is still gray. In this case, the browser updates the cache only in the following three cases:

(1) cache expiration

(2) the cache is cleared;

(3) F5 or ctrl + F5 force refresh. (The processing of various browsers may be different. my test environment is firefox 4)

When we block the preceding header command or use the following code, // tell the client browser not to use the cache, HTTP 1.1 Protocol

The code is as follows:

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

// Tells the client browser not to use the cache and is compatible with the HTTP 1.0 Protocol

The code is as follows:

Header ("Pragma: no-cache ");

The browser reads resources from the server each time it requests. As shown in the figure on the right. Here, we can get a general idea of how the browser caches pages. It is easy for us to come up with a scenario. sometimes we need it for caching and sometimes it is not needed. you can use it based on the scenario. However, the cache function of the browser is very small, but it can save a little bit. Sometimes, the browser cache is very powerful, and we need to force refresh to take effect, such as JS files, sometimes you change, even if the refresh is not changed. In addition to setting page cache in the code, you can also configure it on servers such as APACHE, especially static resources. adding cache can effectively reduce unnecessary requests.

Sometimes, we usually see such code. the string that is followed by the question mark in the JS file does not actually make sense, just to prevent JS from being cached and add a version number to the JS file, it is usually marked with a timestamp. In 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, that is, compressing data. Some tools can be used to compress CSS and JS code. For example, the jquery version is compressed, and the compression ratio is usually large. Jsmin and jspacker are usually used for compression. specific tools can be searched online.

The authorization tag and PHP program are controlled through the header. for example, the code is as follows: header ('cache-Control: max-age = 86400, must-revalidate'); // 24-hour header ('last-Mo...

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.