Skip to content

Commit e646bd5

Browse files
authored
Add wp cache flush-group for flushing a cache group (#85)
* Added group flushing. Needs extra input for testing. * Added failsafe for older WP versions. Also phpcs formatting. * Processed feedback. * Added test for error on clear-group * Renamed clear-group to flush-group Fixed failing test and generated readme file. * Set correct error message in the test. * Make sure a test only runs at 6.1 or later.
1 parent cdc371f commit e646bd5

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,27 @@ Errors if the object cache can't be flushed.
164164

165165

166166

167+
### wp cache flush-group
168+
169+
Removes all cache items in a group, if the object cache implementation supports it.
170+
171+
~~~
172+
wp cache flush-group <group>
173+
~~~
174+
175+
**OPTIONS**
176+
177+
<group>
178+
Cache group key.
179+
180+
**EXAMPLES**
181+
182+
# Clear cache group.
183+
$ wp cache flush-group my_group
184+
Success: Cache group 'my_group' was flushed.
185+
186+
187+
167188
### wp cache get
168189

169190
Gets a value from the object cache.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"cache decr",
3838
"cache delete",
3939
"cache flush",
40+
"cache flush-group",
4041
"cache get",
4142
"cache incr",
4243
"cache replace",

features/cache.feature

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,59 @@ Feature: Managed the WordPress object cache
133133
Error: Could not replace object 'bar' in group 'foo'. Does it not exist?
134134
"""
135135

136+
@require-wp-6.1
137+
Scenario: Some cache groups cannot be cleared.
138+
Given a WP install
139+
When I run `wp cache flush-group add_multiple`
140+
Then STDOUT should be:
141+
"""
142+
Success: Cache group 'add_multiple' was flushed.
143+
"""
144+
145+
@require-wp-6.1
146+
Scenario: Some cache groups cannot be cleared.
147+
Given a WP install
148+
And a wp-content/mu-plugins/unclearable-test-cache.php file:
149+
"""php
150+
<?php
151+
class Dummy_Object_Cache extends WP_Object_Cache {
152+
public function flush_group( $group ) {
153+
if ( $group === 'permanent_root_cache' ) {
154+
return false;
155+
}
156+
return parent::flush_group( $group );
157+
}
158+
}
159+
$GLOBALS['wp_object_cache'] = new Dummy_Object_Cache();
160+
"""
161+
When I try `wp cache flush-group permanent_root_cache`
162+
Then STDERR should be:
163+
"""
164+
Error: Cache group 'permanent_root_cache' was not flushed.
165+
"""
166+
167+
@less-than-wp-6.1
168+
Scenario: Some cache groups cannot be cleared.
169+
Given a WP install
170+
And a wp-content/mu-plugins/unclearable-test-cache.php file:
171+
"""php
172+
<?php
173+
class Dummy_Object_Cache extends WP_Object_Cache {
174+
public function flush_group( $group ) {
175+
if ( $group === 'permanent_root_cache' ) {
176+
return false;
177+
}
178+
return parent::flush_group( $group );
179+
}
180+
}
181+
$GLOBALS['wp_object_cache'] = new Dummy_Object_Cache();
182+
"""
183+
When I try `wp cache flush-group permanent_root_cache`
184+
Then STDERR should be:
185+
"""
186+
Error: Group flushing is not supported.
187+
"""
188+
136189
Scenario: Flushing cache on a multisite installation
137190
Given a WP multisite installation
138191

src/Cache_Command.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,33 @@ public function supports( $args, $assoc_args ) {
378378
WP_CLI::halt( 1 );
379379
}
380380

381+
/**
382+
* Removes all cache items in a group, if the object cache implementation supports it.
383+
*
384+
* ## OPTIONS
385+
*
386+
* <group>
387+
* : Cache group key.
388+
*
389+
* ## EXAMPLES
390+
*
391+
* # Clear cache group.
392+
* $ wp cache flush-group my_group
393+
* Success: Cache group 'my_group' was flushed.
394+
*
395+
* @subcommand flush-group
396+
*/
397+
public function flush_group( $args, $assoc_args ) {
398+
list( $group ) = $args;
399+
400+
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
401+
WP_CLI::error( 'Group flushing is not supported.' );
402+
}
403+
404+
if ( ! wp_cache_flush_group( $group ) ) {
405+
WP_CLI::error( "Cache group '$group' was not flushed." );
406+
}
407+
WP_CLI::success( "Cache group '$group' was flushed." );
408+
}
409+
381410
}

0 commit comments

Comments
 (0)