Skip to content

feat: Add granular cache flushing commands - #125

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

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

Conversation

@alaminfirdows

@alaminfirdows alaminfirdows commented Jun 27, 2026

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

Summary by CodeRabbit

  • New Features

    • Added granular cache-flushing commands for posts, terms, comments, users, and options.
    • Supports clearing individual items by ID or name, or flushing an entire cache group.
    • Provides clear feedback when bulk flushing is unavailable or unsuccessful.
  • Bug Fixes

    • Improved validation for invalid and non-positive IDs.
    • Ensured related metadata and taxonomy caches are cleared with their items.
  • Tests

    • Added acceptance coverage for individual and bulk cache-flushing scenarios.

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
Contributor

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 Outdated
Comment thread src/Cache_Command.php Outdated
Comment thread src/Cache_Command.php Outdated
Comment thread src/Cache_Command.php Outdated
Comment thread src/Cache_Command.php Outdated
Comment thread src/Cache_Command.php Outdated
Comment thread src/Cache_Command.php Outdated
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 Outdated
Comment thread src/Cache_Command.php Outdated
@swissspidy

Copy link
Copy Markdown
Member

@alaminfirdows Would you be up for assessing & addressing the above code review feedback?

@alaminfirdows

Copy link
Copy Markdown
Author

@alaminfirdows Would you be up for assessing & addressing the above code review feedback?

Sorry for the delay, I'll push the updates very soon.

@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@alaminfirdows, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 52 minutes

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

Wait for the limit to reset, then comment @coderabbitai review or push new commits to the PR.

An organization admin can change what happens after included review limits in Billing.

How do review limits work?

CodeRabbit enforces per-developer PR review limits within each organization.

For paid Pro and Pro+ reviews, CodeRabbit uses a developer's included PR review attempts over the past 7 days to set the current hourly allowance. At typical activity levels, the full plan allowance applies. Higher sustained activity can lower the allowance until earlier attempts leave the 7-day window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 720cf475-7e20-4bb5-b660-2c5f2537c6db

📥 Commits

Reviewing files that changed from the base of the PR and between 0e88bc3 and 3e514ea.

📒 Files selected for processing (3)
  • composer.json
  • features/cache-flush-granular.feature
  • src/Cache_Command.php

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: e8008a37-f75d-460e-a532-a0b49287e747

📥 Commits

Reviewing files that changed from the base of the PR and between cac561b and 3e514ea.

📒 Files selected for processing (2)
  • features/cache-flush-granular.feature
  • src/Cache_Command.php

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

Added five granular cache-flushing commands for posts, terms, comments, users, and options. The commands support targeted identifiers and bulk cache-group flushing with validation and failure reporting. Acceptance scenarios cover targeted, invalid-input, and bulk operations.

Changes

Granular cache flushing

Layer / File(s) Summary
Register granular cache commands
composer.json
The bundled command list registers the five granular cache-flushing commands.
Implement targeted cache flushing
src/Cache_Command.php, features/cache-flush-granular.feature
Entity-specific commands validate positive numeric IDs and clear entity and metadata cache entries. Term flushing resolves taxonomy before clearing the term cache. Acceptance scenarios cover targeted commands and invalid IDs.
Validate bulk cache-group flushing
src/Cache_Command.php, features/cache-flush-granular.feature
Bulk commands no longer require an external object cache. They validate group-flush results and report failures. Acceptance scenarios cover complete cache-group flushing for posts, terms, comments, users, and options.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Merge Risk: 🟡 Moderate · up to 3e514

The new granular cache-flush acceptance scenarios use post and term IDs that are not created by the fixtures, so cleanup can error and fail the test suite; merge readiness is moderate until those scenarios use persisted fixture entities.

Suggested reviewers: brianhenryie, ernilambar, janw-me

Sequence Diagram(s)

sequenceDiagram
  participant CLI
  participant Cache_Command
  participant WordPressCacheAPI
  CLI->>Cache_Command: Run a granular cache flush command
  Cache_Command->>Cache_Command: Validate targeted IDs when provided
  Cache_Command->>WordPressCacheAPI: Clear targeted entries or flush a cache group
  WordPressCacheAPI-->>Cache_Command: Return the cache operation result
  Cache_Command-->>CLI: Report success or failure
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: adding granular cache flushing commands.
Docstring Coverage ✅ Passed Docstring check was indeterminate for this PR — some files could not be analyzed in time. Not blocking.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

- Add proper input validation for numeric IDs to prevent silent fallthrough
- Remove wp_using_ext_object_cache() requirement; rely only on wp_cache_supports( 'flush_group' )
- Check return values of wp_cache_flush_group() calls and error on failure
- Add after_invoke hooks to verify cache is actually cleared
- Add @require-wp-6-1 version-gated scenarios for group flush behavior
- Add error test cases for invalid IDs

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@features/cache-flush-granular.feature`:
- Around line 9-20: The cache-flush scenarios currently verify only command
output and must assert canonical cache invalidation in-process. In
features/cache-flush-granular.feature lines 9-20, create post 123 and verify
numeric key 123 is absent from posts and post_meta; lines 28-39, verify key 5 is
absent from terms and update the command if term metadata must also be
invalidated; lines 47-58, verify key 42 is absent from comment and update the
command if comment metadata must also be invalidated; lines 66-77, ensure user 1
exists and verify key 1 is absent from users and user_meta; lines 85-96, verify
both alloptions and my_option are absent after the option flush.

In `@src/Cache_Command.php`:
- Around line 638-645: Reject supplied invalid IDs before choosing the
bulk-flush path. In src/Cache_Command.php ranges 638-645, 676-683, 714-721, and
752-759, detect argument presence separately, validate that the ID is a positive
integer, and only then enter the targeted branch; report invalid supplied values
instead of treating them as absent and flushing all post cache groups.
- Around line 647-649: Update all five cache-clearing handlers in
src/Cache_Command.php at lines 647-649, 685-687, 723-725, 761-763, and 800-801
to validate every wp_cache_flush_group() return value before calling
WP_CLI::success(). Call WP_CLI::error() when any required group fails:
posts/post_meta, terms/term_meta, comment/comment_meta, users/user_meta, or
options; otherwise preserve the existing success messages.
- Around line 675-680: Update src/Cache_Command.php lines 675-680 in flush_term
to pass the term’s taxonomy explicitly to clean_term_cache() and delete the
numeric term ID from term_meta. Also update src/Cache_Command.php lines 713-718
in the targeted comment branch to delete the numeric comment ID from
comment_meta after clean_comment_cache().
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: c6cac611-9ea3-489f-84eb-232208c9fa6f

📥 Commits

Reviewing files that changed from the base of the PR and between e59095d and cac561b.

📒 Files selected for processing (3)
  • composer.json
  • features/cache-flush-granular.feature
  • src/Cache_Command.php

Comment thread features/cache-flush-granular.feature
Comment thread src/Cache_Command.php Outdated
Comment thread src/Cache_Command.php Outdated
Comment thread src/Cache_Command.php
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.

3 participants