Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@
"cache decr",
"cache delete",
"cache flush",
"cache flush-comment",
"cache flush-group",
"cache flush-option",
"cache flush-post",
"cache flush-term",
"cache flush-user",
"cache get",
"cache incr",
"cache patch",
Expand Down
98 changes: 98 additions & 0 deletions features/cache-flush-granular.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
Feature: Granular cache flushing operations

@skip-object-cache
Scenario: Flush specific post cache
Given a WP install
And a wp-content/mu-plugins/test-harness.php file:
"""
<?php
$cache_post = function(){
wp_cache_set( 'post_123', array( 'ID' => 123, 'post_title' => 'Test' ), 'posts' );
wp_cache_set( 'meta_123', array( 'key' => 'value' ), 'post_meta' );
};
WP_CLI::add_hook( 'before_invoke:cache flush-post', $cache_post );
Comment on lines +9 to +24
"""

When I run `wp cache flush-post 123`
Then STDOUT should contain:
"""
Success: Post cache for ID 123 cleared.
"""
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

@skip-object-cache
Scenario: Flush specific term cache
Given a WP install
And a wp-content/mu-plugins/test-harness.php file:
"""
<?php
$cache_term = function(){
wp_cache_set( 'term_5', array( 'term_id' => 5 ), 'terms' );
wp_cache_set( 'term_meta_5', array(), 'term_meta' );
};
WP_CLI::add_hook( 'before_invoke:cache flush-term', $cache_term );
"""

When I run `wp cache flush-term 5`
Then STDOUT should contain:
"""
Success: Term cache for ID 5 cleared.
"""

@skip-object-cache
Scenario: Flush specific comment cache
Given a WP install
And a wp-content/mu-plugins/test-harness.php file:
"""
<?php
$cache_comment = function(){
wp_cache_set( 'comment_42', array(), 'comment' );
wp_cache_set( 'comment_meta_42', array(), 'comment_meta' );
};
WP_CLI::add_hook( 'before_invoke:cache flush-comment', $cache_comment );
"""

When I run `wp cache flush-comment 42`
Then STDOUT should contain:
"""
Success: Comment cache for ID 42 cleared.
"""

@skip-object-cache
Scenario: Flush specific user cache
Given a WP install
And a wp-content/mu-plugins/test-harness.php file:
"""
<?php
$cache_user = function(){
wp_cache_set( 'user_1', array( 'ID' => 1 ), 'users' );
wp_cache_set( 'user_meta_1', array(), 'user_meta' );
};
WP_CLI::add_hook( 'before_invoke:cache flush-user', $cache_user );
"""

When I run `wp cache flush-user 1`
Then STDOUT should contain:
"""
Success: User cache for ID 1 cleared.
"""

@skip-object-cache
Scenario: Flush specific option cache
Given a WP install
And a wp-content/mu-plugins/test-harness.php file:
"""
<?php
$cache_option = function(){
wp_cache_set( 'my_option', 'value', 'options' );
wp_cache_set( 'alloptions', array( 'my_option' => 'value' ), 'options' );
};
WP_CLI::add_hook( 'before_invoke:cache flush-option', $cache_option );
"""

When I run `wp cache flush-option my_option`
Then STDOUT should contain:
"""
Success: Option cache for 'my_option' cleared.
"""


190 changes: 190 additions & 0 deletions src/Cache_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,194 @@ function ( $key ) {
}
}
}

