Make WordPress Core


Ignore:
Timestamp:
07/07/2016 02:26:21 PM (9 years ago)
Author:
boonebgorges
Message:

Comments: Cache results of SELECT FOUND_ROWS() query.

When comment IDs are fetched from the cache rather than the database,
the subsequent SELECT FOUND_ROWS() query will not return the correct value.
To avoid unnecessary queries, we cache the results of the found_comments
query alongside the comment IDs.

Props spacedmonkey.
Fixes #37184.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-comment-query.php

    r37873 r38001  
    395395            wp_cache_set( 'last_changed', $last_changed, 'comment' );
    396396        }
    397         $cache_key = "get_comment_ids:$key:$last_changed";
    398 
    399         $comment_ids = wp_cache_get( $cache_key, 'comment' );
    400         if ( false === $comment_ids ) {
     397
     398        $cache_key   = "get_comments:$key:$last_changed";
     399        $cache_value = wp_cache_get( $cache_key, 'comment' );
     400        if ( false === $cache_value ) {
    401401            $comment_ids = $this->get_comment_ids();
    402             wp_cache_add( $cache_key, $comment_ids, 'comment' );
     402            if ( $comment_ids ) {
     403                $this->set_found_comments();
     404            }
     405
     406            $cache_value = array(
     407                'comment_ids'    => $comment_ids,
     408                'found_comments' => $this->found_comments,
     409            );
     410            wp_cache_add( $cache_key, $cache_value, 'comment' );
     411        } else {
     412            $comment_ids          = $cache_value['comment_ids'];
     413            $this->found_comments = $cache_value['found_comments'];
     414        }
     415
     416        if ( $this->found_comments && $this->query_vars['number'] ) {
     417            $this->max_num_pages = ceil( $this->found_comments / $this->query_vars['number'] );
    403418        }
    404419
     
    410425
    411426        $comment_ids = array_map( 'intval', $comment_ids );
    412 
    413         if ( $comment_ids && $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
    414             /**
    415              * Filters the query used to retrieve found comment count.
    416              *
    417              * @since 4.4.0
    418              *
    419              * @param string           $found_comments_query SQL query. Default 'SELECT FOUND_ROWS()'.
    420              * @param WP_Comment_Query $comment_query        The `WP_Comment_Query` instance.
    421              */
    422             $found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this );
    423             $this->found_comments = (int) $wpdb->get_var( $found_comments_query );
    424 
    425             $this->max_num_pages = ceil( $this->found_comments / $this->query_vars['number'] );
    426         }
    427427
    428428        if ( 'ids' == $this->query_vars['fields'] ) {
     
    898898            $comment_ids = $wpdb->get_col( $this->request );
    899899            return array_map( 'intval', $comment_ids );
     900        }
     901    }
     902
     903    /**
     904     * Populates found_comments and max_num_pages properties for the current query if the limit clause was used.
     905     *
     906     * @since 4.6.0
     907     * @access private
     908     *
     909     * @global wpdb $wpdb WordPress database abstraction object.
     910     */
     911    private function set_found_comments() {
     912        global $wpdb;
     913
     914        if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
     915            /**
     916             * Filters the query used to retrieve found comment count.
     917             *
     918             * @since 4.4.0
     919             *
     920             * @param string $found_comments_query SQL query. Default 'SELECT FOUND_ROWS()'.
     921             * @param WP_Comment_Query $comment_query The `WP_Comment_Query` instance.
     922             */
     923            $found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this );
     924
     925            $this->found_comments = (int) $wpdb->get_var( $found_comments_query );
    900926        }
    901927    }
Note: See TracChangeset for help on using the changeset viewer.