[wp-trac] [WordPress Trac] #39242: Add caching to count_user_posts()

WordPress Trac noreply at wordpress.org
Wed Dec 21 19:24:46 UTC 2022


#39242: Add caching to count_user_posts()
-------------------------------------------------+-------------------------
 Reporter:  johnjamesjacoby                      |       Owner:  whyisjake
     Type:  enhancement                          |      Status:  accepted
 Priority:  normal                               |   Milestone:  Future
                                                 |  Release
Component:  Users                                |     Version:  3.0
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch dev-feedback needs-unit-   |     Focuses:  rest-api,
  tests needs-testing                            |  performance
-------------------------------------------------+-------------------------

Comment (by wpgurudev):

 1. I applied the patch from @johnregan3 , I think the overall approach is
 fine. The only issue seems that it is not handling certain events which
 may need to invalidate this cache and recalculate the count.

 2. IMO, we can introduce an additional parameter `$force = false` to
 `count_user_posts` function as the last parameter with default value
 `false` to be backward compatible.

 3. If `$force` is set to `true`, function would recalculate the user posts
 count and set it as a new entry in the cache.

 4. This will be useful because we can simply call `count_user_posts`
 function with `$force` set to `true` on several hooks like `save_post`,
 `deleted_post` etc. to recalculate user posts count. This will further
 abstract the entire logic of calculating the user posts count without
 need to know the cache key and group names.

 **Further Challenges**

 1. The main issue which I can observe is when we have multiple post types.
 Consider a situation where we pass

 {{{
 #!php
 array( 'post', 'page', 'product' )
 }}}

 as `$post_type` to `count_user_posts` function.

 2. Now, consider if we delete a product then we need to update the cache
 `count_user_post_page_product`, but the challenge is how do we identify
 this combination, because we can easily update `count_user_product`, but
 we cannot be sure about `post_page_product` combination as it can be
 anything like `post_product`, `page_product` etc. as well.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/39242#comment:16>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list