pantheon-systems / wp-redis

WordPress Object Cache using Redis.
https://wordpress.org/plugins/wp-redis/
GNU General Public License v2.0
228 stars 68 forks source link
caching hacktoberfest object-cache redis wordpress wordpress-plugin

WP Redis

Actively Maintained

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.

Description

CircleCI

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.

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.

  1. Install object-cache.php to wp-content/object-cache.php with a symlink or by copying the file.

  2. 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.
    );
  3. If your Redis server is listening through a sockt file instead, set its path on host parameter and change the port to null:

    $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.
    );
  4. Engage thrusters: you are now backing WP's Object Cache with Redis.

  5. (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.

  6. (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.

  7. (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 the WP_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.

  8. (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.

  9. (Optional) To use Relay instead of PhpRedis as the client define the WP_REDIS_USE_RELAY constant to true. For support requests, please use Relay's GitHub discussions.

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

See CONTRIBUTING.md for information on contributing.

Security Policy

Reporting Security Bugs

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.

Frequently Asked Questions

Why would I want to use 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.

How does this work with other caching plugins?

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.

How do I disable the persistent object cache for a bad actor?

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.

Why does the object cache sometimes get out of sync with the database?

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.

Changelog

1.4.5-dev

1.4.4 (November 27, 2023)

1.4.3 (June 26, 2023)

1.4.2 (May 15, 2023)

1.4.1 (May 11, 2023)

1.4.0 (May 9, 2023)

1.3.5 (April 6, 2023)

1.3.4 (March 7, 2023)

1.3.3 (February 28, 2023)

1.3.2 (December 5, 2022)

1.3.1 (December 2, 2022)

1.3.0 (November 29, 2022)

1.2.0 (February 17, 2022)

1.1.4 (October 21, 2021)

1.1.3 (October 21, 2021)

1.1.2 (March 24, 2021)

1.1.1 (August 17, 2020)

1.1.0 (July 13, 2020)

1.0.1 (April 14, 2020)

1.0.0 (March 2, 2020)

0.8.3 (February 24, 2020)

0.8.2 (January 15, 2020)

0.8.1 (January 10, 2020)

0.8.0 (January 6, 2020)

0.7.1 (December 14, 2018)

0.7.0 (August 22, 2017)

0.6.2 (June 5, 2017)

0.6.1 (February 23, 2017)

0.6.0 (September 21, 2016)

0.5.0 (April 27, 2016)

0.4.0 (March 23, 2016)

0.3.0 (February 11, 2016)

0.2.2 (November 24, 2015)

0.2.1 (November 17, 2015)

0.2.0 (November 17, 2015)

0.1

Upgrade Notice

1.4.0 (May 9, 2023)

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.