rhubarbgroup / redis-cache

A persistent object cache backend for WordPress powered by Redis. Supports Predis, PhpRedis, Relay, replication, sentinels, clustering and WP-CLI.
https://wordpress.org/plugins/redis-cache/
GNU General Public License v3.0
442 stars 150 forks source link

I would like to make a suggestion on user experience optimization design. #324

Closed hsxnet closed 3 years ago

hsxnet commented 3 years ago

Thank you very much for writing this plug-in: redis object cache. From the perspective of users, I would like to give you some suggestions on user experience design optimization from the following application scenarios:

1、 Application scenario description:

(1). Operation and maintenance engineers will customize the server security port "'port '= > 6379" to prevent hackers from intruding. However, every time the program is upgraded, they need to manually modify the port "'port' = > 6379" in multiple files to customize their own ports, such as "'port '= > 19888.".

(2). Multiple WordPress websites may be installed in a server. Therefore, at present, the "redis object cache" uses "'Database '= > 0." if there are two WordPress websites, it will cause conflicts. Therefore, the database serial number must be modified in the source code. For example, the first website is "'Database' = > 1" and the second website is "'Database '= > 2"

2、 Perfect repair suggestions:

(1). Write the 'port' and 'database' variables to the configuration file and put them in the root directory.

(2). If there is only one website in the user server and the port "6379" has not been modified, it will be run directly.

(3). If there are multiple websites in the user's server and the port "6379" has been modified, you can modify it to your own port number and database serial number in the file of "redis cache" and "conf.php". When upgrading later, this conf.php doesn't need to be overridden every time. In this way, the configuration can be configured once, and then it can be upgraded automatically.

Thank you again for your contribution and free sharing.

naxvog commented 3 years ago

Hi @hsxnet

some suggestions regarding your issues:

Generally you should add the configuration constants to your wp-config.php file to avoid editing plugin files and losing your configuration in the update process. WordPress removes the entire plugin's folder when updating, deleting every file in it and creating a new plugin folder for the plugin form scratch when updating (same process goes for theme-updates).

1.1 Custom Server Port for Security:

If you use just one (monolithic) server you can utilize unix sockets to connect to redis which is not only slightly faster but does not require an open port to begin with. You can change to sockets using define( 'WP_REDIS_SCHEME', 'unix' ); and define( 'WP_REDIS_PATH', /*change your path here*/ '/var/run/redis/redis.sock' ); (documentation).

If you still need to use ports use define( 'WP_REDIS_PORT', 19888 ); in your wp-config.php to change the default port to 19888 (documentation).

1.2 Database Selection / Collision Avoidance:

You have at least 2 options here. As you said it is possible to change the database number (redis its default configuration will create 10 databases) using define( 'WP_REDIS_DATABASE', 0 ); (documentation).

Alternatively you can change the key prefix. Changing the prefix will allow you to utilize a single redis database for multiple WordPress installations as every key is prefixed with the set value. A good starter-prefix would be the site's URL: define( 'WP_REDIS_PREFIX', 'www.example.com' );. If you host several WordPress installations I would suggest setting the prefix to a random string to avoid 'guessing-attacks' (or use multiple redis instances with different credentials/socket-locations in the first place).

2. Configurations Files:

You are effectively writing your own configuration file with the additions to wp-config.php so strictly speaking a separate configuration file is not needed (this might change in the future if a configuration GUI is introduced).

tillkruss commented 3 years ago

@hsxnet I generally use environment variables when I don't want to touch the php config files.

define( 'WP_REDIS_HOST', getenv( 'REDIS_HOST') );
define( 'WP_REDIS_DATABASE', getenv( 'REDIS_DB') ?: 0 );
hsxnet commented 3 years ago

Hi Till,

  1. Current situation of my website: (1) At present, three WordPress websites are installed on my server. (2) The database serial numbers is 1, 2and 3. (3) The default redis port is changed from 6379 to 19888.

  2. At present, my approach is as follows: (1) Add the following code under the site root directory of each site: wp-config.php define('WP CACHE KEY SALT', 'www.website-1.com'); Add the following code to the root directory of the first website: define ('wp ') CACHE KEY SALT', 'www.website-1.com'); Add the following code to the root directory of the second website: define ('wp ') CACHE KEY SALT', 'www.website-2.com'); Add the following code to the root directory of the second website: define ('wp ') CACHE KEY SALT', 'www.website-3.com'); (2) Each upgrade needs to modify the file: redis cache, includes, object cache.php Line 545: change port 6379 to 19888. Line 546: change the database serial number 0 to 2. 'port' => 19888, 'database' => 2, (3) Each upgrade needs to modify the file: redis cache, dependencies, predis, predis, SRC, connection, parameters.php Line 28: change port 6379 to 19888. (4) Each upgrade needs to modify the following files: redis cache, dependencies, colinmollenhour, credis, client.php Line 318: change port 6379 to 19888. (5) After the above modification, the redis cache of the three websites can be used normally.

  3. According to your last instruction: I add the following code under the site root directory of each site: wp-config.php define('WP REDIS PORT', getenv('REDIS PORT')?: 19888); define('WP REDIS DATABASE', getenv('REDIS DB')?: 3); Each website corresponds to a different data number. After saving, re enable the plug-in, which is invalid. Please continue to advise. Thank you very much.

