Making WordPress.org

Changeset 5862


Ignore:
Timestamp:
09/03/2017 10:04:47 AM (9 years ago)
Author:
SergeyBiryukov
Message:

Support Forums: Add caching for user's topics and reviews count queries.

Props ketuchetan for initial patch.
Fixes #2967.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-users.php

    r5860 r5862  
    3131                add_filter( 'bbp_replies_pagination',          array( $this, 'parse_user_topics_pagination_args' ) );
    3232                add_filter( 'bbp_before_title_parse_args',     array( $this, 'parse_user_topics_title_args' ) );
     33
     34                // Clear user's topics and reviews count cache.
     35                add_action( 'bbp_new_topic',                   array( $this, 'clear_user_topics_count_cache' ), 10, 4 );
    3336        }
    3437
     
    284287                }
    285288
    286                 $count = (int) $wpdb->get_var( $wpdb->prepare(
    287                         "SELECT COUNT(*)
    288                                 FROM {$wpdb->posts}
    289                                 WHERE post_type = 'topic'
    290                                         AND post_status IN ( 'publish', 'closed' )
    291                                         AND post_parent <> %d
    292                                         AND post_author = %d",
    293                         Plugin::REVIEWS_FORUM_ID,
    294                         $user_id
    295                 ) );
     289                // Check cache.
     290                $count = wp_cache_get( $user_id, 'user-topics-count' );
     291                if ( false === $count ) {
     292                        $count = (int) $wpdb->get_var( $wpdb->prepare(
     293                                "SELECT COUNT(*)
     294                                        FROM {$wpdb->posts}
     295                                        WHERE post_type = 'topic'
     296                                                AND post_status IN ( 'publish', 'closed' )
     297                                                AND post_parent <> %d
     298                                                AND post_author = %d",
     299                                Plugin::REVIEWS_FORUM_ID,
     300                                $user_id
     301                        ) );
     302                        wp_cache_set( $user_id, $count, 'user-topics-count' );
     303                }
    296304
    297305                return $count;
     
    318326                }
    319327
    320                 $count = (int) $wpdb->get_var( $wpdb->prepare(
    321                         "SELECT COUNT(*)
    322                                 FROM {$wpdb->posts}
    323                                 WHERE post_type = 'topic'
    324                                         AND post_status IN ( 'publish', 'closed' )
    325                                         AND post_parent = %d
    326                                         AND post_author = %d",
    327                         Plugin::REVIEWS_FORUM_ID,
    328                         $user_id
    329                 ) );
     328                // Check cache.
     329                $count = wp_cache_get( $user_id, 'user-reviews-count' );
     330                if ( false === $count ) {
     331                        $count = (int) $wpdb->get_var( $wpdb->prepare(
     332                                "SELECT COUNT(*)
     333                                        FROM {$wpdb->posts}
     334                                        WHERE post_type = 'topic'
     335                                                AND post_status IN ( 'publish', 'closed' )
     336                                                AND post_parent = %d
     337                                                AND post_author = %d",
     338                                Plugin::REVIEWS_FORUM_ID,
     339                                $user_id
     340                        ) );
     341                        wp_cache_set( $user_id, $count, 'user-reviews-count' );
     342                }
    330343
    331344                return $count;
    332345        }
    333346
     347        /**
     348         * Clear user's topics and reviews count cache.
     349         *
     350         * @param int   $topic_id       Topic ID.
     351         * @param int   $forum_id       Forum ID.
     352         * @param array $anonymous_data Anonymous poster data.
     353         * @param int   $topic_author   Topic author ID.
     354         */
     355        public function clear_user_topics_count_cache( $topic_id, $forum_id, $anonymous_data, $topic_author ) {
     356                if ( Plugin::REVIEWS_FORUM_ID != $forum_id ) {
     357                        wp_cache_delete( $topic_author, 'user-topics-count' );
     358                } else {
     359                        wp_cache_delete( $topic_author, 'user-reviews-count' );
     360                }
     361        }
     362
    334363}
Note: See TracChangeset for help on using the changeset viewer.