Skip to content

Commit bcdeeca

Browse files
committed
feat: Add granular cache flushing commands
Adds granular cache flushing for posts, terms, comments, users, and options. Implements CLI equivalents for WordPress cache-clearing functions: - wp cache flush-post [ID] - wp cache flush-term [ID] - wp cache flush-comment [ID] - wp cache flush-user [ID] - wp cache flush-option [name] Addresses #108: Allow selective cache clearing instead of flushing entire cache. Without IDs/names, clears all cache groups for that type. With IDs/names, clears specific items using WordPress core functions.
1 parent c0509b6 commit bcdeeca

3 files changed

Lines changed: 306 additions & 0 deletions

File tree

‎composer.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@
3939
"cache decr",
4040
"cache delete",
4141
"cache flush",
42+
"cache flush-comment",
4243
"cache flush-group",
44+
"cache flush-option",
45+
"cache flush-post",
46+
"cache flush-term",
47+
"cache flush-user",
4348
"cache get",
4449
"cache incr",
4550
"cache patch",
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
Feature: Granular cache flushing operations
2+
3+
@skip-object-cache
4+
Scenario: Flush post cache
5+
Given a WP install
6+
And a wp-content/mu-plugins/test-harness.php file:
7+
"""
8+
<?php
9+
$cache_post = function(){
10+
wp_cache_set( 'post_123', array( 'ID' => 123, 'post_title' => 'Test' ), 'posts' );
11+
wp_cache_set( 'meta_123', array( 'key' => 'value' ), 'post_meta' );
12+
};
13+
WP_CLI::add_hook( 'before_invoke:cache flush-post', $cache_post );
14+
"""
15+
16+
When I run `wp cache flush-post`
17+
Then STDOUT should contain:
18+
"""
19+
Success: Post caches cleared.
20+
"""
21+
22+
When I run `wp cache flush-post 123`
23+
Then STDOUT should contain:
24+
"""
25+
Success: Post cache for ID 123 cleared.
26+
"""
27+
28+
@skip-object-cache
29+
Scenario: Flush term cache
30+
Given a WP install
31+
And a wp-content/mu-plugins/test-harness.php file:
32+
"""
33+
<?php
34+
$cache_term = function(){
35+
wp_cache_set( 'term_5', array( 'term_id' => 5 ), 'terms' );
36+
wp_cache_set( 'term_meta_5', array(), 'term_meta' );
37+
};
38+
WP_CLI::add_hook( 'before_invoke:cache flush-term', $cache_term );
39+
"""
40+
41+
When I run `wp cache flush-term`
42+
Then STDOUT should contain:
43+
"""
44+
Success: Term caches cleared.
45+
"""
46+
47+
When I run `wp cache flush-term 5`
48+
Then STDOUT should contain:
49+
"""
50+
Success: Term cache for ID 5 cleared.
51+
"""
52+
53+
@skip-object-cache
54+
Scenario: Flush comment cache
55+
Given a WP install
56+
And a wp-content/mu-plugins/test-harness.php file:
57+
"""
58+
<?php
59+
$cache_comment = function(){
60+
wp_cache_set( 'comment_42', array(), 'comment' );
61+
wp_cache_set( 'comment_meta_42', array(), 'comment_meta' );
62+
};
63+
WP_CLI::add_hook( 'before_invoke:cache flush-comment', $cache_comment );
64+
"""
65+
66+
When I run `wp cache flush-comment`
67+
Then STDOUT should contain:
68+
"""
69+
Success: Comment caches cleared.
70+
"""
71+
72+
When I run `wp cache flush-comment 42`
73+
Then STDOUT should contain:
74+
"""
75+
Success: Comment cache for ID 42 cleared.
76+
"""
77+
78+
@skip-object-cache
79+
Scenario: Flush user cache
80+
Given a WP install
81+
And a wp-content/mu-plugins/test-harness.php file:
82+
"""
83+
<?php
84+
$cache_user = function(){
85+
wp_cache_set( 'user_1', array( 'ID' => 1 ), 'users' );
86+
wp_cache_set( 'user_meta_1', array(), 'user_meta' );
87+
};
88+
WP_CLI::add_hook( 'before_invoke:cache flush-user', $cache_user );
89+
"""
90+
91+
When I run `wp cache flush-user`
92+
Then STDOUT should contain:
93+
"""
94+
Success: User caches cleared.
95+
"""
96+
97+
When I run `wp cache flush-user 1`
98+
Then STDOUT should contain:
99+
"""
100+
Success: User cache for ID 1 cleared.
101+
"""
102+
103+
@skip-object-cache
104+
Scenario: Flush option cache
105+
Given a WP install
106+
And a wp-content/mu-plugins/test-harness.php file:
107+
"""
108+
<?php
109+
$cache_option = function(){
110+
wp_cache_set( 'my_option', 'value', 'options' );
111+
wp_cache_set( 'alloptions', array( 'my_option' => 'value' ), 'options' );
112+
};
113+
WP_CLI::add_hook( 'before_invoke:cache flush-option', $cache_option );
114+
"""
115+
116+
When I run `wp cache flush-option`
117+
Then STDOUT should contain:
118+
"""
119+
Success: Option caches cleared.
120+
"""
121+
122+
When I run `wp cache flush-option my_option`
123+
Then STDOUT should contain:
124+
"""
125+
Success: Option cache for 'my_option' cleared.
126+
"""

