-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refine cache handling invalidation in count_user_posts #8549
base: trunk
Are you sure you want to change the base?
Conversation
Updated caching logic to use user-specific cache groups for post queries, improving granularity and efficiency. Ensured proper cache invalidation for `post_author` when relevant user data changes. Adjusted author-based SQL checks to ensure compatibility with cache updates.
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Replaced direct cache deletion with a new `clean_post_author_cache` function for better reusability and maintainability. Added a PHPUnit test to ensure cache invalidation behaves correctly when reassigning posts between users, covering edge cases. This change improves consistency and makes the codebase easier to maintain.
This test ensures proper cache invalidation when a post's author is updated via a direct database query. It verifies that post counts for both the original and new authors are accurate and that cache behavior functions as expected.
@@ -622,7 +622,8 @@ function count_user_posts( $userid, $post_type = 'post', $public_only = false ) | |||
$where = get_posts_by_author_sql( $post_type, true, $userid, $public_only ); | |||
$query = "SELECT COUNT(*) FROM $wpdb->posts $where"; | |||
|
|||
$last_changed = wp_cache_get_last_changed( 'posts' ); | |||
$cache_group = ! is_numeric( $userid ) ? 'posts' : 'post_author:' . $userid; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to avoid a separate group per author as flushing post counts by group/bumping the last change for authors to refresh will require developers loop through each author in order to do so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a super edge case. It was possible to pass false
or null
or 0
It would result in getting all posts post types. I am not sure why you would ever do this, but it was possible before.
Either way, this is a case we could consider if we support or not. If we decided to support it the tests in this PR should be committed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you are referring to, my concern is that there is an individual cache group for each author. In order to flush all user counts a developer would be required to run the following.
wp_cache_set_posts_last_changed()
$user_ids = get_users( [ 'fields' => 'ID', 'number' => -1 ] );
// Or maybe a direct query of unique author IDs in the posts table, an unidexed column
foreach( $user_ids as $user_id ) {
clean_post_author_cache( $user_id );
}
I think the approach needs to be rethought so the user counts are stored in a single group
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What i am discussing is passing null, false, 0 to count_user_posts, as these are the first parameter.
Doing a per user is kind of the point, you don't need to invalidate every cache. dynamic groups is not the best, we could have kind another way to do this, maybe one group and prefix last change.
Updated caching logic to use user-specific cache groups for post queries, improving granularity and efficiency. Ensured proper cache invalidation for
post_author
when relevant user data changes. Adjusted author-based SQL checks to ensure compatibility with cache updates.Added tests for null, zero, false and float values, as was not tested before.
Trac ticket: https://core.trac.wordpress.org/ticket/39242
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.