From e457f5e8628bc4faf6d1f42d381e20f29b5bbacc Mon Sep 17 00:00:00 2001 From: Jan Willem Oostendorp Date: Wed, 5 Apr 2023 16:17:31 +0200 Subject: [PATCH 1/7] Added group flushing. Needs extra input for testing. --- features/cache.feature | 17 +++++++++++++++++ src/Cache_Command.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/features/cache.feature b/features/cache.feature index 472f28d19..8bfbb8a65 100644 --- a/features/cache.feature +++ b/features/cache.feature @@ -132,3 +132,20 @@ Feature: Managed the WordPress object cache """ Error: Could not replace object 'bar' in group 'foo'. Does it not exist? """ + + When I run `wp cache clear-group add_multiple` + Then STDOUT should be: + """ + Success: Cache group 'add_multiple' was flushed. + """ + + When I try `wp cache clear-group non_existing` + Then STDERR should be: + """ + Error: Cache group 'non_existing' is not supported. + """ + When I try `wp cache clear-group false_return` + Then STDERR should be: + """ + Warning: Cache group 'false_return' was not be flushed. + """ diff --git a/src/Cache_Command.php b/src/Cache_Command.php index 1b5c8b172..10f4cc285 100644 --- a/src/Cache_Command.php +++ b/src/Cache_Command.php @@ -339,4 +339,32 @@ public function type( $args, $assoc_args ) { WP_CLI::line( $message ); } + /** + * Removes all cache items in a group, if the object cache implementation supports it. + * + * ## OPTIONS + * + * + * : Cache group key. + * + * ## EXAMPLES + * + * # Clear cache group. + * $ wp cache clear-group my_group + * + * @subcommand clear-group + */ + public function clear_group( $args, $assoc_args ) { + list( $group ) = $args; + + if (! wp_cache_supports( 'flush_group' )) { + WP_CLI::error( "Group flushing is not supported." ); + } + + if ( ! wp_cache_flush_group( $group ) ) { + WP_CLI::warning("Cache group '$group' was not be flushed." ); + } + WP_CLI::success( "Cache group '$group' was flushed." ); + } + } From ac0f8e07e57aed73f452929067455f3e3d61b376 Mon Sep 17 00:00:00 2001 From: Jan Willem Oostendorp Date: Wed, 5 Apr 2023 17:11:32 +0200 Subject: [PATCH 2/7] Added failsafe for older WP versions. Also phpcs formatting. --- src/Cache_Command.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Cache_Command.php b/src/Cache_Command.php index 10f4cc285..563b7710f 100644 --- a/src/Cache_Command.php +++ b/src/Cache_Command.php @@ -357,12 +357,16 @@ public function type( $args, $assoc_args ) { public function clear_group( $args, $assoc_args ) { list( $group ) = $args; - if (! wp_cache_supports( 'flush_group' )) { - WP_CLI::error( "Group flushing is not supported." ); + if ( ! function_exists( 'wp_cache_supports' ) ) { + WP_CLI::error( 'Checking cache features is only available in WordPress 6.1 and higher' ); + } + + if ( ! wp_cache_supports( 'flush_group' ) ) { + WP_CLI::error( 'Group flushing is not supported.' ); } if ( ! wp_cache_flush_group( $group ) ) { - WP_CLI::warning("Cache group '$group' was not be flushed." ); + WP_CLI::warning( "Cache group '$group' was not be flushed." ); } WP_CLI::success( "Cache group '$group' was flushed." ); } From f64f35fed7abffa3d8d8a2984085ce8c18e1360c Mon Sep 17 00:00:00 2001 From: Jan Willem Oostendorp Date: Fri, 7 Apr 2023 15:18:46 +0200 Subject: [PATCH 3/7] Processed feedback. --- features/cache.feature | 1 + src/Cache_Command.php | 9 +++------ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/features/cache.feature b/features/cache.feature index 8bfbb8a65..6b81a344e 100644 --- a/features/cache.feature +++ b/features/cache.feature @@ -144,6 +144,7 @@ Feature: Managed the WordPress object cache """ Error: Cache group 'non_existing' is not supported. """ + When I try `wp cache clear-group false_return` Then STDERR should be: """ diff --git a/src/Cache_Command.php b/src/Cache_Command.php index 563b7710f..ce8bdff54 100644 --- a/src/Cache_Command.php +++ b/src/Cache_Command.php @@ -351,22 +351,19 @@ public function type( $args, $assoc_args ) { * * # Clear cache group. * $ wp cache clear-group my_group + * Success: Cache group 'my_group' was flushed. * * @subcommand clear-group */ public function clear_group( $args, $assoc_args ) { list( $group ) = $args; - if ( ! function_exists( 'wp_cache_supports' ) ) { - WP_CLI::error( 'Checking cache features is only available in WordPress 6.1 and higher' ); - } - - if ( ! wp_cache_supports( 'flush_group' ) ) { + if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) { WP_CLI::error( 'Group flushing is not supported.' ); } if ( ! wp_cache_flush_group( $group ) ) { - WP_CLI::warning( "Cache group '$group' was not be flushed." ); + WP_CLI::error( "Cache group '$group' was not flushed." ); } WP_CLI::success( "Cache group '$group' was flushed." ); } From 4b8c33976dab6acf566552d0ca222e475d942e79 Mon Sep 17 00:00:00 2001 From: Jan Willem Oostendorp Date: Mon, 17 Apr 2023 16:38:39 +0200 Subject: [PATCH 4/7] Added test for error on clear-group --- features/cache.feature | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/features/cache.feature b/features/cache.feature index 9658bf9e2..25cc60c6d 100644 --- a/features/cache.feature +++ b/features/cache.feature @@ -139,16 +139,25 @@ Feature: Managed the WordPress object cache Success: Cache group 'add_multiple' was flushed. """ - When I try `wp cache clear-group non_existing` - Then STDERR should be: - """ - Error: Cache group 'non_existing' is not supported. - """ - - When I try `wp cache clear-group false_return` + Scenario: Some cache groups cannot be cleared. + Given a WP install + And a wp-content/mu-plugins/unclearable-test-cache.php file: + """php + Date: Wed, 26 Apr 2023 13:37:54 +0200 Subject: [PATCH 5/7] Renamed clear-group to flush-group Fixed failing test and generated readme file. --- README.md | 21 +++++++++++++++++++++ composer.json | 1 + features/cache.feature | 27 +++++++++++++++++++++++++-- src/Cache_Command.php | 6 +++--- 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 10c91ecce..962bc4483 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,27 @@ Errors if the object cache can't be flushed. +### wp cache flush-group + +Removes all cache items in a group, if the object cache implementation supports it. + +~~~ +wp cache flush-group +~~~ + +**OPTIONS** + + + Cache group key. + +**EXAMPLES** + + # Clear cache group. + $ wp cache flush-group my_group + Success: Cache group 'my_group' was flushed. + + + ### wp cache get Gets a value from the object cache. diff --git a/composer.json b/composer.json index 3fbceb9c8..b5294858b 100644 --- a/composer.json +++ b/composer.json @@ -37,6 +37,7 @@ "cache decr", "cache delete", "cache flush", + "cache flush-group", "cache get", "cache incr", "cache replace", diff --git a/features/cache.feature b/features/cache.feature index 25cc60c6d..a45105148 100644 --- a/features/cache.feature +++ b/features/cache.feature @@ -133,12 +133,35 @@ Feature: Managed the WordPress object cache Error: Could not replace object 'bar' in group 'foo'. Does it not exist? """ - When I run `wp cache clear-group add_multiple` + When I run `wp cache flush-group add_multiple` Then STDOUT should be: """ Success: Cache group 'add_multiple' was flushed. """ + @require-wp-6.1 + Scenario: Some cache groups cannot be cleared. + Given a WP install + And a wp-content/mu-plugins/unclearable-test-cache.php file: + """php + Date: Wed, 26 Apr 2023 14:25:35 +0200 Subject: [PATCH 6/7] Set correct error message in the test. --- features/cache.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/cache.feature b/features/cache.feature index a45105148..fd49984f6 100644 --- a/features/cache.feature +++ b/features/cache.feature @@ -180,7 +180,7 @@ Feature: Managed the WordPress object cache When I try `wp cache flush-group permanent_root_cache` Then STDERR should be: """ - Error: Cache group 'permanent_root_cache' was not flushed. + Error: Group flushing is not supported. """ Scenario: Flushing cache on a multisite installation From e8d2fa86b1903aef7c5c3c18ee5127e4d16cc749 Mon Sep 17 00:00:00 2001 From: Jan Willem Oostendorp Date: Wed, 26 Apr 2023 14:33:33 +0200 Subject: [PATCH 7/7] Make sure a test only runs at 6.1 or later. --- features/cache.feature | 3 +++ 1 file changed, 3 insertions(+) diff --git a/features/cache.feature b/features/cache.feature index fd49984f6..0626dfd58 100644 --- a/features/cache.feature +++ b/features/cache.feature @@ -133,6 +133,9 @@ Feature: Managed the WordPress object cache Error: Could not replace object 'bar' in group 'foo'. Does it not exist? """ + @require-wp-6.1 + Scenario: Some cache groups cannot be cleared. + Given a WP install When I run `wp cache flush-group add_multiple` Then STDOUT should be: """