Speed Up Your Site with Simple Caching in PHP

Published 6th December 2009

After reading David Walsh’s “Retrieve, Cache, and Display Your FeedBurner Subscriber Count” post I was inspired to expand on the example he had created and created my own simple PHP caching class. To be honest I was amazed at the results. So I’ve decided to share with you what I’ve done and how I did it.

The PHP Class

First off we are going to start with good coding practice and make a PHP class. This makes for reusable code so here it is:

<?php class SimpleCache { //Path to cache folder (with trailing /) var $cache_path = 'cache/'; //Length of time to cache a file in seconds var $cache_time = 3600; function set_cache($label, $data) { file_put_contents($this->cache_path . $label .'.cache', $data); } function get_cache($label) { if(file_exists($this->cache_path . $label .'.cache') && (time() - $this->cache_time cache_path . $label .'.cache'))) { return file_get_contents($this->cache_path . $label .'.cache'); } return false; } //Helper function for retrieving data from url function fetch_data($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); $content = curl_exec($ch); curl_close($ch); return $content; } } ?>

This is just a simple class with three functions and two variables. The two variables simply hold a path to your “cache” folder and a period of time for your files to be cahed (3600 second is one hour by the way). The functions do the following:

  • set_cache($label, $data) — This function simply writes the $data to a file using the $label provided. We use labels here so that there is no file interaction outside the class (later in the tutorial). What I mean by that is we don’t want people to have to worry about file extensions and writing and reading and all that. Just set the path to the “cache” folder and that’s it.
  • get_cache($label) — This function does most of the work but is still very simple. Basically if a cache file exists for the given $label and the file is not “out of date” then we read the contents from the file. Simple.
  • fetch_data($url) — You don’t need to worry much about this helper function other than to know it uses cURL to fetch data from API’s (see below).

How to use the Class

So on to using the class. Basically a simple API call looks something like the following:

require('simpleCache.php'); $cache = new SimpleCache(); if($data = $cache->get_cache('some_api')) { echo = $data; } else { $data = $cache->fetch_data("http://some.api.url/?id=username"); $cache->set_cache('some_api', $data); echo = $data; }

As you can see in the demo we make API calls to Feedburner and Twitter. This is a simple process. I’m not going to explain what is happening here as it is fairly straightforward. What’s more interesting is the benefits of using such a simple class. Just check out the demo to see the kind of time difference between cached and un-cached page loads.


So there you have it. Some simple PHP caching and a nice reusable class to go away and play with. As always let me know what you think, if the script can be improved and if you find it useful and use it anywhere.