Posts tagged with “Output Buffering”

Simple PHP Caching Using Output Buffering

I’ve worked on quite a few PHP projects recently, and all of them have required some form of caching. From working with each, I’ve come up with a pretty efficient method for caching code using PHP’s output buffering. It ends up being really quick and super flexible.

Output Buffering Basics

Output buffering is a pretty simple concept: instead of letting PHP return data to your user’s browser, you capture it and store it in a “buffer,” and you can decide what to do with it. Here’s a simple example:

ob_start();
echo "Hello!  This is buffered.";
$buffer = ob_get_clean();

Let’s go through the code line-by-line. The first line calls ob_start() (docs), which starts output buffering. The next line normally would be sent to the browser. Instead, since I called ob_start(), it gets stored in our buffer. The third line takes the current buffer and assigns it to the $buffer variable and stops the current buffer, all using the ob_get_clean() function (docs). It’s really simple stuff, and it becomes very powerful when used correctly.

How Caching Will Work

For this post, I’m going to be caching a simple API, and the general process will work like this:

  1. A user makes an API call, something like http://mysite.com/api/?method=myapp.search&type=people&query=Kyle
  2. If a cache file exists for the call, and it is younger than 15 minutes, skip to #6.
  3. Start buffering PHP’s output.
  4. Run the code to process the request.
  5. Save the contents of the buffer to a file, with a unique filename.
  6. Return result to the user.

Where Cached Output Will Be Saved

To save the output, I’m going to be creating a file for each unique request. For this application, the request will be unique based on the GET parameters passed. To do this, I’ll be creating an MD5 hash of an alphabetical list of GET keys and values. Here’s the function:

function cache_key() {
  $keys = array();
  foreach($_GET as $key => $value) {
    $keys[] = $key . "=" . $value;
  }
  sort($keys);
  return md5(implode('&', $keys));
}

function cache_filename() {
  globals $cache_dir;
  return $cache_dir . '/' . cache_key() . '.cache';
}

Please note that this will have to be customized based on what exactly you’re caching. For instance, if you’re caching individual pages, you may want to create the key using the path to the page. Whatever it is you’re using, just make sure it is unique and consistent for each page.

Checking The Cache

When a request is made, it’s necessary to first check to see if it has already been cached, and, if it has, whether the cache hasn’t expired. I’ll be using the filesystem to achieve this:

$cache_time = 15*60; // 15 minutes in seconds

function cache_exists() {
  globals $cache_time;

  if(@file_exists(cache_filename()) && time() - $cache_time < @filemtime(cache_filename())) {
    return true;
  } else {
    return false;
  }
}

So what exactly is going on here? If you take a close look, we’re using the file_exists() (docs) and filemtime() (docs) functions to see if the cache file already exists and, if it does, whether it’s recent enough to serve (in this case, if it’s less than 15 minutes old, the function returns true). I’m placing @ signs before these two functions so that, if they fail, it doesn’t return an error. Instead, the function will just return false and the code will run as if no cache file exists.

Putting It All Together

Now, it’s time to get everything working together. First, a couple of necessary functions for saving and reading the cache:

function read_cache() {
  return file_get_contents(cache_filename());
}

function save_cache($value) {
  $fp = @fopen(cache_filename(), 'w');
  @fwrite($fp, $value);
  @fclose($fp);
}

Now, a few calls to wrap around your code:

function start_cache() {
  if(cache_exists()) {
    echo read_cache();
    exit();
  } else {
    ob_start();
  }
}

function stop_cache() {
  $data = ob_get_clean();
  save_cache($data);
  echo $data;
}

And to implement it, this is all you need to do:

start_cache();
// Your code that needs to be cached
stop_cache();

And you’re done! All-in-all, it’s a very simple way to achieve a very powerful result.

Drawbacks to This Method

The first thing you want to keep in mind when using this caching method is that it caches the entire page. This can be good: if everyone visiting the page sees the same content anyways, why not cache it for everyone? However, if you’re serving a page that appears different to different users, it can be a bad idea. For instance, what if an administrator visits the page, and it gets cached? When the next non-administrator visits, they’re going to see all the administration information. Bad news.

Also, due to the simplicity of this method, there’s no way to easily expire the cache of a single page. Let’s go back to the blog entry example. If you decide to make a change to the entry, you’ll have to wait at least 15 minutes before the cache is cleared, or you have to go in and delete all the cache files (since it’s difficult to determine which file goes with which page). For many applications, this probably won’t an issue, but it’s something to keep in mind.

Download the Source

Hopefully, this was helpful. If you’d like to download the entire source, you can grab it here.

Posted on September 16, 2008 11 Comments
Tagged with: , , , , , ,