Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <group>
~~~

**OPTIONS**

<group>
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.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"cache decr",
"cache delete",
"cache flush",
"cache flush-group",
"cache get",
"cache incr",
"cache replace",
Expand Down
53 changes: 53 additions & 0 deletions features/cache.feature
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,59 @@ 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:
"""
Success: Cache group 'add_multiple' was flushed.
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll want to move this step to a new scenario so you can control it with @require-wp-6.1


@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
<?php
class Dummy_Object_Cache extends WP_Object_Cache {
public function flush_group( $group ) {
if ( $group === 'permanent_root_cache' ) {
return false;
}
return parent::flush_group( $group );
}
}
$GLOBALS['wp_object_cache'] = new Dummy_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.
"""

@less-than-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
<?php
class Dummy_Object_Cache extends WP_Object_Cache {
public function flush_group( $group ) {
if ( $group === 'permanent_root_cache' ) {
return false;
}
return parent::flush_group( $group );
}
}
$GLOBALS['wp_object_cache'] = new Dummy_Object_Cache();
"""
When I try `wp cache flush-group permanent_root_cache`
Then STDERR should be:
"""
Error: Group flushing is not supported.
"""

Scenario: Flushing cache on a multisite installation
Given a WP multisite installation

Expand Down
29 changes: 29 additions & 0 deletions src/Cache_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,33 @@ public function supports( $args, $assoc_args ) {
WP_CLI::halt( 1 );
}

/**
* Removes all cache items in a group, if the object cache implementation supports it.
*
* ## OPTIONS
*
* <group>
* : Cache group key.
*
* ## EXAMPLES
*
* # Clear cache group.
* $ wp cache flush-group my_group
* Success: Cache group 'my_group' was flushed.
*
* @subcommand flush-group
*/
public function flush_group( $args, $assoc_args ) {
list( $group ) = $args;

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::error( "Cache group '$group' was not flushed." );
}
WP_CLI::success( "Cache group '$group' was flushed." );
}

}