tillkruss commented 3 years ago

Advise on what exactly?

naxvog commented 3 years ago

Add the following to the first site's wp-config.php:

define( 'WP_REDIS_PREFIX', 'www.website-1.com' );
define( 'WP_REDIS_PORT', 19888 );

Using the prefix you don't need to change the redis database. If you want to change the database regardless you may add define( 'WP_REDIS_DATABASE', 1 ) or your chosen DB.

Change the given snippet to suit the second and third site and add it to their wp-config.php accordingly.

Using this approach you don't need to change any plugin files and the configuration will persist even after a plugin update.

hsxnet commented 3 years ago

Add the following to the first site's wp-config.php:

define( 'WP_REDIS_PREFIX', 'www.website-1.com' );
define( 'WP_REDIS_PORT', 19888 );

Using the prefix you don't need to change the redis database. If you want to change the database regardless you may add define( 'WP_REDIS_DATABASE', 1 ) or your chosen DB.

Change the given snippet to suit the second and third site and add it to their wp-config.php accordingly.

Using this approach you don't need to change any plugin files and the configuration will persist even after a plugin update.

Thank you very much. I follow your method to configure: wp-config.php

define( 'WP_REDIS_PREFIX', 'www.website-1.com' ); define( 'WP_REDIS_PORT', 19888 ); define( 'WP_REDIS_DATABASE', 1 );

However, the plug-in is not enabled normally. The error is as follows:

【Diagnostics】 Errors: [ "Connection refused" ]

【Overview】 Status: Not connected

Can you tell me how to repair and use it successfully?

Thanks!

naxvog commented 3 years ago

Generally for every configuration change you have made to the plugin directly, there should be a configuration constant that you can set. If the list you provided was complete the plugin should connect to the redis server. The wiki is listing every available connection and configuration constant.

Depending on how exactly you modified the plugin's code you may want to revert the changes you have made to the plugin or just reinstall the plugin.

Also be aware that any changes to the wp-config.php must be made above the following comment or at least before the last require_once statement in the file.

/* That's all, stop editing! Happy publishing. */

Example with the configuration snippet (keep in mind that I have omitted the beginning of the config file):

/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give each
 * a unique prefix. Only numbers, letters, and underscores please!
 */
$table_prefix = 'wp_';

/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * For information on other constants that can be used for debugging,
 * visit the documentation.
 *
 * @link https://wordpress.org/support/article/debugging-in-wordpress/
 */
define( 'WP_DEBUG', false );

/**
 * WP Redis Cache configuration
 *
 * @link https://github.com/rhubarbgroup/redis-cache/wiki
 */
define( 'WP_REDIS_PREFIX', 'www.website-1.com' );
define( 'WP_REDIS_PORT', 19888 );
define( 'WP_REDIS_DATABASE', 1 );

/* That's all, stop editing! Happy publishing. */

/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
    define( 'ABSPATH', __DIR__ . '/' );
}

/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';
hsxnet commented 3 years ago

Generally for every configuration change you have made to the plugin directly, there should be a configuration constant that you can set. If the list you provided was complete the plugin should connect to the redis server. The wiki is listing every available connection and configuration constant.

Depending on how exactly you modified the plugin's code you may want to revert the changes you have made to the plugin or just reinstall the plugin.

Also be aware that any changes to the wp-config.php must be made above the following comment or at least before the last require_once statement in the file.

/* That's all, stop editing! Happy publishing. */

Example with the configuration snippet (keep in mind that I have omitted the beginning of the config file):

/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give each
 * a unique prefix. Only numbers, letters, and underscores please!
 */
$table_prefix = 'wp_';

/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * For information on other constants that can be used for debugging,
 * visit the documentation.
 *
 * @link https://wordpress.org/support/article/debugging-in-wordpress/
 */
define( 'WP_DEBUG', false );

/**
 * WP Redis Cache configuration
 *
 * @link https://github.com/rhubarbgroup/redis-cache/wiki
 */
define( 'WP_REDIS_PREFIX', 'www.website-1.com' );
define( 'WP_REDIS_PORT', 19888 );
define( 'WP_REDIS_DATABASE', 1 );

/* That's all, stop editing! Happy publishing. */

/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
  define( 'ABSPATH', __DIR__ . '/' );
}

/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';

Thank you very much for your help!

The reason why the plugin failed to start was found: I misplaced this section of the configuration.I put it at the end of WP-CONFIG.PHP, so it doesn't work.

Following your demonstration, I moved to the demonstration location and now everything is fine.

Thank you very much again!

Have a good time.