WordPress multi-site data coexistence (using Redis at the same time)

 

WordPress multi-site data coexistence (using Redis at the same time)
WordPress uses Redis for multi-site data coexistence

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

WordPress multi-site data coexistence (using Redis at the same time)

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.

  1. define ( 'WP_REDIS_HOST' , '127.0.0.1' );
  2. define ( 'WP_REDIS_PORT' , 6379 );
  3. // define('WP_REDIS_PASSWORD','secret' );
  4. define ( 'WP_REDIS_TIMEOUT' , 1 );
  5. define ( 'WP_REDIS_READ_TIMEOUT' , 1 );
  6.  
  7.  
  8. // change the database for each site to avoid cache collisions
  9. define ( 'WP_REDIS_DATABASE' , 0 );
  10.  
  11.  
  12. // supported clients: `phpredis`, `credis`, `predis` and `hhvm`
  13. // define('WP_REDIS_CLIENT','phpredis' );
  14.  
  15.  
  16. // automatically delete cache keys after 7 days
  17. // define('WP_REDIS_MAXTTL', 60 * 60 * 24 * 7 );
  18.  
  19.  
  20. // bypass the object cache, useful for debugging
  21. // 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):

 

  1. WP_REDIS_SCHEME (default: tcp )
  2. 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).
  3.  
  4.  
  5. WP_REDIS_HOST (default value: 127.0 . 0.1 )
  6. The IP or host name of the target server . This will be ignored when connecting to Redis using UNIX domain sockets .
  7.  
  8.  
  9. WP_REDIS_PORT (default value: 6379 )
  10. The TCP / IP port of the target server . This will be ignored when connecting to Redis using UNIX domain sockets .
  11.  
  12.  
  13. WP_REDIS_PATH (default: not set)
  14. Using UNIX domain sockets connected to Redis used for UNIX path domain socket file.
  15.  
  16.  
  17. WP_REDIS_DATABASE (default value: 0 )
  18. Accept the value used to automatically select the logical database through the SELECT command.
  19.  
  20.  
  21. WP_REDIS_PASSWORD (default: not set)
  22. Accept a value that is used to authenticate to the password-protected Redis server through the AUTH command .
  23.  
  24.  
  25. To use the Redis . 6 of the ACL , set it to [ 'username' , 'password' ] array (required PhpRedis 5.3 +).
  26.  
  27.  
  28. WP_REDIS_TIMEOUT (default value: 5 )
  29. The time (in seconds) to try to initially connect to the Redis server before failing (a fraction of a second is allowed).
  30.  
  31.  
  32. WP_REDIS_READ_TIMEOUT (default value: 5 )
  33. The time (in seconds) to try to read from the Redis server before failing (a fraction of a second is allowed).
  34.  
  35.  
  36. WP_REDIS_RETRY_INTERVAL (default: not set)
  37. The time (in milliseconds) to retry failed connection attempts.

3. Change the redis cache plugin configuration file

WordPress multi-site data coexistence (using Redis at the same time)

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.

WordPress multi-site data coexistence (using Redis at the same time)

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.