Users who frequently use WordPress and multiple sites should have encountered data conflicts on multiple sites when using redis to cache the database.
There are many ways to deal with this problem. Let's talk about several common ways to use redis at the same time for multiple sites.
1. Modify the database prefix when creating a WordPress site
We create a database when WordPress is installed in one step, and the default database prefix is: wp_
We can change the characters at will, such as xxx_
In this way, when the data is cached, there will be no data conflicts between the various libraries. This is also the most recommended way. We suggest changing the default database prefix when creating a site in the future.
2. Modify the relevant constants in the wp-config.php configuration file
The wp-config.php configuration file is a file that we often modify. This site has posted a lot of codes for adding or deleting various functions in the wp-config.php file.
This method is suitable for sites that have already been created, and is not suitable for situations where the database is re-operated.
This method comes from official documents:
https://github.com/rhubarbgroup/redis-cache/wiki/Connection-Parameters
By default, the object caching plugin will connect to Redis 127.0.0.1:6379 via TCP and select the database 0 library.
To adjust the connection parameters, you can define any of the following constants in the wp-config.php file.
- 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 );
Among them define('WP_REDIS_DATABASE', 0); The value 0 behind here is to change the parameters of the redis library. Redis defaults to 16 libraries, and each station can be set to a different value, such as 1 or 2, 3, 4, 5, 6, and so on.
Explanation of other setting items (from Google Translate):
- WP_REDIS_SCHEME (default: tcp )
- Specify the protocol used to communicate with the Redis instance. Internally, the client uses the connection class associated with the specified connection scheme. Support tcp ( TCP / IP ), UNIX ( UNIX domain socket), TLS (Transport Layer Security) or HTTP (by Webdis the HTTP protocol).
- WP_REDIS_HOST (default value: 127.0 . 0.1 )
- The IP or host name of the target server . This will be ignored when connecting to Redis using UNIX domain sockets .
- WP_REDIS_PORT (default value: 6379 )
- The TCP / IP port of the target server . This will be ignored when connecting to Redis using UNIX domain sockets .
- WP_REDIS_PATH (default: not set)
- Using UNIX domain sockets connected to Redis used for UNIX path domain socket file.
- WP_REDIS_DATABASE (default value: 0 )
- Accept the value used to automatically select the logical database through the SELECT command.
- WP_REDIS_PASSWORD (default: not set)
- Accept a value that is used to authenticate to the password-protected Redis server through the AUTH command .
- To use the Redis . 6 of the ACL , set it to [ 'username' , 'password' ] array (required PhpRedis 5.3 +).
- WP_REDIS_TIMEOUT (default value: 5 )
- The time (in seconds) to try to initially connect to the Redis server before failing (a fraction of a second is allowed).
- WP_REDIS_READ_TIMEOUT (default value: 5 )
- The time (in seconds) to try to read from the Redis server before failing (a fraction of a second is allowed).
- WP_REDIS_RETRY_INTERVAL (default: not set)
- The time (in milliseconds) to retry failed connection attempts.
3. Change the redis cache plugin configuration file
Take the Redis Object Cache caching plugin as an example.
The file directory of this plugin on the server is /wp-content/plugins/redis-cache/
There is an object-cache.php file in its includes directory, just modify it.
Roughly at line 546, just change the database value, the default value is 0
Restart the Redis Object Cache cache plug-in after saving the file.