Memcached for WordPress on Ubuntu 14.04

If you’re unsure of what an object cache is, or what memcached is, your first port of call should be Scotty’s article on the subject.

WordPress has built-in support for a PHP object cache. However, it only exists temporarily – for a single page load. The best way to explain this is to think about options. When a page loads, WP loads all options from the database (technically all ‘autoloaded’ options, which is the default). Any later request for get_option() will call from the in-built object cache meaning no more hits to the database.

However, if you want this to be more persistent, you need to set up one of several solutions. One is setting up a memcached server. Sounds complicated. It kinda is…but, thanks to people who are much, much smarter than I am, the process of setting up memcached for WordPress is now simple. In order to get this working, you need to do two or three things;

  1. Install memcached on your server
  2. Place a config file in the correct location
  3. (sometimes) Adjust your wp-config.php file

Install memcached on Ubuntu 14.04

Easily the best tutorial on this is to be found on DigitalOcean’s wonderful forums. Follow the steps outlined in this tutorial to install memcached on your Ubuntu 14.04 installation.

Choose an object-cache file to use

As with almost anything WordPress, you have several options here. There is a plugin on the main repo that has been around for years that works well. However, from what I understand it doesn’t implement every method that is available to you and, well, frankly, I like being up-to-date.

The one I recommend is Zack‘s WordPress pecl memcached object cache.

You’ll need to download and copy the object-cache.php file to your wp-content directory.  (in the root of that directory, alongside your themes and plugins directories). This makes it a ‘drop-in’ plugin in WordPress parlance.

Update your wp-config.php file

Some solutions don’t require this step. However, if you’re following on from above, then you’ll need to add the following code to the bottom of your config file;

global $memcached_servers;
$memcached_servers = array(
        '', // Memcached server IP address
        11211        // Memcached server port

And you’re done!

Leave a comment

Your email address will not be published. Required fields are marked *