From 5b85a81bb178311b5b23ec526a18aadcf9b0e9e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Till=20Kr=C3=BCss?= Date: Mon, 6 Oct 2025 09:21:47 -0700 Subject: [PATCH 1/4] delete transients --- CHANGELOG.md | 4 ++++ includes/class-plugin.php | 26 ++++++++++++++++++++++++++ includes/cli/class-commands.php | 8 ++++++++ 3 files changed, 38 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6643efa..b4d45f59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +- Delete transients when cache is enabled + ## 2.7.0 - Preserve key TTL when calling (in|de)crement methods diff --git a/includes/class-plugin.php b/includes/class-plugin.php index 213f53ef..edc0bde4 100644 --- a/includes/class-plugin.php +++ b/includes/class-plugin.php @@ -119,6 +119,8 @@ public function add_actions_and_filters() { add_action( 'wp_head', [ $this, 'register_shutdown_hooks' ] ); + add_action( 'redis_object_cache_enable', [ $this, 'maybe_delete_transients' ] ); + add_filter( 'qm/collectors', [ $this, 'register_qm_collector' ], 25 ); add_filter( 'qm/outputter/html', [ $this, 'register_qm_output' ] ); @@ -1204,6 +1206,30 @@ public function register_shutdown_hooks() { } } + /** + * Registers all hooks associated with the shutdown hook + * + * @param bool $result + * @return void + */ + public function maybe_delete_transients( $result ) { + global $wpdb; + + if ( ! $result ) { return; } + + $wpdb->query( $wpdb->prepare( + "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", + '_transient_%', + '_site_transient_%' + ) ); + + if ( is_multisite() ) { + $wpdb->query( $wpdb->prepare( + "DELETE FROM {$wpdb->sitemeta} WHERE meta_key LIKE %s", '_site_transient_%' + ) ); + } + } + /** * Displays the redis cache html comment * diff --git a/includes/cli/class-commands.php b/includes/cli/class-commands.php index 9957f851..13d1e42b 100644 --- a/includes/cli/class-commands.php +++ b/includes/cli/class-commands.php @@ -77,6 +77,14 @@ public function enable() { FS_CHMOD_FILE ); + /** + * Fires on cache enable event + * + * @since 1.3.5 + * @param bool $result Whether the filesystem event (copy of the `object-cache.php` file) was successful. + */ + do_action( 'redis_object_cache_enable', $result ); + if ( $copy ) { WP_CLI::success( __( 'Object cache enabled.', 'redis-cache' ) ); } else { From f90beae75d68298cb0ca019a050c8960dd149529 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Till=20Kr=C3=BCss?= Date: Mon, 6 Oct 2025 09:23:32 -0700 Subject: [PATCH 2/4] fix var name --- includes/cli/class-commands.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/cli/class-commands.php b/includes/cli/class-commands.php index 13d1e42b..8503c228 100644 --- a/includes/cli/class-commands.php +++ b/includes/cli/class-commands.php @@ -83,7 +83,7 @@ public function enable() { * @since 1.3.5 * @param bool $result Whether the filesystem event (copy of the `object-cache.php` file) was successful. */ - do_action( 'redis_object_cache_enable', $result ); + do_action( 'redis_object_cache_enable', $copy ); if ( $copy ) { WP_CLI::success( __( 'Object cache enabled.', 'redis-cache' ) ); From 07a76890c2247e658a9fcfd8508d85edf7af2252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Till=20Kr=C3=BCss?= Date: Mon, 6 Oct 2025 09:25:41 -0700 Subject: [PATCH 3/4] update comment --- includes/class-plugin.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/includes/class-plugin.php b/includes/class-plugin.php index edc0bde4..f2ffd039 100644 --- a/includes/class-plugin.php +++ b/includes/class-plugin.php @@ -1207,7 +1207,8 @@ public function register_shutdown_hooks() { } /** - * Registers all hooks associated with the shutdown hook + * Delete all transients if the cache was enabled successfully. + * Callback for `redis_object_cache_enable` action. * * @param bool $result * @return void @@ -1215,7 +1216,9 @@ public function register_shutdown_hooks() { public function maybe_delete_transients( $result ) { global $wpdb; - if ( ! $result ) { return; } + if ( ! $result ) { + return; + } $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", From aee6e97a2b1c785fe775821219586f941b952aee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Till=20Kr=C3=BCss?= Date: Mon, 6 Oct 2025 11:58:06 -0700 Subject: [PATCH 4/4] Rename parameter in maybe_delete_transients method Update parameter name in maybe_delete_transients method. --- includes/class-plugin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-plugin.php b/includes/class-plugin.php index f2ffd039..141ef18b 100644 --- a/includes/class-plugin.php +++ b/includes/class-plugin.php @@ -1210,10 +1210,10 @@ public function register_shutdown_hooks() { * Delete all transients if the cache was enabled successfully. * Callback for `redis_object_cache_enable` action. * - * @param bool $result + * @param bool $should_delete * @return void */ - public function maybe_delete_transients( $result ) { + public function maybe_delete_transients( $should_delete ) { global $wpdb; if ( ! $result ) {