Easy Caching with StashPHP

So short post this time, I swear I’m not dead, just busy!

Frequently with PHP you are going to need to cache things, mostly expensive SQL queries, but also data you aren’t going to want to be inserting into the database on every page hit, for instance website statistics.

With PHP we have a few options to achieve this;

  1. Caching with the file system.
    •  Upsides:
      • Works well with the Opcode cache
      • Usually the fastest method of caching for small or medium websites
    • Downsides:
      • Clearing the cache can be a lot slower as you will have to recursively search through path’s and delete.
  2. Caching with SqlLite
    1. Upsides:
      • Can be substantially faster than a full-blown RDBMS
      • All data is stored in a normal file in the host’s file system.
    2. Downsides:
      • Can only support one writer at a time, which can cause high file system latency, which is inconvenient if there are many clients trying to access it simultaneously.
  3. Caching with APC
    1. Upsides:
      • Makes PHP faster for you through the so called opcode caching.
      • No special configuration required.
    2. Downsides:
      • Practically none
  4. Caching with Memcached
    1. Upsides:
      • Allows machines to pool their memory together as one large memory cache, perfect for large websites.
      • Cross platform and cross RDBMS
    2. Downsides:
      • Stores data in the RAM, not ideal for small systems
      • Is considered to be a volatile in-memory key/value store
  5. Caching with Redis
    1. Upsides:
      1. Can act like memcached as a key/value store however it’s really a data structure server.
      2. Persistence to disk, by default.
      3. Values up to 512MB in size
      4. Built in clustering
      5. Extremely fast at everything
    2. Downsides:
      1. The more objects you put in it, the more memory its going to use.

So as you can see there are a bunch of different systems that handle caching in arguably better or worse ways depending on how big your website is. Putting a small website on Redis is probably overkill,  you might already have set up a RDBMS solution and now not want to change to a key value store etc.

This is where StashPHP comes in, you basically use the StashPHP library to cache things like so:

First you setup the driver to use, lets just use File System for the moment:

<?php // Create Driver with default options $driver = new Stash\Driver\FileSystem(); $driver->setOptions(array());

// Inject the driver into a new Pool object.
$pool = new Stash\Pool($driver);

Now you can setup your by wrapping the following code around your code:

<?php // Get a cache item. $item = $pool->getItem('path/to/item');

// Attempt to get the data
$data = $item->get();

// Check to see if the data was a miss.
    // Let other processes know that this one is rebuilding the data.

    // Run intensive code
    $data = codeThatTakesALongTime();

    // Store the expensive to generate data.

// Continue as normal.

Now rather than later on when you decide to add another cache, rather than needing to go rewrite all your caching calls etc. you can just change the setup of the drivers like so:

<?php $subDrivers = array(); $subDrivers[] = new Stash\Driver\Apc(); $subDrivers[] = new Stash\Driver\FileSystem(); $subDrivers[] = new Stash\Driver\Memcached(); $options = array('drivers' => $subDrivers);
$driver = new Stash\Driver\Composite($options);

$pool = new Stash\Pool($driver);

Presto, we’ve just avoided a nightmare!