Table of Contents
Description
For sites concerned with high traffic, speed for logged-in users, or dynamic pageloads, a high-speed and persistent object cache is a must. You also need something that can scale across multiple instances of your application, so using local file caches or APC are out.
Redis is a great answer, and one we bundle on the Pantheon platform. This is our plugin for integrating with the cache, but you can use it on any self-hosted WordPress site if you have Redis. Install from WordPress.org or Github.
It’s important to note that a persistent object cache isn’t a panacea – a page load with 2,000 Redis calls can be 2 full seconds of object cache transactions. Make sure you use the object cache wisely: keep to a sensible number of keys, don’t store a huge amount of data on each key, and avoid stampeding frontend writes and deletes.
Go forth and make awesome! And, once you’ve built something great, send us feature requests (or bug reports). Take a look at the wiki for useful code snippets and other tips.
WP-CLI Commands
This plugin implements a variety of WP-CLI commands. All commands are grouped into the wp redis
namespace.
$ wp help redis
NAME
wp redis
SYNOPSIS
wp redis <command>
SUBCOMMANDS
cli Launch redis-cli using Redis configuration for WordPress
debug Debug object cache hit / miss ratio for any page URL.
enable Enable WP Redis by creating the symlink for object-cache.php
info Provide details on the Redis connection.
Use wp help redis <command>
to learn more about each command.
Contributing
The best way to contribute to the development of this plugin is by participating on the GitHub project:
https://github.com/pantheon-systems/wp-redis
Pull requests and issues are welcome!
You may notice there are two sets of tests running, on two different services:
- Travis CI runs the PHPUnit test suite in a variety of environment configurations (e.g. Redis enabled vs. Redis disabled).
- Circle CI runs the Behat test suite against a Pantheon site, to ensure the plugin’s compatibility with the Pantheon platform.
Both of these test suites can be run locally, with a varying amount of setup.
PHPUnit requires the WordPress PHPUnit test suite, and access to a database with name wordpress_test
. If you haven’t already configured the test suite locally, you can run bash bin/install-wp-tests.sh wordpress_test root '' localhost
. You’ll also need to enable Redis and the PHPRedis extension in order to run the test suite against Redis.
Behat requires a Pantheon site with Redis enabled. Once you’ve created the site, you’ll need install Terminus, and set the TERMINUS_TOKEN
, TERMINUS_SITE
, and TERMINUS_ENV
environment variables. Then, you can run ./bin/behat-prepare.sh
to prepare the site for the test suite.
Installation
This assumes you have a PHP environment with the required PhpRedis extension and a working Redis server (e.g. Pantheon). WP Redis also works with Predis via humanmade/wp-redis-predis-client.
- Install
object-cache.php
towp-content/object-cache.php
with a symlink or by copying the file. -
If you’re not running on Pantheon, edit wp-config.php to add your cache credentials, e.g.:
$redis_server = array( 'host' => '127.0.0.1', 'port' => 6379, 'auth' => '12345', 'database' => 0, // Optionally use a specific numeric Redis database. Default is 0. );
-
If your Redis server is listening through a sockt file instead, set its path on
host
parameter and change the port tonull
:$redis_server = array( 'host' => '/path/of/redis/socket-file.sock', 'port' => null, 'auth' => '12345', 'database' => 0, // Optionally use a specific numeric Redis database. Default is 0. );
-
Engage thrusters: you are now backing WP’s Object Cache with Redis.
- (Optional) To use the
wp redis
WP-CLI commands, activate the WP Redis plugin. No activation is necessary if you’re solely using the object cache drop-in. - (Optional) To use the same Redis server with multiple, discreet WordPress installs, you can use the
WP_CACHE_KEY_SALT
constant to define a unique salt for each install. - (Optional) To use true cache groups, with the ability to delete all keys for a given group, register groups with
wp_cache_add_redis_hash_groups()
, or define theWP_REDIS_USE_CACHE_GROUPS
constant to true to enable with all groups. However, when enabled, the expiration value is not respected because expiration on group keys isn’t a feature supported by Redis. - (Optional) On an existing site previously using WordPress’ transient cache, use WP-CLI to delete all (
%_transient_%
) transients from the options table:wp transient delete-all
. WP Redis assumes responsibility for the transient cache.