From 8f596927f753b2cbbdbd3d82105e7fe403cb0a5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Till=20Kru=CC=88ss?= Date: Sat, 7 Oct 2023 09:29:56 -0700 Subject: [PATCH] wip --- README.md | 6 +++- includes/class-plugin.php | 16 ++------- includes/class-predis.php | 62 ++++++++++++++++++++++++++------- includes/cli/class-commands.php | 15 ++------ 4 files changed, 59 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index c720fda2..28970614 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # Redis Object Cache for WordPress +Update Banner: Fast load times; Less Database load; screenshots +Add: SpinupWP, BlueHost, LiquidWeb and other platforms that use it to the banner maybe also to the readme + A persistent object cache backend powered by Redis®¹. Supports [Predis](https://github.com/predis/predis/), [PhpRedis (PECL)](https://github.com/phpredis/phpredis), [Relay](https://relaycache.com), replication, sentinels, clustering and [WP-CLI](http://wp-cli.org/). [![Redis Object Cache screenshots](/.wordpress-org/collage-sm.jpg?raw=true)](/.wordpress-org/collage.png?raw=true) @@ -41,7 +44,8 @@ The Redis Object Cache plugin comes with vast set of configuration options. If y | `WP_REDIS_MAXTTL` | `0` | The maximum time-to-live of cache keys | | `WP_REDIS_CLIENT` | | The client used to communicate with Redis. Defaults to `phpredis` when installed, otherwise `predis`. Supports `phpredis`, `predis`, `relay` | | `WP_REDIS_TIMEOUT` | `1` | The connection timeout in seconds | -| `WP_REDIS_READ_TIMEOUT` | `1` | The timeout in seconds when reading/writing | +| `WP_REDIS_READ_TIMEOUT` | `1` | The timeout in seconds when reading/writing | +| `WP_REDIS_FLUSH_TIMEOUT` | `5` | The timeout in seconds when flushing | | `WP_REDIS_IGNORED_GROUPS` | `[]` | Groups that should not be cached between requests in Redis |
diff --git a/includes/class-plugin.php b/includes/class-plugin.php index 94a019c3..958e0439 100644 --- a/includes/class-plugin.php +++ b/includes/class-plugin.php @@ -946,13 +946,7 @@ public function do_admin_actions() { ); if ( $result ) { - try { - $predis = new Predis(); - $predis->flush(); - } catch ( Exception $exception ) { - // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log - error_log( $exception ); - } + (new Predis)->flush(); } /** @@ -982,13 +976,7 @@ public function do_admin_actions() { $result = $wp_filesystem->delete( WP_CONTENT_DIR . '/object-cache.php' ); if ( $result ) { - try { - $predis = new Predis(); - $predis->flush(); - } catch ( Exception $exception ) { - // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log - error_log( $exception ); - } + (new Predis)->flush(); } /** diff --git a/includes/class-predis.php b/includes/class-predis.php index 2fe11c5d..8171bbb8 100644 --- a/includes/class-predis.php +++ b/includes/class-predis.php @@ -24,7 +24,7 @@ class Predis { * * @return void */ - public function connect() { + public function connect( $read_timeout = null ) { // Load bundled Predis library. if ( ! class_exists( '\Predis\Client' ) ) { require_once WP_REDIS_PLUGIN_PATH . '/dependencies/predis/predis/autoload.php'; @@ -39,7 +39,7 @@ public function connect() { 'port' => 6379, 'database' => 0, 'timeout' => 1, - 'read_timeout' => 1, + 'read_timeout' => $read_timeout ?? 1, ]; $settings = [ @@ -128,13 +128,26 @@ public function connect() { } /** - * Invalidate all items in the cache. + * Flushes the entire Redis database using the `WP_REDIS_FLUSH_TIMEOUT`. * - * @return bool True on success, false on failure. + * @param bool $throw_exception Whether to throw exception on error. + * @return bool */ - public function flush() { + public function flush( $throw_exception = false ) { + $flush_timeout = defined( 'WP_REDIS_FLUSH_TIMEOUT' ) + ? intval(WP_REDIS_FLUSH_TIMEOUT) + : 5; + if ( is_null( $this->redis ) ) { - $this->connect(); + try { + $this->connect( $flush_timeout ); + } catch ( Exception $exception ) { + if ( $throw_exception ) { + throw $exception; + } + + return false; + } } if ( defined( 'WP_REDIS_CLUSTER' ) ) { @@ -143,23 +156,46 @@ public function flush() { $this->redis->flushdb( $master ); } } catch ( Exception $exception ) { + if ( $throw_exception ) { + throw $exception; + } + return false; } - } else { - try { - $this->redis->flushdb(); - } catch ( Exception $exception ) { - return false; + + return true; + } + + try { + // TODO: test Predis return value... + + var_dump($this->redis->flushdb()); + exit; + + return $this->redis->flushdb(); + } catch ( Exception $exception ) { + if ( $throw_exception ) { + throw $exception; } + + return false; } + } - return true; + /** + * Flushes the entire Redis database using the `WP_REDIS_FLUSH_TIMEOUT` + * and will throw an exception if anything goes wrong. + * + * @return bool + */ + public function flushOrFail() { + return $this->flush( true ); } /** * Builds a clean connection array out of redis clusters array. * - * @return array + * @return array */ protected function build_cluster_connection_array() { $cluster = array_values( WP_REDIS_CLUSTER ); diff --git a/includes/cli/class-commands.php b/includes/cli/class-commands.php index 3c816f96..7628f616 100644 --- a/includes/cli/class-commands.php +++ b/includes/cli/class-commands.php @@ -173,20 +173,11 @@ public function update_dropin() { */ protected function flush_redis() { try { - $predis = new Predis(); - $predis->connect(); + return (new Predis)->flushOrFail(); } catch ( Exception $exception ) { - return $exception->getMessage(); - } + error_log( $exception ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log - try { - $predis->flush(); - } catch ( Exception $exception ) { - // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log - error_log( $exception ); + return $exception->getMessage(); } - - return true; } - }