‎src/Cache_Command.php‎

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,4 +611,179 @@ function ( $key ) {
611611
}
612612
}
613613
}
614+
615+
/**
616+
* Clears post related caches.
617+
*
618+
* @subcommand flush-post
619+
*
620+
* ## OPTIONS
621+
*
622+
* [<id>]
623+
* : Post ID. If not specified, clears all post caches.
624+
*
625+
* ## EXAMPLES
626+
*
627+
* # Clear all post caches.
628+
* $ wp cache flush-post
629+
* Success: Post caches cleared.
630+
*
631+
* # Clear cache for a specific post.
632+
* $ wp cache flush-post 123
633+
* Success: Post cache for ID 123 cleared.
634+
*
635+
* @param array $args Positional arguments.
636+
*/
637+
public function flush_post( $args ) {
638+
$post_id = ! empty( $args ) ? (int) $args[0] : null;
639+
640+
if ( $post_id ) {
641+
clean_post_cache( $post_id );
642+
WP_CLI::success( "Post cache for ID $post_id cleared." );
643+
} else {
644+
wp_cache_flush_group( 'posts' );
645+
wp_cache_flush_group( 'post_meta' );
646+
WP_CLI::success( 'Post caches cleared.' );
647+
}
648+
}
649+
650+
/**
651+
* Clears term related caches.
652+
*
653+
* @subcommand flush-term
654+
*
655+
* ## OPTIONS
656+
*
657+
* [<id>]
658+
* : Term ID. If not specified, clears all term caches.
659+
*
660+
* ## EXAMPLES
661+
*
662+
* # Clear all term caches.
663+
* $ wp cache flush-term
664+
* Success: Term caches cleared.
665+
*
666+
* # Clear cache for a specific term.
667+
* $ wp cache flush-term 5
668+
* Success: Term cache for ID 5 cleared.
669+
*
670+
* @param array $args Positional arguments.
671+
*/
672+
public function flush_term( $args ) {
673+
$term_id = ! empty( $args ) ? (int) $args[0] : null;
674+
675+
if ( $term_id ) {
676+
clean_term_cache( $term_id );
677+
WP_CLI::success( "Term cache for ID $term_id cleared." );
678+
} else {
679+
wp_cache_flush_group( 'terms' );
680+
wp_cache_flush_group( 'term_meta' );
681+
WP_CLI::success( 'Term caches cleared.' );
682+
}
683+
}
684+
685+
/**
686+
* Clears comment related caches.
687+
*
688+
* @subcommand flush-comment
689+
*
690+
* ## OPTIONS
691+
*
692+
* [<id>]
693+
* : Comment ID. If not specified, clears all comment caches.
694+
*
695+
* ## EXAMPLES
696+
*
697+
* # Clear all comment caches.
698+
* $ wp cache flush-comment
699+
* Success: Comment caches cleared.
700+
*
701+
* # Clear cache for a specific comment.
702+
* $ wp cache flush-comment 42
703+
* Success: Comment cache for ID 42 cleared.
704+
*
705+
* @param array $args Positional arguments.
706+
*/
707+
public function flush_comment( $args ) {
708+
$comment_id = ! empty( $args ) ? (int) $args[0] : null;
709+
710+
if ( $comment_id ) {
711+
clean_comment_cache( $comment_id );
712+
WP_CLI::success( "Comment cache for ID $comment_id cleared." );
713+
} else {
714+
wp_cache_flush_group( 'comment' );
715+
wp_cache_flush_group( 'comment_meta' );
716+
WP_CLI::success( 'Comment caches cleared.' );
717+
}
718+
}
719+
720+
/**
721+
* Clears user related caches.
722+
*
723+
* @subcommand flush-user
724+
*
725+
* ## OPTIONS
726+
*
727+
* [<id>]
728+
* : User ID. If not specified, clears all user caches.
729+
*
730+
* ## EXAMPLES
731+
*
732+
* # Clear all user caches.
733+
* $ wp cache flush-user
734+
* Success: User caches cleared.
735+
*
736+
* # Clear cache for a specific user.
737+
* $ wp cache flush-user 1
738+
* Success: User cache for ID 1 cleared.
739+
*
740+
* @param array $args Positional arguments.
741+
*/
742+
public function flush_user( $args ) {
743+
$user_id = ! empty( $args ) ? (int) $args[0] : null;
744+
745+
if ( $user_id ) {
746+
clean_user_cache( $user_id );
747+
WP_CLI::success( "User cache for ID $user_id cleared." );
748+
} else {
749+
wp_cache_flush_group( 'users' );
750+
wp_cache_flush_group( 'user_meta' );
751+
WP_CLI::success( 'User caches cleared.' );
752+
}
753+
}
754+
755+
/**
756+
* Clears option related caches.
757+
*
758+
* @subcommand flush-option
759+
*
760+
* ## OPTIONS
761+
*
762+
* [<name>]
763+
* : Option name. If not specified, clears all option caches.
764+
*
765+
* ## EXAMPLES
766+
*
767+
* # Clear all option caches.
768+
* $ wp cache flush-option
769+
* Success: Option caches cleared.
770+
*
771+
* # Clear cache for a specific option.
772+
* $ wp cache flush-option my_option
773+
* Success: Option cache for 'my_option' cleared.
774+
*
775+
* @param array $args Positional arguments.
776+
*/
777+
public function flush_option( $args ) {
778+
$option_name = ! empty( $args ) ? $args[0] : null;
779+
780+
if ( $option_name ) {
781+
wp_cache_delete( 'alloptions', 'options' );
782+
wp_cache_delete( $option_name, 'options' );
783+
WP_CLI::success( "Option cache for '$option_name' cleared." );
784+
} else {
785+
wp_cache_flush_group( 'options' );
786+
WP_CLI::success( 'Option caches cleared.' );
787+
}
788+
}
614789
}

0 commit comments

Comments
 (0)