/**
* Clears post related caches.
*
* @subcommand flush-post
*
* ## OPTIONS
*
* [<id>]
* : Post ID. If not specified, clears all post caches.
*
* ## EXAMPLES
*
* # Clear all post caches.
* $ wp cache flush-post
* Success: Post caches cleared.
*
* # Clear cache for a specific post.
* $ wp cache flush-post 123
* Success: Post cache for ID 123 cleared.
*
* @param array<string> $args Positional arguments.
*/
public function flush_post( $args ) {
$post_id = ! empty( $args ) ? (int) $args[0] : null;

if ( $post_id ) {
clean_post_cache( $post_id );
WP_CLI::success( "Post cache for ID $post_id cleared." );
Comment thread
alaminfirdows marked this conversation as resolved.
Resolved
} else {
if ( ! wp_using_ext_object_cache() || ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
WP_CLI::error( 'Flushing all post caches requires WordPress 6.1+' );
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
wp_cache_flush_group( 'posts' );
wp_cache_flush_group( 'post_meta' );
Comment thread
alaminfirdows marked this conversation as resolved.
Resolved
WP_CLI::success( 'Post caches cleared.' );
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
}

/**
* Clears term related caches.
*
* @subcommand flush-term
*
* ## OPTIONS
*
* [<id>]
* : Term ID. If not specified, clears all term caches.
*
* ## EXAMPLES
*
* # Clear all term caches.
* $ wp cache flush-term
* Success: Term caches cleared.
*
* # Clear cache for a specific term.
* $ wp cache flush-term 5
* Success: Term cache for ID 5 cleared.
*
* @param array<string> $args Positional arguments.
*/
public function flush_term( $args ) {
$term_id = ! empty( $args ) ? (int) $args[0] : null;

if ( $term_id ) {
clean_term_cache( $term_id );
WP_CLI::success( "Term cache for ID $term_id cleared." );
Comment thread
alaminfirdows marked this conversation as resolved.
Resolved
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
} else {
if ( ! wp_using_ext_object_cache() || ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
WP_CLI::error( 'Flushing all term caches requires WordPress 6.1+' );
}
wp_cache_flush_group( 'terms' );
wp_cache_flush_group( 'term_meta' );
Comment thread
alaminfirdows marked this conversation as resolved.
Resolved
WP_CLI::success( 'Term caches cleared.' );
}
}

/**
* Clears comment related caches.
*
* @subcommand flush-comment
*
* ## OPTIONS
*
* [<id>]
* : Comment ID. If not specified, clears all comment caches.
*
* ## EXAMPLES
*
* # Clear all comment caches.
* $ wp cache flush-comment
* Success: Comment caches cleared.
*
* # Clear cache for a specific comment.
* $ wp cache flush-comment 42
* Success: Comment cache for ID 42 cleared.
*
* @param array<string> $args Positional arguments.
*/
public function flush_comment( $args ) {
$comment_id = ! empty( $args ) ? (int) $args[0] : null;

if ( $comment_id ) {
clean_comment_cache( $comment_id );
WP_CLI::success( "Comment cache for ID $comment_id cleared." );
Comment thread
alaminfirdows marked this conversation as resolved.
Resolved
} else {
if ( ! wp_using_ext_object_cache() || ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
WP_CLI::error( 'Flushing all comment caches requires WordPress 6.1+' );
}
wp_cache_flush_group( 'comment' );
wp_cache_flush_group( 'comment_meta' );
Comment thread
alaminfirdows marked this conversation as resolved.
Resolved
WP_CLI::success( 'Comment caches cleared.' );
}
}

/**
* Clears user related caches.
*
* @subcommand flush-user
*
* ## OPTIONS
*
* [<id>]
* : User ID. If not specified, clears all user caches.
*
* ## EXAMPLES
*
* # Clear all user caches.
* $ wp cache flush-user
* Success: User caches cleared.
*
* # Clear cache for a specific user.
* $ wp cache flush-user 1
* Success: User cache for ID 1 cleared.
*
* @param array<string> $args Positional arguments.
*/
public function flush_user( $args ) {
$user_id = ! empty( $args ) ? (int) $args[0] : null;

if ( $user_id ) {
clean_user_cache( $user_id );
WP_CLI::success( "User cache for ID $user_id cleared." );
Comment thread
alaminfirdows marked this conversation as resolved.
Resolved
} else {
if ( ! wp_using_ext_object_cache() || ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
WP_CLI::error( 'Flushing all user caches requires WordPress 6.1+' );
}
wp_cache_flush_group( 'users' );
wp_cache_flush_group( 'user_meta' );
Comment thread
alaminfirdows marked this conversation as resolved.
Resolved
WP_CLI::success( 'User caches cleared.' );
}
}

/**
* Clears option related caches.
*
* @subcommand flush-option
*
* ## OPTIONS
*
* [<name>]
* : Option name. If not specified, clears all option caches.
*
* ## EXAMPLES
*
* # Clear all option caches.
* $ wp cache flush-option
* Success: Option caches cleared.
*
* # Clear cache for a specific option.
* $ wp cache flush-option my_option
* Success: Option cache for 'my_option' cleared.
*
* @param array<string> $args Positional arguments.
*/
public function flush_option( $args ) {
$option_name = ! empty( $args ) ? $args[0] : null;

if ( $option_name ) {
Comment thread
alaminfirdows marked this conversation as resolved.
Resolved
wp_cache_delete( 'alloptions', 'options' );
wp_cache_delete( $option_name, 'options' );
WP_CLI::success( "Option cache for '$option_name' cleared." );
} else {
if ( ! wp_using_ext_object_cache() || ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
WP_CLI::error( 'Flushing all option caches requires WordPress 6.1+' );
}
wp_cache_flush_group( 'options' );
Comment thread
alaminfirdows marked this conversation as resolved.
Resolved
WP_CLI::success( 'Option caches cleared.' );
}
}
}
Loading