Last update at :2024-05-15,Edit by888u
Users who frequently use WordPress and have multiple sites may have encountered the problem of data conflicts on multiple sites when using redis cache database.
There are many official ways to deal with this problem. Here are some common ways to use redis on multiple sites at the same time.
1. Modify the database prefix when creating a WordPress site
Our WordPress creates a database in one step during installation, where the default database prefix is: wp_
We can change the characters at will, for example to xxx_
In this way, there will be no data conflicts between libraries when the data is cached. This is also the most recommended way. We recommend changing the default database prefix when creating a site in the future.
2. Modify 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 further operations on the database.
This method comes from official documentation:
https://github.com/rhubarbgroup/redis-cache/wiki/Connection-Parameters
By default, the object cache 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 after this 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 changing it to 1 or 2, 3, 4, 5, 6, etc.
Explanation of other settings (from Google Translate):
WP_REDIS_SCHEME (default: tcp) Specifies the protocol used to communicate with the Redis instance. Internally, the client uses the connection class associated with the specified connection scheme. Supports tcp (TCP/IP), unix (UNIX domain socket), tls (Transport Layer Security) or http (HTTP protocol via Webdis). WP_REDIS_HOST (default: 127.0.0.1) The IP or hostname of the target server. This is ignored when connecting to Redis using UNIX domain sockets. WP_REDIS_PORT (default: 6379) The TCP/IP port of the target server. This is ignored when connecting to Redis using UNIX domain sockets. WP_REDIS_PATH (default: not set) The path to the UNIX domain socket file used when connecting to Redis using UNIX domain sockets. WP_REDIS_DATABASE (default: 0) Accepts a numeric value used to automatically select a logical database via the SELECT command. WP_REDIS_PASSWORD (default: not set) Accepts a value used to authenticate to a password-protected Redis server via the AUTH command. To use a Redis 6 ACL, set it to an array of [‘username’, ‘password’] (requires PhpRedis 5.3+). WP_REDIS_TIMEOUT (default: 5) The time in seconds to attempt an initial connection to the Redis server before failing (allow fractions of a second). WP_REDIS_READ_TIMEOUT (default: 5) The time in seconds to attempt to read from the Redis server before failing (allow fractions of a second). WP_REDIS_RETRY_INTERVAL (default: not set) The time in milliseconds to retry a failed connection attempt.
3. Change the redis cache plug-in configuration file
Here is the Redis Object Cache cache plug-in as an example.
The file directory of this plug-in 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.
Recommended site search: Chongqing Telecom server hosting, mainland China php space, domain name and host, virtual host, free asp.net space, registered international domain name free domain name space, Guangzhou website registration, anti-complaint vps host Hong Kong space, p>
发表评论