WordPress Caching using APC and batcache

If you run a WordPress site on a single server or shared hosting service, this is the simplest (effective) caching setup I’ve found.

Using a Combination of batcache (full page caching used by WordPress.com) and the APC Object Cache Backend, we’ll get an effective caching setup by installing a few files and changing one line in wp-config.php

What this will do for you

Using this combination of Batcache and APC will give you full page caching so that repeated hits to the same page will only be dynamically rendered the first time. Subsequent hits will be served from cache. This is especially helpful for traffic spikes.

The cache expiration time is configurable, and being a simple blog, I have my cache expiration set for 1 week. This means that once someone hits a blog post, it isn’t rendered dynamically again for an entire week, and other visitors to the same post will get a speedy cached copy.

Install APC

The first thing to check to make sure that this will work for you is that you have APC (Alternative PHP Cache) installed. You can check this by loading up phpinfo();. It should be listed as an installed module.

If not, before you continue, verify that APC is installable in your environment.

If you are on your own server, check your distribution’s package repository.

If you are in a shared hosting environment, check with your host for the possibility of installing APC for your account.

Now that we’ve verified APC is installed, there are only a few steps to set this up.

Install APC Object Cache Backend

Download APC Object Cache Backend

Place object-cache.php in your wp-content/ directory.

Install Batcache

Download batcache

Place advanced-cache.php in your wp-content/ directory.

Optional - If you want to regenerate the homepage cache automatically when you make a new post, install batcache.php into your plugins directory and enable it in the WordPress Admin.

wp-config.php

Once all of this is in place you should go to your plugins page in the WordPress admin and click “Drop-ins” near the top to see these two plugins. You should get a warning about the WP_CACHE option not being set.

The last step to enable this is to add (or change) this
in your wp-config.php.

define(‘WP_CACHE’, true);

Once this is in place you should now see the caching stats as the last thing in the <head/> tag of your site, looking something like this.

Configuring batcache

There are a few simple things you can do to control how batcache operates. By default, batcache stores cache for 300 seconds ( 5 minutes ), so anyone requesting a page that someone else has requested in the last 5 minutes, will get the cached copy. Preventing that pageview from generating database traffic and php rendering.

This and many other options can be changed from their defaults near the top of the advanced-cache.php file.

This file is well commented so look at it’s source for more direction on how to fine tune it to your sites profile and traffic patterns.