PHP static Cache Simple Production

Source: Internet
Author: User

The purpose of the cache is to make our pages run faster, reduce the number of read database content, give users a better experience, so we can make our own program cache, and set a cache expiration time to ensure consistency with the database, of course, not all programs are suitable for caching, Here is a brief introduction to the method of static cache production of PHP:

1. First of all we have to set a cache file location, that is, storage directory

For example, I store it in the cache directory, and all future cache files are placed in that directory.

2. In the page where you want to do a static cache, we define a variable to hold the path to the cache file (relative to the path of the current page)

$filename = "./cache/test.html";

3. Define a variable to store the cache expiration time

$cachetime = 5;

4. Determine if the cache file exists or whether it expires, if the cache file does not exist in the execution of the source code generation cache, or time expired should also be re-cached, if the above conditions are not met the call cache page display.

if (!file_exists ($filename) | | filemtime ($filename) + $cachetime <time ()) {//Cache page code}else{include ($filename);//If present , call cache file}

5. Generate the cache, before the page output content, we call the Ob_start () method to open the memory buffer, the contents of the output will be put into memory.

Ob_start ();

6. After the page finishes outputting, save the contents of the memory to a static page.

Fetch the page code from the memory cache $content = Ob_get_contents ();   Store the acquired content into the cache file File_put_contents ($filename, $content);    Clear Memory cache Ob_flush ();

7. Finally we add a section of the output to test whether the cache is re-cached, if the page has output that content represents the re-generated cache file, if not, the description is directly read the cache file

echo "######################################";  Test whether the cache file is called and the cache file does not output this sentence

So we simply do a static cache, the complete code is as follows:

<?php$filename = "./cache/test.html";//define cache validity $cachetime = 5;//Determine if the cache file exists if (!file_exists ($filename) | | filemtime ( $filename) + $cachetime <time ())//filemtime ($filename) Gets the file modification time, plus the defined cache time is less than the current time {//Open memory Cache Ob_start (); ><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

Finally look at the effect:

The first time you execute the page, the cache appears:

Once again, the cache is not re-established, but the static page test.html is read directly:

After a while, the cache is regenerated, which is the cache expiration time we set earlier.

PHP static Cache Simple Production

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.