CARVIEW |
Opened 7 months ago
Last modified 3 weeks ago
#63045 new enhancement
Add caching to `count_many_users_posts()`
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | 6.9 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Users | Keywords: | has-patch has-unit-tests needs-testing |
Focuses: | performance | Cc: |
Description
Follow up to #39242, specifically as raised in https://core.trac.wordpress.org/ticket/39242#comment:39: As we already added caching to count_user_posts()
, it makes sense to add similar caching to count_many_users_posts()
.
cc @spacedmonkey @peterwilsoncc
Attachments (1)
- 63045-cache-count_many_users_posts.diff (1.2 KB) - added by sachinrajcp123 2 months ago.
Download all attachments as: .zip
Change History (18)
This ticket was mentioned in PR #8444 on WordPress/wordpress-develop by @sukhendu2002.
7 months ago
#1
- Keywords has-patch added; needs-patch removed
This ticket was mentioned in Slack in #core by jeffpaul. View the logs.
7 months ago
This ticket was mentioned in Slack in #core by jeffpaul. View the logs.
7 months ago
This ticket was mentioned in Slack in #core-performance by adamsilverstein. View the logs.
4 months ago
#5
@adamsilverstein
4 months ago
- Keywords has-unit-tests added; needs-unit-tests removed
This ticket was mentioned in PR #9106 on WordPress/wordpress-develop by @rollybueno.
3 months ago
#6
Add proper caching support to count_many_users_posts() function
Enhances the performance of count_many_users_posts() function with working cache key generation.
Trac ticket: https://core.trac.wordpress.org/ticket/63045
#7
@rollybueno
3 months ago
I'm not a fan of md5() the query statement so I pushed a new patch version in https://github.com/WordPress/wordpress-develop/pull/9106/ that:
- More transparent cache keys – avoids md5() and instead uses readable, parameter-based keys (user IDs, post type,
$public_only
), which makes it easier to debug and trace. - Proper separation of cached data – different input combinations get unique keys, reducing the risk of stale or incorrect cache results.
- Better cache grouping – avoids using a static group, so it's safer and more aligned with how the query behaves.
- Easier to maintain or extend – the logic is clearer and more flexible for future changes.
Overall, it's a more accurate, debuggable, and scalable approach to caching in count_many_users_posts()
.
My questions:
- ❓ Should we have the OC an expiration? If yes, how long? I have set to 1 hour but it's open for discussions.
- ❓ We probably need cache clearing function, but on what actions it should hook into?
This ticket was mentioned in Slack in #core-performance by rollybueno. View the logs.
3 months ago
#9
@rollybueno
3 months ago
- Keywords 2nd-opinion dev-feedback added
During the bi-weekly coffee-hours, this needs more discussion regarding the caching approach, either using readable cache key or using md5.
Props @flixos90
#10
@spacedmonkey
3 months ago
I strongly believe we should work on #59592 first, as that will effect this ticket's approach.
@sachinrajcp123
2 months ago
- Attachment 63045-cache-count_many_users_posts.diff added
#11
@sachinrajcp123
2 months ago
Add object caching to count_many_users_posts() to avoid repeated database queries, similar to count_user_posts().
#12
@kalpeshh
6 weeks ago
- Keywords needs-refresh added
I checked the PR https://github.com/WordPress/wordpress-develop/pull/9106/files and 63045-cache-count_many_users_posts.diff
The issue with both approaches is different cache_key
would be generated if order of array
$users is different
$userlist = implode( ',', array_map( 'absint', $users ) ); $cache_key = "count_many_users_posts_{$post_type}_{$public_only}_" . str_replace( ',', '_', $userlist ) . '_' . get_current_user_id();
For above code,
$users = [ 1, 2, 3 ] → $userlist = "1,2,3" → _1_2_3_ in the cache key. $users = [ 3, 2, 1 ] → $userlist = "3,2,1" → _3_2_1_ in the cache key.
To fix this, we should normalize the $users
array before building the key:
- Ensure all IDs are integers.
- Remove duplicates.
- Sort the array to guarantee consistent order.
Here is the suggested fix for this,
$users = array_map( 'absint', (array) $users ); $users = array_filter( $users ); // remove 0/invalids $users = array_unique( $users ); // remove duplicates sort( $users ); // enforce consistent order $userlist = implode( '_', $users ); $cache_key = "count_many_users_posts_{$post_type}_{$public_only}_{$userlist}_" . get_current_user_id();
#13
@SirLouen
6 weeks ago
- Keywords changes-requested added; needs-refresh removed
Hey @kalpeshh, for your information, needs-refresh
means, that the patch is not applying correctly. What I think you were trying to add is changes-requested
. I'm adding this for you.
This ticket was mentioned in Slack in #core-performance by westonruter. View the logs.
4 weeks ago
#16
@rollybueno
3 weeks ago
Thanks for the feedback @kalpeshh! Updated https://github.com/WordPress/wordpress-develop/pull/9106.
I just noticed, however, that @spacedmonkey has also requested feedback on https://github.com/WordPress/wordpress-develop/pull/8444 , which I only saw after I have updated my branch.
By the way that PR has unit test(still need some polishing) so I' think we should use that instead of mine.
Keeping the existing keywords for https://github.com/WordPress/wordpress-develop/pull/8444
#17
@rollybueno
3 weeks ago
- Keywords needs-testing added; 2nd-opinion dev-feedback changes-requested removed
Update: @peterwilsoncc and @westonruter have reviewed PR https://github.com/WordPress/wordpress-develop/pull/9106, and I’ve applied their feedback. The changes include:
- Switching to md5 for the cache key to handle Memcached’s 250-character limit
- Replacing with
wp_cache_get_salted()
- Adding a dedicated unit test function specifically for this ticket
Marking the ticket as needs testing
.
Trac ticket: https://core.trac.wordpress.org/ticket/63045