Skip to content

feat: Add granular cache flushing commands#125

Open
alaminfirdows wants to merge 8 commits into
wp-cli:mainfrom
alaminfirdows:feat/granular-cache-flush
Open

feat: Add granular cache flushing commands#125
alaminfirdows wants to merge 8 commits into
wp-cli:mainfrom
alaminfirdows:feat/granular-cache-flush

Conversation

@alaminfirdows

Copy link
Copy Markdown

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]

Completed #108

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 wp-cli#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.
@alaminfirdows alaminfirdows requested a review from a team as a code owner June 27, 2026 04:20
@github-actions

Copy link
Copy Markdown
Contributor

Hello! 👋

Thanks for opening this pull request! Please check out our contributing guidelines. We appreciate you taking the initiative to contribute to this project.

Contributing isn't limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation.

Here are some useful Composer commands to get you started:

  • composer install: Install dependencies.
  • composer test: Run the full test suite.
  • composer phpcs: Check for code style violations.
  • composer phpcbf: Automatically fix code style violations.
  • composer phpunit: Run unit tests.
  • composer behat: Run behavior-driven tests.

To run a single Behat test, you can use the following command:

# Run all tests in a single file
composer behat features/some-feature.feature

# Run only a specific scenario (where 123 is the line number of the "Scenario:" title)
composer behat features/some-feature.feature:123

You can find a list of all available Behat steps in our handbook.

@github-actions github-actions Bot added command:cache-flush Related to 'cache flush' command scope:distribution Related to distribution labels Jun 27, 2026
@codecov

codecov Bot commented Jun 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 52.00000% with 24 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Cache_Command.php 52.00% 24 Missing ⚠️

📢 Thoughts on this report? Let us know!

Add array<string> type hints to method parameters for PHPStan compliance.
Add wp_cache_supports() checks before calling wp_cache_flush_group().
Requires WordPress 6.1+ or persistent object cache with group flushing
support. Specific item flushing works on all WordPress versions.

Update tests to validate specific item flushing and expect errors
when group flushing not supported.
Remove " or a persistent object cache with group flushing support" suffix
from error messages to match test expectations. Commands now show:
- "Flushing all post caches requires WordPress 6.1+"
- "Flushing all term caches requires WordPress 6.1+"
- "Flushing all comment caches requires WordPress 6.1+"
- "Flushing all user caches requires WordPress 6.1+"
- "Flushing all option caches requires WordPress 6.1+"
Replace wp_cache_supports check with direct function existence check.
wp_cache_flush_group was added in WordPress 6.1, so checking if it exists
is simpler and more direct than checking cache support via wp_cache_supports.
Use wp_cache_supports('flush_group') to check if the object cache
implementation supports group flushing, rather than just checking
if the function exists. This properly detects when the feature
is supported by the specific cache implementation in use.
WP 6.1+ default in-memory cache returns true for wp_cache_supports('flush_group'),
so the old check never errored on modern WordPress. Add wp_using_ext_object_cache()
as the primary gate: group flushing only makes sense with a persistent external
cache (Redis, Memcached, etc.), so error without one regardless of WP version.
WP 6.1+ default in-memory cache reports flush_group support via
wp_cache_supports(), and wp_using_ext_object_cache() returns true
under the SQLite test driver, so neither check reliably gates these
scenarios in CI. Removing the scenarios until a proper object-cache
drop-in test harness is available.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds new wp cache subcommands to flush caches at a more granular level (post, term, comment, user, option), addressing the need to avoid expensive full-cache flushes and to expose CLI equivalents of WordPress cache-clearing functions.

Changes:

  • Introduces flush-post, flush-term, flush-comment, flush-user, and flush-option subcommands in Cache_Command.
  • Adds Behat coverage for the new subcommands (currently focused on “specific ID/name” invocation).
  • Registers the new commands in composer.json bundled command metadata.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 11 comments.

File Description
src/Cache_Command.php Adds new granular cache flushing subcommands and their runtime behavior.
features/cache-flush-granular.feature Introduces acceptance tests for the new granular flush commands.
composer.json Adds the new subcommands to the bundled commands list.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Cache_Command.php
Comment on lines +638 to +642
$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 src/Cache_Command.php
Comment on lines +644 to +648
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+' );
}
wp_cache_flush_group( 'posts' );
wp_cache_flush_group( 'post_meta' );
Comment thread src/Cache_Command.php
Comment on lines +676 to +680
$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 src/Cache_Command.php
Comment on lines +682 to +686
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 src/Cache_Command.php
Comment on lines +714 to +718
$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 src/Cache_Command.php
Comment on lines +752 to +756
$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 src/Cache_Command.php
Comment on lines +758 to +762
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 on lines +9 to +13
$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 thread src/Cache_Command.php
Comment on lines +790 to +792
$option_name = ! empty( $args ) ? $args[0] : null;

if ( $option_name ) {
Comment thread src/Cache_Command.php
Comment on lines +797 to +800
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' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

command:cache-flush Related to 'cache flush' command scope:distribution Related to distribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants