Skip to content
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

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from

Conversation

jonnynews
Copy link

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.

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.
Copy link

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props spacedmonkey.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link

Test using WordPress Playground

The 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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

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;
Copy link
Contributor

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.

Copy link
Author

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.

Copy link
Contributor

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

Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants