WordPress Object Cache
It is not unusual for WordPress to run dozens or even hundreds (yikes!) of database queries when putting together a page. What’s worse, these queries aren’t the sort from children’s bedtime stories. They’re the dirty ones, full of temporary tables, JOINs on JOINs on JOINs, generic data types, and insufficient indexing. They’re the MySQL equivalent of The Wire.
As traffic loads increase, these computationally-heavy operations can quickly lead to a bottleneck, making your site sluggish or bringing it offline altogether.
To get a better idea of your own database use (or abuse), install a plugin like Query Monitor, which puts detailed query stats in the admin toolbar.
If you’re nearing the limit of what MySQL can handle, there are a few basic things you should consider doing:
- Go through your plugins and deactivate and delete anything that isn’t being used.
- Set reasonable post limits (10 or fewer) for archive pages.
- If you have a custom theme, try to minimize the number of secondary queries being run inside The Loop.
Beyond that, well, that’s the purpose of this article: Object Caching!
WordPress has the ability to cache query results (and other key/value data) in memory for later retrieval so it doesn’t have to pester MySQL on repeat requests.
This can dramatically reduce the load on MySQL, while maintaining comparable speed.
The main disadvantage of Object Cache is that objects are, well, cached. This means that changes made in the backend might not be immediately reflected on the front end.
This can be a deal breaker for applications with transactional data like e-commerce sites, unless the code contains strategic calls to wp_cache_flush(). But for content-driven sites like blogs and portfolios, it can be the perfect solution.
Object Cache also takes over handling of “transient” data, which otherwise get stored in the wp_options table. This will both speed up plugins and themes that rely on the transient API, and also reduce bloat in the database (there is no automatic garbage collection process run against wp_options, so data remains forever, and ever, and ever… (unless the same cache key is requested again)).
WordPress Object Caching is designed with extensibility in mind. All it needs is an API for key/value storage and retrieval.
There are innumerable candidates for the job, so let’s just focus on some of the most common:
XCache is my favorite PHP opcode cacher (opcode cache, incidentally, will also speed up the execution of your PHP scripts in general).
It is lightweight, scalable, and supports the latest versions of PHP.
To get started, install the XCache PHP extension (available in most Linux repositories), tweak the INI settings* (variable cache is the relevant feature here), and restart PHP.
If you have multiple sites running on the same server, you need to ensure you are running version 3.0.3 or later.
The XCache extension comes bundled with admin scripts you can copy to your web root to get an idea of how cache is being utilized. You should install this after it has been running for a while to see if the memory allocations need to be raised or lowered (just make sure you restrict access, and delete it when you’re done!). Remember, the memory settings are per-process, so don’t get carried away.
APC is another PHP opcode cacher, but it is no longer actively developed.
Unless you are running an older version of PHP or your hosting environment comes bundled with it, XCache is a better option.
There remains, however, one excellent use case for APC: Facebook’s HHVM engine supports it out of the box! If you have already replaced PHP with HHVM, just install the APC Object Cache Backend plugin and you’re good to go.
Redis is a powerful key/value caching solution with support for multiple servers.
For complex web applications consisting of multiple servers, it is an ideal solution as the cache can be shared across more than one machine.
For more simple setups, it is a bit much, and noticeably slower than XCache or APC.
To get started, install the Redis server and PHP extension (if you are running HHVM instead of PHP, the “extension” is built-in). Once you have that configured, you can install the Redis Object Cache plugin.