PHP Cache Dynamic Pages To Speed Up Load Times

If your website receives a good amount of traffic everyday and your webpages are loading slow, you might want to consider implementing some sort of caching mechanism on your website to speed-up page loading time. Because as we all know that each client-server request consists of many queries, loops, calculations, database queries etc. these all add-up to processing time, which eventually increases page loading time. The most simplest way to avoid all these is to create cache files and store them in a separate directory, which can later be served as fast loading static pages instead of regular dynamic pages.

PHP Caching

There are several other PHP cache engines such as APC, Xcache or OPcache to boost your application performance, but they all work quite differently, to understand them you can find many tutorials dedicated to them on the web. Here I’m going to show you the most simplest way of caching PHP pages, and that is using PHP Output Buffer and Filesystem Functions, combining these two methods we can have magnificent caching system.

PHP Output buffer :— It interestingly improves performance and decreases the amount of time it takes to download, because the output is not being sent to browser in pieces but the whole HTML page as one variable. The method is insanely simple take a look at the code below :

1
2
3
4
5
6
7
<?php
ob_start(); // start the output buffer

/* the content */
ob_get_contents();  gets the contents of the output buffer
ob_end_flush(); // Send the output and turn off output buffering
?>

When you call ob_start() on the top of the code, it turns output buffering on, which means anything after this will be stored in the buffer, instead of outputting on the browser. The content in the buffer can be retrieved using ob_get_contents(). You should call ob_end_flush() at the end of the code to send the output to the browser and turn buffering off.

PHP Filesystem :— You may be familiar with PHP file system, it is a part of the PHP core, which allow us to read and write the file system. Have a look at the following code.

1
2
3
$fp = fopen('/path/to/file.txt', 'w');  //open file for writing
fwrite($fp, 'I want to write this'); //write
fclose($fp); //Close file pointer

As you can see the first line of the code fopen() opens the file for writing, the mode ‘w’ places the file pointer at the beginning of the file and if file does not exist, it attempts to create one. Second line fwrite() writes the string to the opened file, and finally fclose() closes the successfully opened file at the beginning of the code.

Implementing PHP caching

Now you should be pretty clear about PHP output buffer and filesystem, we can use these both methods to create our PHP caching system. Please have a look at the picture below, the Flowchart gives us the basic idea about our cache system.
PHP cache system in Action
The cycle starts when a user request the content, we just check whether the cache copy exist for the currently requested page, if it doesn’t exist we generate a new page, create cache copy and then output the result. If the cache already exist, we just have to fetch the file and send it to the user browser.

Take a look at the Full PHP cache code below, you can just copy and paste it in your PHP projects, it should work flawlessly as depicted in above Flowchart. You can play with the settings in the code, modify the cache expire time, cache file extension, ignored pages etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
//settings
$cache_ext  = '.html'; //file extension
$cache_time     = 3600;  //Cache file expires afere these seconds (1 hour = 3600 sec)
$cache_folder   = 'cache/'; //folder to store Cache files
$ignore_pages   = array('', '');

$dynamic_url    = 'http://'.$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING']; // requested dynamic page (full url)
$cache_file     = $cache_folder.md5($dynamic_url).$cache_ext; // construct a cache file
$ignore = (in_array($dynamic_url,$ignore_pages))?true:false; //check if url is in ignore list

if (!$ignore && file_exists($cache_file) && time() - $cache_time < filemtime($cache_file)) { //check Cache exist and it's not expired.
    ob_start('ob_gzhandler'); //Turn on output buffering, "ob_gzhandler" for the compressed page with gzip.
    readfile($cache_file); //read Cache file
    echo '<!-- cached page - '.date('l jS \of F Y h:i:s A', filemtime($cache_file)).', Page : '.$dynamic_url.' -->';
    ob_end_flush(); //Flush and turn off output buffering
    exit(); //no need to proceed further, exit the flow.
}
//Turn on output buffering with gzip compression.
ob_start('ob_gzhandler');
######## Your Website Content Starts Below #########
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Page to Cache</title>
    </head>
        <body>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer ut tellus libero.
        </body>
</html>
<?php
######## Your Website Content Ends here #########

if (!is_dir($cache_folder)) { //create a new folder if we need to
    mkdir($cache_folder);
}
if(!$ignore){
    $fp = fopen($cache_file, 'w');  //open file for writing
    fwrite($fp, ob_get_contents()); //write contents of the output buffer in Cache file
    fclose($fp); //Close file pointer
}
ob_end_flush(); //Flush and turn off output buffering

?>

You must place your PHP content between the enclosed comment lines, In fact I’d suggest putting them in separate header and footer file, so that it can generate and serve cache files for all the different dynamic pages. If you read the comment lines in the code carefully, you should find it pretty much self explanatory.

  1. Get the currently requested URL location.
  2. Construct a location path for the cache file, convert URL to MD5 hash for the fixed cache file name.
  3. Check whether URL is in ignore list.
  4. Check for existing unexpired cache file, if exist, just open and output the content with gzip compression.
  5. Or else, we create a new cache file and output the HTML result with gzip compression.

Conclusion

I hope this code will help you create your own cache system, but you must avoid caching in certain types of pages such as members area (after user logs in), search pages or a constantly updating homepage. You must also combine and compress JavaScripts, CSS and HTML to boots performance even more, and test your webpages using Google’s pageSpeed.

  1. Hi Steve,

    I want to use cache system in my list pages. But I have some dynmic fields like a vote buttons or ‘an hour ago’ etc.

    What should I do? I need logical behind more than codes. Thank u.

     Reply
  2. Hey! thanks for the code. It works great!

     Reply
  3. Thanks a lot for this great cache script!
    I visit your site often while there is a lot of interesting info for me.
    After putting this script into a page and testing it, the loading time decreased from about 400ms to 60ms!!!!!

     Reply
  4. Hey, this worked great, thank you so much for the code!!!

     Reply
    • Also, how do you flush the cache when you’ve updated the page (let’s say you’re doing testing) and want to see the updated page? Is there an easy part in the code to comment out? Thanks!

  5. thanks for this tutorial, very helpfull

     Reply
  6. How about image ? Is including too on cache file? My html content using a lot of images that make loading take long time.

     Reply
  7. hmmm :D thanks, its really help

     Reply
  8. if i a have a dynamic page which fetches 5 records from database.
    tomorrow someone adds the sixth record
    and i again hit the same url will it show me 5 records or 6 records ?
    what do you mean by this : if the Web developer manually edits the file

     Reply
    • All that is required of you is to set an appropriate cache time. In my case, I will be modifying the code, I can use an unlimited cache time, since all my content is moderated, and I shall be coding a cache purge upon authorizing a change to the site. You may consider this technique for yourself, if it is a special interest to your situation, on top of a cache time, as well.

Message Type : Question Comment ?
  • Question : Can include code, jsFiddle, codePen etc using Markdown Syntax.
  • Comment : Short comments and questions.