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

WordPress Trac noreply at wordpress.org
Sat Jan 27 12:51:53 UTC 2018


#39242: Add caching to count_user_posts()
-------------------------------------+------------------------------------
 Reporter:  johnjamesjacoby          |       Owner:
     Type:  enhancement              |      Status:  new
 Priority:  normal                   |   Milestone:  Awaiting Review
Component:  Users                    |     Version:
 Severity:  normal                   |  Resolution:
 Keywords:  needs-patch 2nd-opinion  |     Focuses:  rest-api, performance
-------------------------------------+------------------------------------
Changes (by milindmore22):

 * focuses:   => rest-api, performance


Comment:

 Yes, performances of websites using REST is highly affected.

 I have added a patch based on WP VIP function below.

 {{{#!php
 <?php

 /**
  * Cached version of count_user_posts, which is uncached but doesn't
 always need to hit the db
  *
  * count_user_posts is generally fast, but it can be easy to end up with
 many redundant queries
  * if it's called several times per request. This allows bypassing the db
 queries in favor of
  * the cache
  */
 function wpcom_vip_count_user_posts( $user_id ) {
     if ( ! is_numeric( $user_id ) ) {
         return 0;
     }

     $cache_key = 'vip_' . (int) $user_id;
     $cache_group = 'user_posts_count';

     if ( false === ( $count = wp_cache_get( $cache_key, $cache_group ) ) )
 {
         $count = count_user_posts( $user_id );

         wp_cache_set( $cache_key, $count, $cache_group, 5 *
 MINUTE_IN_SECONDS );
     }

     return $count;
 }
 }}}

 Do we need proper cache invalidation or  5 Min expiration timeout will do
 ?

 I think we should handle this in 5.0 thoughts ?

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


More information about the wp-trac mailing list