### Basic Redis Configuration Example Source: https://github.com/rhubarbgroup/redis-cache/wiki/Home This example shows a basic configuration for connecting to a Redis server. Ensure these definitions are placed before 'wp-settings.php' in your wp-config.php file. ```php define( 'WP_REDIS_HOST', '127.0.0.1' ); define( 'WP_REDIS_PORT', 6379 ); // define( 'WP_REDIS_PASSWORD', 'secret' ); define( 'WP_REDIS_TIMEOUT', 1 ); define( 'WP_REDIS_READ_TIMEOUT', 1 ); // change the database for each site to avoid cache collisions define( 'WP_REDIS_DATABASE', 0 ); // supported clients: `phpredis`, `credis`, `predis` and `hhvm` // define( 'WP_REDIS_CLIENT', 'phpredis' ); // automatically delete cache keys after 7 days // define( 'WP_REDIS_MAXTTL', 60 * 60 * 24 * 7 ); // bypass the object cache, useful for debugging // define( 'WP_REDIS_DISABLED', true ); ``` -------------------------------- ### Sharding Mode: Start 3 Redis Instances Source: https://github.com/rhubarbgroup/redis-cache/wiki/Docker-Development Starts 3 Redis instances in sharding mode. The default client used is predis. ```bash ./docker/start.sh shard ``` -------------------------------- ### Replication Mode: Start Master and Slaves Source: https://github.com/rhubarbgroup/redis-cache/wiki/Docker-Development Starts 4 Redis containers (1 master, 3 slaves) in replication mode. The default client used is phpredis. ```bash ./docker/start.sh replication ``` -------------------------------- ### Cluster Mode: Start 3 Redis Instances Source: https://github.com/rhubarbgroup/redis-cache/wiki/Docker-Development Starts 3 Redis instances in cluster mode. The default client used is predis. ```bash ./docker/start.sh cluster ``` -------------------------------- ### Default Usage: Start Single Redis Container Source: https://github.com/rhubarbgroup/redis-cache/wiki/Docker-Development Starts a single Redis container for the simplest mode of operation. The default client used is phpredis. ```bash ./docker/start.sh ``` -------------------------------- ### Sentinel Mode: Start Sentinels and Replicated Redis Source: https://github.com/rhubarbgroup/redis-cache/wiki/Docker-Development Starts 3 sentinel instances and 6 normal Redis containers (1 master, 5 slaves) in replication mode. The default client used is predis. ```bash ./docker/start.sh sentinel ``` -------------------------------- ### Install Predis using Composer Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Use Composer to add Predis as a project dependency. ```shell composer require predis/predis ``` -------------------------------- ### Basic Redis Cluster Setup Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/colinmollenhour/credis/README.markdown Initialize Credis_Cluster for utilizing Redis cluster scalability. Requires the phpredis extension. Pass a cluster name or seed nodes. ```php set('key','value'); echo "Get: ".$cluster->get('key').PHP_EOL; ``` -------------------------------- ### Install and Configure Redis Cache via Composer Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/INSTALL.md Use Composer to install the Redis Object Cache plugin, enable the drop-in, check its status, and configure connection parameters using wp-cli. ```bash # Install the plugin: composer require wpackagist-plugin/redis-cache # Next, enable the drop-in: wp redis enable # Check the connection: wp redis status # Configure the plugin wp config set WP_REDIS_HOST "127.0.0.1" wp config set WP_REDIS_PORT "6379" wp config set WP_REDIS_DATABASE "15" ``` -------------------------------- ### Predis Sentinel Configuration with Authentication and Database Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Example of Predis Sentinel configuration including password authentication and specifying a different database index. These are passed via the 'parameters' client option. ```php $options = [ 'replication' => 'sentinel', 'service' => 'mymaster', 'parameters' => [ 'password' => $secretpassword, 'database' => 10, ], ]; ``` -------------------------------- ### Configure Redis Cluster Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/README.md Configure a Redis Cluster setup using the 'phpredis' client. This defines the nodes participating in the cluster. ```php define( 'WP_REDIS_CLIENT', 'phpredis' ); define( 'WP_REDIS_CLUSTER', [ 'tcp://127.0.0.1:6379?alias=node-01', 'tcp://127.0.0.2:6379?alias=node-02', 'tcp://127.0.0.3:6379?alias=node-03', ] ); ``` -------------------------------- ### Configure Redis Connection Parameters Source: https://github.com/rhubarbgroup/redis-cache/wiki/Connection-Parameters Define these constants in your wp-config.php to customize Redis connection settings. This example shows explicit settings for host, port, password, timeouts, database, client, max TTL, and disabling the cache. ```php define( 'WP_REDIS_HOST', '127.0.0.1' ); define( 'WP_REDIS_PORT', 6379 ); // define( 'WP_REDIS_PASSWORD', 'secret' ); define( 'WP_REDIS_TIMEOUT', 1 ); define( 'WP_REDIS_READ_TIMEOUT', 1 ); // change the database for each site to avoid cache collisions // values 0-15 are valid in a default redis config. define( 'WP_REDIS_DATABASE', 0 ); // supported clients: `phpredis`, `credis`, `predis` and `hhvm` // define( 'WP_REDIS_CLIENT', 'phpredis' ); // automatically delete cache keys after 7 days // define( 'WP_REDIS_MAXTTL', 60 * 60 * 24 * 7 ); // bypass the object cache, useful for debugging // define( 'WP_REDIS_DISABLED', true ); ``` -------------------------------- ### Credis Contribution: Code Formatting Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/colinmollenhour/credis/README.markdown Commands to install the PHP-CS-Fixer tool and format the project code according to coding standards. ```shell composer require "friendsofphp/php-cs-fixer:^3.13" --dev --no-update -n composer format ``` -------------------------------- ### Configure Relay Client Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/README.md Set the client to 'relay' for advanced caching that keeps a partial replica of Redis' dataset in PHP's memory. Each WordPress installation requires a dedicated Redis database and unique prefix when using Relay. ```php define( 'WP_REDIS_CLIENT', 'relay' ); define( 'WP_REDIS_HOST', '127.0.0.1' ); define( 'WP_REDIS_PORT', 6379 ); // when using Relay, each WordPress installation // MUST a dedicated Redis database and unique prefix define( 'WP_REDIS_DATABASE', 0 ); define( 'WP_REDIS_PREFIX', 'db3:' ); // consume less memory define( 'WP_REDIS_IGBINARY', true ); ``` -------------------------------- ### Check Redis Server Connection Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/FAQ.md Use redis-cli to verify if the Redis server is running and accessible. This command is useful for initial setup and troubleshooting connection issues. ```bash redis-cli PING ``` ```bash redis-cli -h 127.0.0.1 -p 6379 PING ``` -------------------------------- ### Activate Redis Cache Plugin Source: https://github.com/rhubarbgroup/redis-cache/wiki/WP-CLI-Commands Activates the Redis object cache plugin using WP-CLI. Ensure the plugin is installed before activation. ```bash wp plugin activate redis-cache ``` -------------------------------- ### Open Redis-CLI on Second Redis Slave Instance Source: https://github.com/rhubarbgroup/redis-cache/wiki/Docker-Development Connects to the second Redis slave instance using redis-cli. The index might vary based on the Docker Compose setup. ```bash docker-compose exec --index=2 redis-slave redis-cli ``` -------------------------------- ### redis_object_cache_get Source: https://github.com/rhubarbgroup/redis-cache/wiki/Hooks-&-Actions Fires on every cache get request. ```APIDOC ## redis_object_cache_get ### Description Fires on every cache get request. ### Parameters * _`mixed`_ `$value` Value of the cache entry. * _`string`_ `$key` The cache key. * _`string`_ `$group` The group value appended to the $key. * _`bool`_ `$force` Whether a forced refetch has taken place rather than relying on the local cache. * _`bool`_ `$found` Whether the key was found in the cache. * _`float`_ `$execute_time` Execution time for the request in seconds. ``` -------------------------------- ### redis_object_cache_get_multiple Source: https://github.com/rhubarbgroup/redis-cache/wiki/Hooks-&-Actions Fires on every cache get multiple request. ```APIDOC ## redis_object_cache_get_multiple ### Description Fires on every cache get multiple request. ### Parameters * _`mixed`_ `$value` Value of the cache entry. * _`string`_ `$key` The cache key. * _`string`_ `$group` The group value appended to the $key. * _`bool`_ `$force` Whether a forced refetch has taken place rather than relying on the local cache. * _`float`_ `$execute_time` Execution time for the request in seconds. ``` -------------------------------- ### Flush Redis Server Data Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/FAQ.md Execute FLUSHALL command to clear all data from the Redis server. This is necessary when multiple WordPress installations share the same Redis database index, causing domain redirection issues. ```bash redis-cli -h 127.0.01 -p 6379 FLUSHALL ``` -------------------------------- ### Stop All Docker Containers Source: https://github.com/rhubarbgroup/redis-cache/wiki/Docker-Development Stops all running Docker containers. Add the -v flag to delete all persistent data and start clean next time. ```bash ./docker/start.sh down ``` -------------------------------- ### Basic Client Configuration with Prefix Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Instantiates a Predis client with a specified key prefix. This option is useful for namespacing keys in Redis. ```php $client = new Predis\Client($parameters, ['prefix' => 'sample:']); ``` -------------------------------- ### Execute Predis Pipeline with Fluent Interface Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Demonstrates using Predis pipelines with a fluent interface, allowing commands to be chained before execution. This provides a more readable way to construct pipelines. ```php $responses = $client->pipeline()->set('foo', 'bar')->get('foo')->execute(); ``` -------------------------------- ### Configure Predis for Master/Slave Replication Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Sets up a Predis client with a static list of master and slave servers for replication. The master is explicitly defined, while slaves are optional. ```php $parameters = ['tcp://10.0.0.1?role=master', 'tcp://10.0.0.2', 'tcp://10.0.0.3']; $options = ['replication' => 'predis']; $client = new Predis\Client($parameters, $options); ``` -------------------------------- ### Configure Redis Replication (Sentinel) Source: https://github.com/rhubarbgroup/redis-cache/wiki/Replication-&-Clustering For Sentinel-based replication, define `WP_REDIS_SENTINEL` with the Sentinel alias and `WP_REDIS_SERVERS` with the Sentinel host addresses. Ensure `WP_REDIS_CLIENT` is set to 'predis'. ```php define( 'WP_REDIS_CLIENT', 'predis' ); define( 'WP_REDIS_SENTINEL', 'my-sentinel' ); define( 'WP_REDIS_SERVERS', [ 'tcp://127.0.0.1:5380', 'tcp://127.0.0.2:5381', 'tcp://127.0.0.3:5382', ] ); ``` -------------------------------- ### Connect to Redis with Default Parameters Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Instantiate a Predis client without parameters to connect to localhost on the default port (6379). ```php $client = new Predis\Client(); $client->set('foo', 'bar'); $value = $client->get('foo'); ``` -------------------------------- ### Basic Credis Client Initialization Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/colinmollenhour/credis/README.markdown Instantiate the Credis_Client with a connection string. Supports Unix socket, TCP, and TLS connections. ```php $redis = new Credis_Client(/* connection string */); ``` -------------------------------- ### Configure Replication Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/README.md Set up Redis replication using the 'predis' client. This configuration defines master and replica servers for high availability. ```php define( 'WP_REDIS_CLIENT', 'predis' ); define( 'WP_REDIS_SERVERS', [ 'tcp://127.0.0.1:6379?database=5&role=master', 'tcp://127.0.0.2:6379?database=5&alias=replica-01', ] ); ``` -------------------------------- ### Implement Custom Connection Backend Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Create custom connection classes implementing Predis\Connection\NodeConnectionInterface or extending Predis\Connection\AbstractConnection to support new network backends. ```php class MyConnectionClass implements Predis\Connection\NodeConnectionInterface { // Implementation goes here... } // Use MyConnectionClass to handle connections for the `tcp` scheme: $client = new Predis\Client('tcp://127.0.0.1', [ 'connections' => ['tcp' => 'MyConnectionClass'], ]); ``` -------------------------------- ### Configure Redis Sentinel Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/README.md Set up Redis Sentinel for high availability using the 'predis' client. Specify the Sentinel master name and server addresses. ```php define( 'WP_REDIS_CLIENT', 'predis' ); define( 'WP_REDIS_SENTINEL', 'my-sentinel' ); define( 'WP_REDIS_SERVERS', [ 'tcp://127.0.0.1:5380', 'tcp://127.0.0.2:5381', 'tcp://127.0.0.3:5382', ] ); ``` -------------------------------- ### Open Redis-CLI on Master Instance Source: https://github.com/rhubarbgroup/redis-cache/wiki/Docker-Development Connects to the Redis master instance using redis-cli. ```bash docker-compose exec redis-master redis-cli ``` -------------------------------- ### Configure Relay Connection Backend Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Use the Relay integration for performance gains by caching a partial replica of the Redis dataset in PHP shared runtime memory. ```php $client = new Predis\Client('tcp://127.0.0.1', [ 'connections' => 'relay', ]); ``` -------------------------------- ### Cluster Configuration (Client-Side Sharding) Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Configures Predis to work with a cluster using traditional client-side sharding. Requires manual rebalancing of the keyspace. ```php $parameters = ['tcp://10.0.0.1', 'tcp://10.0.0.2', 'tcp://10.0.0.3']; $options = ['cluster' => 'predis']; $client = new Predis\Client($parameters); ``` -------------------------------- ### Configure Redis Replication (Master-Replica) Source: https://github.com/rhubarbgroup/redis-cache/wiki/Replication-&-Clustering Use the `WP_REDIS_SERVERS` constant to define master and replica servers for replication. Ensure `WP_REDIS_CLIENT` is set to 'predis'. ```php define( 'WP_REDIS_CLIENT', 'predis' ); define( 'WP_REDIS_SERVERS', [ 'tcp://127.0.0.1:6379?database=5&alias=master', 'tcp://127.0.0.2:6379?database=5&alias=replica-01', ] ); ``` -------------------------------- ### Basic Credis Client Usage Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/colinmollenhour/credis/README.markdown Instantiate a Credis_Client, set a key-value pair, and retrieve the value. Demonstrates automatic flattening of array arguments for commands like RPUSH. ```php require 'Credis/Client.php'; $redis = new Credis_Client('localhost'); $redis->set('awesome', 'absolutely'); echo sprintf('Is Credis awesome? %s.\n', $redis->get('awesome')); // When arrays are given as arguments they are flattened automatically $redis->rpush('particles', array('proton','electron','neutron')); $particles = $redis->lrange('particles', 0, -1); ``` -------------------------------- ### Connect with TLS/SSL Encryption (Array) Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Enable TLS/SSL encryption for secure connections by using the 'tls' scheme and providing SSL options. ```php $client = new Predis\Client([ 'scheme' => 'tls', 'ssl' => ['cafile' => 'private.pem', 'verify_peer' => true], ]); ``` -------------------------------- ### Configure Redis Connection in wp-config.php Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/INSTALL.md Define constants in wp-config.php to configure the Redis host, port, prefix, database, and timeouts. Ensure these constants are defined before the wp-settings.php file is included. ```php // adjust Redis host and port if necessary define( 'WP_REDIS_HOST', '127.0.0.1' ); define( 'WP_REDIS_PORT', 6379 ); // change the prefix and database for each site to avoid cache data collisions define( 'WP_REDIS_PREFIX', 'my-moms-site' ); define( 'WP_REDIS_DATABASE', 0 ); // 0-15 // reasonable connection and read+write timeouts define( 'WP_REDIS_TIMEOUT', 1 ); define( 'WP_REDIS_READ_TIMEOUT', 1 ); ``` ```php /* That's all, stop editing! Happy publishing. */ require_once(ABSPATH . 'wp-settings.php'); ``` -------------------------------- ### Connect with TLS/SSL Encryption (URI) Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Enable TLS/SSL encryption using a URI string, specifying SSL options within the query parameters. ```php $client = new Predis\Client('tls://127.0.0.1?ssl[cafile]=private.pem&ssl[verify_peer]=1'); ``` -------------------------------- ### Execute Predis Pipeline within a Callable Block Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Shows how to execute a series of commands as a pipeline within a callable block. This reduces network latency by sending multiple commands at once. ```php $responses = $client->pipeline(function ($pipe) { for ($i = 0; $i < 1000; $i++) { $pipe->set("key:". $i, str_pad($i, 4, '0', 0)); $pipe->get("key:". $i); } }); ``` -------------------------------- ### Custom Replication Strategy for EVAL/EVALSHA Scripts Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Demonstrates how to customize replication behavior for EVAL and EVALSHA commands. A custom strategy can be used to specify which Lua scripts are safe to execute on slaves, preventing unnecessary switches to the master. ```php $parameters = ['tcp://10.0.0.1?role=master', 'tcp://10.0.0.2', 'tcp://10.0.0.3']; $options = ['replication' => function () { // Set scripts that won't trigger a switch from a slave to the master node. $strategy = new Predis\Replication\ReplicationStrategy(); $strategy->setScriptReadOnly($LUA_SCRIPT); return new Predis\Connection\Replication\MasterSlaveReplication($strategy); }]; $client = new Predis\Client($parameters, $options); $client->eval($LUA_SCRIPT, 0); // Sticks to slave using `eval`... $client->evalsha(sha1($LUA_SCRIPT), 0); // ... and `evalsha`, too. ``` -------------------------------- ### Open Redis-CLI on First Redis Sentinel Instance Source: https://github.com/rhubarbgroup/redis-cache/wiki/Docker-Development Connects to the first Redis sentinel instance using redis-cli on port 26379. ```bash docker-compose exec --index=1 redis-sentinel redis-cli -p 26379 ``` -------------------------------- ### Connect using UNIX Domain Sockets (Array) Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Connect to a local Redis instance using a UNIX domain socket by specifying the 'unix' scheme and the socket path. ```php $client = new Predis\Client(['scheme' => 'unix', 'path' => '/path/to/redis.sock']); ``` -------------------------------- ### Connect to Redis using URI String Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Provide connection parameters as a URI string. This is an alternative to using named arrays. ```php $client = new Predis\Client('tcp://10.0.0.1:6379'); ``` -------------------------------- ### Configure TLS/SSL Stream Options Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/README.md Define additional TLS/SSL stream connection options for secure connections. This can be used to customize peer verification. ```php define( 'WP_REDIS_SSL_CONTEXT', [ 'verify_peer' => false, 'verify_peer_name' => false, ]); ``` -------------------------------- ### wp redis enable Source: https://github.com/rhubarbgroup/redis-cache/wiki/WP-CLI-Commands Enables the Redis object cache by creating the object cache drop-in. This command respects existing unknown drop-ins. ```APIDOC ## wp redis enable ### Description Enables the Redis object cache. Default behavior is to create the object cache drop-in, unless an unknown object cache drop-in is present. ### Method CLI command ### Endpoint wp redis enable ### Parameters None ### Response Confirmation of Redis object cache enablement. ``` -------------------------------- ### Connect to Redis using Named Array Parameters Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Specify connection parameters like scheme, host, and port using a PHP associative array. ```php $client = new Predis\Client([ 'scheme' => 'tcp', 'host' => '10.0.0.1', 'port' => 6379, ]); ``` -------------------------------- ### Set Maximum Cache Key Time-To-Live Source: https://github.com/rhubarbgroup/redis-cache/wiki/Configuration-Options Configure the maximum time-to-live in seconds for cache keys that have an expiration time of 0. ```php define( 'WP_REDIS_MAXTTL', 86400 ); ``` -------------------------------- ### Connect using UNIX Domain Sockets (URI) Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Connect to a local Redis instance using a UNIX domain socket via a URI string with the 'unix' scheme. ```php $client = new Predis\Client('unix:/path/to/redis.sock'); ``` -------------------------------- ### Configure Test Environment Variables Source: https://github.com/rhubarbgroup/redis-cache/wiki/Local-Development Create a `config.sh` file to override default test environment variables. This is optional if default values are acceptable. ```bash GIT_BRANCH=trunk # the wordpress version you want to test against DB_HOST=127.0.0.1 DB_PASS=mysuperpassword # your password, default empty DB_NAME=yourdatabase # your database name, default `wp_tests` DB_USER=youruser # your database user, default `root` ``` -------------------------------- ### Execute Raw Redis Commands Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Send raw Redis commands to the server without argument filtering or response parsing. Arguments must be provided as an array. ```php $response = $client->executeRaw(['SET', 'foo', 'bar']); ``` -------------------------------- ### Configure Redis Client Source: https://github.com/rhubarbgroup/redis-cache/wiki/Configuration-Options Specify the client used for Redis communication. Supported options are 'phpredis', 'predis', and 'relay'. ```php define( 'WP_REDIS_CLIENT', 'predis' ); ``` -------------------------------- ### Configure Predis with Redis Sentinel for HA Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Configures a Predis client to use Redis Sentinel for high availability. This requires a list of sentinel connection parameters, replication set to 'sentinel', and the service name. ```php $sentinels = ['tcp://10.0.0.1', 'tcp://10.0.0.2', 'tcp://10.0.0.3']; $options = ['replication' => 'sentinel', 'service' => 'mymaster']; $client = new Predis\Client($sentinels, $options); ``` -------------------------------- ### Configure Redis Serializer Source: https://github.com/rhubarbgroup/redis-cache/wiki/Configuration-Options Use PhpRedis' built-in serializers. Supported values are Redis::SERIALIZER_PHP and Redis::SERIALIZER_IGBINARY. ```php define( 'WP_REDIS_SERIALIZER', Redis::SERIALIZER_IGBINARY ); ``` -------------------------------- ### Connect via Unix Socket Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/README.md Use this configuration to connect to Redis using a Unix domain socket. Ensure the socket path is correct. ```php define( 'WP_REDIS_SCHEME', 'unix' ); define( 'WP_REDIS_PATH', '/var/run/redis.sock' ); ``` -------------------------------- ### Connect via TCP with TLS Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/README.md Configure TCP connection with TLS encryption. Specify the host and port for your Redis instance. ```php define( 'WP_REDIS_SCHEME', 'tls' ); define( 'WP_REDIS_HOST', 'master.ncit.ameaqx.use1.cache.amazonaws.com' ); define( 'WP_REDIS_PORT', 6379 ); ``` -------------------------------- ### Configure Redis Sharding Source: https://github.com/rhubarbgroup/redis-cache/wiki/Replication-&-Clustering Use the `WP_REDIS_SHARDS` constant to define multiple Redis servers for sharding. Each server can be configured with a database and an alias. ```php define( 'WP_REDIS_SHARDS', [ 'tcp://127.0.0.1:6379?database=10&alias=shard-01', 'tcp://127.0.0.2:6379?database=10&alias=shard-02', 'tcp://127.0.0.3:6379?database=10&alias=shard-03', ] ); ``` -------------------------------- ### Configure Redis Clustering (Redis 3.0+) Source: https://github.com/rhubarbgroup/redis-cache/wiki/Replication-&-Clustering For Redis 3.0 and later, use the `WP_REDIS_CLUSTER` constant to define the nodes in your Redis cluster. Each node can be configured with an alias. ```php define( 'WP_REDIS_CLUSTER', [ 'tcp://127.0.0.1:6379?alias=node-01', 'tcp://127.0.0.2:6379?alias=node-02', 'tcp://127.0.0.3:6379?alias=node-03', ] ); ``` -------------------------------- ### Define Default Global Cache Groups Source: https://github.com/rhubarbgroup/redis-cache/wiki/Configuration-Options Set the list of network-wide cache groups that should not be prefixed with the blog-id. This is applicable only in a Multisite environment. ```php define( 'WP_REDIS_GLOBAL_GROUPS', [ 'blog-details', 'blog-id-cache', 'blog-lookup', 'global-posts', 'networks', 'rss', 'sites', 'site-details', 'site-lookup', 'site-options', 'site-transient', 'users', 'useremail', 'userlogins', 'usermeta', 'user_meta', 'userslugs', ] ); ``` -------------------------------- ### Enable Redis Object Cache Source: https://github.com/rhubarbgroup/redis-cache/wiki/WP-CLI-Commands Enables the Redis object cache by creating the object cache drop-in file. This command will overwrite an existing drop-in if it's not recognized. ```bash wp redis enable ``` -------------------------------- ### Configure Sharding with PhpRedis Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/README.md Configure sharding using PhpRedis' RedisArray feature. This distributes data across multiple Redis instances. ```php define( 'WP_REDIS_CLIENT', 'phpredis' ); define( 'WP_REDIS_SHARDS', [ 'tcp://127.0.0.1:6379?database=10&alias=shard-01', 'tcp://127.0.0.2:6379?database=10&alias=shard-02', 'tcp://127.0.0.3:6379?database=10&alias=shard-03', ] ); ``` -------------------------------- ### Set Redis Socket Path Source: https://github.com/rhubarbgroup/redis-cache/wiki/Configuration-Options Define the path to the Redis socket file if using a Unix domain socket. ```php define( 'WP_REDIS_PATH', '/var/run/redis/redis-server.sock' ); ``` -------------------------------- ### Define and Inject Lua Script Commands Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Extend Predis\Command\ScriptCommand to define Lua scripts for remote execution. Scripts are identified by SHA1 hash for efficiency. ```php class ListPushRandomValue extends Predis\Command\ScriptCommand { public function getKeysCount() { return 1; } public function getScript() { return << [ 'lpushrand' => 'ListPushRandomValue', ], ]); $response = $client->lpushrand('random_values', $seed = mt_rand()); ``` -------------------------------- ### Enable igbinary Serializer Source: https://github.com/rhubarbgroup/redis-cache/wiki/Configuration-Options Set to true to enable the igbinary serializer. This setting is ignored if WP_REDIS_SERIALIZER is already defined. ```php define( 'WP_REDIS_IGBINARY', true ); ``` -------------------------------- ### Define and Inject Custom Redis Commands Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Extend Predis\Command\Command to define new Redis commands or override existing ones. Inject custom commands into the client's command factory. ```php class BrandNewRedisCommand extends Predis\Command\Command { public function getId() { return 'NEWCMD'; } } ``` ```php $client = new Predis\Client($parameters, [ 'commands' => [ 'newcmd' => 'BrandNewRedisCommand', ], ]); $response = $client->newcmd(); ``` -------------------------------- ### Register Predis Autoloader Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Manually register Predis's autoloader if not using Composer. Ensure the Predis path is in your include_path. ```php // Prepend a base path if Predis is not available in your "include_path". require 'Predis/Autoloader.php'; Predis\Autoloader::register(); ``` -------------------------------- ### Aggregate Multiple Connections Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Configure Predis to aggregate multiple Redis connections for clustering or replication, mixing array and URI configurations. ```php $client = new Predis\Client([ 'tcp://10.0.0.1?alias=first-node', ['host' => '10.0.0.2', 'alias' => 'second-node'], ], [ 'cluster' => 'predis', ]); ``` -------------------------------- ### Connect to Redis with TLS Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/colinmollenhour/credis/README.markdown Enables transport level security by using a 'tls://' connection string. This is recommended over plain TCP for secure communication. ```php require 'Credis/Client.php'; $redis = new Credis_Client('tls://127.0.0.1:6379'); $redis->set('awesome', 'absolutely'); echo sprintf('Is Credis awesome? %s.\n', $redis->get('awesome')); // When arrays are given as arguments they are flattened automatically $redis->rpush('particles', array('proton','electron','neutron')); $particles = $redis->lrange('particles', 0, -1); ``` -------------------------------- ### redis_object_cache_enable Source: https://github.com/rhubarbgroup/redis-cache/wiki/Hooks-&-Actions Fires on cache enabling event. ```APIDOC ## redis_object_cache_enable ### Description Fires on cache enabling event. ### Parameters * _`bool`_ `$result` Whether the filesystem event was successfull. ``` -------------------------------- ### Enter WordPress Container Bash Shell Source: https://github.com/rhubarbgroup/redis-cache/wiki/Docker-Development Executes a bash shell inside the WordPress container for interactive use. ```bash docker-compose exec wordpress bash ``` -------------------------------- ### Connect to Password-Protected Redis Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Connect to Redis servers that require a password for authentication. For Redis >= 6.0 with ACLs, username is also required. ```php // Example for password protection (username omitted if not needed): // $client = new Predis\Client(['host' => '10.0.0.1', 'password' => 'your_password']); // Example for Redis >= 6.0 with ACLs: // $client = new Predis\Client(['host' => '10.0.0.1', 'username' => 'your_username', 'password' => 'your_password']); ``` -------------------------------- ### Redis Cluster Configuration (Coordinated) Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Connects to a Redis Cluster managed by redis-cluster. The client automatically discovers nodes and handles redirection. ```php $parameters = ['tcp://10.0.0.1', 'tcp://10.0.0.2', 'tcp://10.0.0.3']; $options = ['cluster' => 'redis']; $client = new Predis\Client($parameters, $options); ``` -------------------------------- ### Set Metrics Collection Timeframe Source: https://github.com/rhubarbgroup/redis-cache/wiki/Configuration-Options Define the maximum timeframe in seconds for collecting object cache metrics. Increase this value with caution. ```php define( 'WP_REDIS_METRICS_MAX_TIME', 7200 ); ``` -------------------------------- ### Delete All Transients with WP-CLI Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/FAQ.md Use this WP-CLI command to clear all transients stored in the database before enabling Redis. This ensures that Redis will be used for new transients. ```bash wp transient delete-all ``` -------------------------------- ### redis_object_cache_update_dropin Source: https://github.com/rhubarbgroup/redis-cache/wiki/Hooks-&-Actions Fires on dropin update event. ```APIDOC ## redis_object_cache_update_dropin ### Description Fires on dropin update event. ### Parameters * _`bool`_ `$result` Whether the filesystem event was successfull. ``` -------------------------------- ### Debug Redis Cache Flushes in PHP Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/FAQ.md Use this action hook to log detailed information about cache flush events. Check the generated log file to identify plugins causing frequent flushes. ```php add_action( 'redis_object_cache_flush', function( $results ) { ob_start(); echo date( 'c' ) . PHP_EOL; var_dump( $results ); debug_print_backtrace(); error_log( ob_get_clean(), 3, ABSPATH . '/redis-cache-flush.log' ); }, 10, 5 ); ``` -------------------------------- ### Connect with ACL Authentication Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/README.md Configure ACL authentication for Redis by providing a username and password. This is used for enhanced security. ```php define( 'WP_REDIS_PASSWORD', [ 'username', 'password' ] ); ``` -------------------------------- ### redis_object_cache_set Source: https://github.com/rhubarbgroup/redis-cache/wiki/Hooks-&-Actions Fires on every cache set. ```APIDOC ## redis_object_cache_set ### Description Fires on every cache set. ### Parameters * _`string`_ `$key` The cache key. * _`mixed`_ `$value` Value of the cache entry. * _`string`_ `$group` The group value appended to the $key. * _`int`_ `$expiration` The time in seconds the entry expires. 0 for no expiry. * _`float`_ `$execute_time` Execution time for the request in seconds. ``` -------------------------------- ### Enable Selective Cache Flushing Source: https://github.com/rhubarbgroup/redis-cache/wiki/Configuration-Options When set to true, cache flushing will only remove keys prefixed with WP_REDIS_PREFIX, rather than emptying the entire Redis database. ```php define( 'WP_REDIS_SELECTIVE_FLUSH', true ); ``` -------------------------------- ### redis_object_cache_error Source: https://github.com/rhubarbgroup/redis-cache/wiki/Hooks-&-Actions Fires on every cache error. ```APIDOC ## redis_object_cache_error ### Description Fires on every cache error. ### Parameters * _`\Exception`_ `$exception` The exception triggered. ``` -------------------------------- ### redis_object_cache_flush Source: https://github.com/rhubarbgroup/redis-cache/wiki/Hooks-&-Actions Fires on every cache flush. ```APIDOC ## redis_object_cache_flush ### Description Fires on every cache flush. ### Parameters * _`null|array`_ `$results` Array of flush results. * _`int`_ `$delay` Given number of seconds to waited before invalidating the items. * _`bool`_ `$seletive` Whether a selective flush took place. * _`string`_ `$salt` The defined key prefix. * _`float`_ `$execute_time` Execution time for the request in seconds. ``` -------------------------------- ### wp redis update-dropin Source: https://github.com/rhubarbgroup/redis-cache/wiki/WP-CLI-Commands Updates the Redis object cache drop-in, overwriting any existing drop-in by default. ```APIDOC ## wp redis update-dropin ### Description Updates the Redis object cache drop-in. Default behavior is to overwrite any existing object cache drop-in. ### Method CLI command ### Endpoint wp redis update-dropin ### Parameters None ### Response Confirmation of the object cache drop-in update. ``` -------------------------------- ### Set Unflushable Cache Groups Source: https://github.com/rhubarbgroup/redis-cache/wiki/Configuration-Options Specify cache groups that should not be flushed during a selective cache flush operation. ```php define( 'WP_REDIS_UNFLUSHABLE_GROUPS', ['do-not-flush-me'] ); ``` -------------------------------- ### Disable Graceful Failures Source: https://github.com/rhubarbgroup/redis-cache/wiki/Configuration-Options Set to false to disable graceful failures and allow exceptions to be thrown when the cache encounters issues. ```php define( 'WP_REDIS_GRACEFUL', false ); ``` -------------------------------- ### Execute Redis Transactions Source: https://github.com/rhubarbgroup/redis-cache/blob/develop/dependencies/predis/predis/README.md Execute Redis transactions using a callable block or a fluent interface. Supports automatic retries for aborted transactions. ```php $responses = $client->transaction(function ($tx) { $tx->set('foo', 'bar'); $tx->get('foo'); }); ``` ```php $responses = $client->transaction()->set('foo', 'bar')->get('foo')->execute(); ``` -------------------------------- ### Disable Admin Banners Source: https://github.com/rhubarbgroup/redis-cache/wiki/Configuration-Options Set to true to hide administrative banners and notices that promote Object Cache Pro. This does not affect the settings screen. ```php define( 'WP_REDIS_DISABLE_BANNERS', true ); ``` -------------------------------- ### wp redis status Source: https://github.com/rhubarbgroup/redis-cache/wiki/WP-CLI-Commands Shows the current status of the Redis object cache and, if available, the connected client. ```APIDOC ## wp redis status ### Description Show the Redis object cache status and (when possible) client. ### Method CLI command ### Endpoint wp redis status ### Parameters None ### Response Status of the Redis object cache and client information. ``` -------------------------------- ### Set Ignored Cache Groups Source: https://github.com/rhubarbgroup/redis-cache/wiki/Configuration-Options Define cache groups that should not be cached in Redis. Defaults to ['counts', 'plugins']. ```php define( 'WP_REDIS_IGNORED_GROUPS', ['counts', 'plugins'] ); ``` -------------------------------- ### redis_object_cache_delete Source: https://github.com/rhubarbgroup/redis-cache/wiki/Hooks-&-Actions Fires on every cache key deletion. ```APIDOC ## redis_object_cache_delete ### Description Fires on every cache key deletion. ### Parameters * _`string`_ `$key` The cache key. * _`string`_ `$group` The group value appended to the $key. * _`float`_ `$execute_time` Execution time for the request in seconds. ``` -------------------------------- ### redis_object_cache_disable Source: https://github.com/rhubarbgroup/redis-cache/wiki/Hooks-&-Actions Fires on cache disabling event. ```APIDOC ## redis_object_cache_disable ### Description Fires on cache disabling event. ### Parameters * _`bool`_ `$result` Whether the filesystem event was successfull. ``` -------------------------------- ### Diagnose Frequent Cache Flushes Source: https://github.com/rhubarbgroup/redis-cache/wiki/Troubleshooting Use this snippet to log the callstack of every cache flush. This helps identify plugins or processes causing excessive cache invalidation. Do not use in production. ```php add_action( 'redis_object_cache_flush', function( $results, $delay, $selective, $salt, $execute_time ) { ob_start(); echo date( 'c' ) . PHP_EOL; debug_print_backtrace(); var_dump( func_get_args() ); error_log( ABSPATH . '/redis-cache-flush.log', 3, ob_get_clean() ); }, 10, 5 ); ```