Contributors: getpantheon, danielbachhuber, mboynes, Outlandish Josh jspellman jazzs3quence
Tags: cache, plugin, redis
Requires at least: 3.0.1
Tested up to: 6.4.1
Stable tag: 1.4.5-dev
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Back your WP Object Cache with Redis, a high-performance in-memory storage backend.
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.
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 totrue
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. -
(Optional) To use Relay instead of PhpRedis as the client define the
WP_REDIS_USE_RELAY
constant totrue
. For support requests, please use Relay's GitHub discussions.
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.
See CONTRIBUTING.md for information on contributing.
Please report security bugs found in the WP Redis plugin's source code through the Patchstack Vulnerability Disclosure Program. The Patchstack team will assist you with verification, CVE assignment, and notify the developers of this plugin.
If you are concerned with the speed of your site, backing it with a high-performance, persistent object cache can have a huge impact. It takes load off your database, and is faster for loading all the data objects WordPress needs to run.
This plugin is for the internal application object cache. It doesn't have anything to do with page caches. On Pantheon you do not need additional page caching, but if you are self-hosted you can use your favorite page cache plugins in conjunction with WP Redis.
A page load with 2,000 Redis calls can be 2 full seconds of object cache transactions. If a plugin you're using is erroneously creating a huge number of cache keys, you might be able to mitigate the problem by disabling cache persistency for the plugin's group:
wp_cache_add_non_persistent_groups( array( 'bad-actor' ) );
This declaration means use of wp_cache_set( 'foo', 'bar', 'bad-actor' );
and wp_cache_get( 'foo', 'bad-actor' );
will not use Redis, and instead fall back to WordPress' default runtime object cache.
There's a known issue with WordPress alloptions
cache design. Specifically, a race condition between two requests can cause the object cache to have stale values. If you think you might be impacted by this, review this GitHub issue for links to more context, including a workaround.
- Updates Pantheon WP Coding Standards to 2.0 [#445]
- Handle duplicate keys in
get_multiple
function [#448] (props @Souptik2001)
- Bug fix: Fixes assumption that CACHE_PORT & CACHE_PASSWORD are Set. [428] (props @timnolte)
- Adds WP.org validation GitHub action [#435]
- Bug fix: Fixes incorrect order of
array_replace_recursive
and other issues [434] (props @timnolte) - Bug fix: Replace use of wp_strip_all_tags in object-cache.php [434] (props @timnolte)
- Bug fix: Don't strip tags from the cache password. [434] (props @timnolte)
- Bug fix: Removes exception loop caused by
esc_html
in_exception_handler()
[421]
- Bug fix:
wp_cache_flush_runtime
should only clear the local cache [413]
- Add support for
flush_runtime
andflush_group
functions [#405] - Add
pantheon-wp-coding-standards
[#400] - Update CONTRIBUTING.MD [#406]
- Update Composer dependencies [#401]
- Bump tested up to version to 6.2
- Update Composer dependencies [#394]
- Set
missing_redis_message
if Redis service is not connected [#391]
- Add PHP 8.2 support [#388]
- Remove Grunt, add valid license to Composer file [#387]
- Update Composer dependencies [#384] [#385]
- Fix broken
wp_cache_supports
function [#382].
- Declare
wp_cache_supports
function and support features. [#378] - Make dependabot target
develop
branch for PRs. [#376] - Declare
wp_cache_supports
function and support features. [#378]
- Added CONTRIBUTING.MD and GitHub action to automate deployments to wp.org. [#368]
- Adds support for Relay via
WP_REDIS_USE_RELAY
constant [#344].
- Fixes some faulty logic in
WP_REDIS_IGNORE_GLOBAL_GROUPS
check [#333].
- Supports a
WP_REDIS_IGNORE_GLOBAL_GROUPS
constant to prevent groups from being added to global caching group [#331].
- Applies logic used elsewhere to fall back to
$_SERVER
inwp_redis_get_info()
[#316].
- Returns cache data in correct order when using
wp_cache_get_multiple()
and internal cache is already primed [#292].
- Implements
wp_cache_get_multiple()
for WordPress 5.5 [#287]. - Bails early when connecting to Redis throws an Exception to avoid fatal error [285].
- Adds support for specifying Redis database number from environment/server variables [#273].
- Plugin is stable.
- Fixes
wp redis cli
by usingproc_open()
directly, instead ofWP_CLI::launch()
[#268].
- Catches exceptions when trying to connect to Redis [#265].
- Adds
WP_REDIS_DEFAULT_EXPIRE_SECONDS
constant to set default cache expire value [#264].
- Uses
flushdb
instead offlushAll
to avoid flushing the entire Redis instance [#259].
- Better support in
wp_cache_init()
for drop-ins like LudicrousDB [#231]. - Cleans up PHPCS issues.
- Adds filterable connection methods to permit use of Predis. See humanmade/wp-redis-predis-client for more details.
- Bug fix: Preserves null values in internal cache.
- Bug fix: Converts numeric values to their true type when getting.
- Bug fix: correctly passes an empty password to
redis-cli
. - Variety of improvements to the test suite.
- Introduces three new WP-CLI commands:
wp redis debug
to display cache hit/miss ratio for any URL;wp redis info
to display high-level Redis statistics;wp redis enable
to create theobject-cache.php
symlink. - Permits a Redis database to be defined with
$redis_server['database']
. - Introduces
wp_cache_add_redis_hash_groups()
, which permits registering specific groups to use Redis hashes, and is more precise than our existingWP_REDIS_USE_CACHE_GROUPS
constant.
- Performance boost! Removes redundant
exists
call fromwp_cache_get()
, which easily halves the number of Redis calls. - Uses
add_action()
and$wpdb
in a safer manner for compatibility with Batcache, which loads the object cache before aforementioned APIs are available. - For debugging purposes, tracks number of calls to Redis, and includes breakdown of call types.
- Adds a slew of more explicit test coverage against existing features.
- For consistency with the actual Redis call, calls
del
instead ofdelete
. - Bug fix: If a group isn't persistent, don't ever make an
exists
call against Redis.
- Introduces
wp redis-cli
, a WP-CLI command to launch redis-cli with WordPress' Redis credentials. - Bug fix: Ensures fail back mechanism works as expected on multisite, by writing to sitemeta table instead of the active site's options table.
- Bug fix: Uses 'default' as the default cache group, mirroring WordPress core, such that
$wp_object_cache->add( 'foo', 'bar' )
===wp_cache_add( 'foo', 'bar' )
.
- Introduces opt-in support for Redis cache groups. Enable with
define( 'WP_REDIS_USE_CACHE_GROUPS', true );
. When enabled, WP Redis persists cache groups in a structured manner, instead of hashing the cache key and group together. - Uses PHP_CodeSniffer and WordPress Coding Standards sniffs to ensure WP Redis adheres to WordPress coding standards.
- Bug fix: Permits use of a Unix socket in
$redis_server['host']
by ensuring the supplied$port
is null.
- Bug fix: use
INSERT IGNORE INTO
instead ofINSERT INTO
to prevent SQL errors when two concurrent processes attempt to write failback flag at the same time. - Bug fix: use
E_USER_WARNING
withtrigger_error()
. - Bug fix: catch Exceptions thrown during authentication to permit failing back to internal object cache.
- Bug fix: prevent SQL error when
$wpdb->options
isn't yet initialized on multisite.
- Gracefully fails back to the WordPress object cache when Redis is unavailable or intermittent. Previously, WP Redis would hard fatal.
- Triggers a PHP error if Redis goes away mid-request, for you to monitor in your logs. Attempts one reconnect based on specific error messages.
- Forces a flushAll on Redis when Redis comes back after failing. This behavior can be disabled with the
WP_REDIS_DISABLE_FAILBACK_FLUSH
constant. - Show an admin notice when Redis is unavailable but is expected to be.
- Initial commit of working code for the benefit of all.
WP Redis 1.4.0 adds support for the flush_runtime
and flush_group
functions. If you've copied object-cache.php
and made your own changes, be sure to copy these additions over as well.