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.

git-svn-id: https://develop.svn.wordpress.org/trunk@38001 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2016-07-07 14:26:21 +00:00
parent 44da2a77b9
commit 9d34db0590
2 changed files with 68 additions and 19 deletions

View File

@ -394,12 +394,27 @@ class WP_Comment_Query {
$last_changed = microtime(); $last_changed = microtime();
wp_cache_set( 'last_changed', $last_changed, 'comment' ); wp_cache_set( 'last_changed', $last_changed, 'comment' );
} }
$cache_key = "get_comment_ids:$key:$last_changed";
$comment_ids = wp_cache_get( $cache_key, 'comment' ); $cache_key = "get_comments:$key:$last_changed";
if ( false === $comment_ids ) { $cache_value = wp_cache_get( $cache_key, 'comment' );
if ( false === $cache_value ) {
$comment_ids = $this->get_comment_ids(); $comment_ids = $this->get_comment_ids();
wp_cache_add( $cache_key, $comment_ids, 'comment' ); if ( $comment_ids ) {
$this->set_found_comments();
}
$cache_value = array(
'comment_ids' => $comment_ids,
'found_comments' => $this->found_comments,
);
wp_cache_add( $cache_key, $cache_value, 'comment' );
} else {
$comment_ids = $cache_value['comment_ids'];
$this->found_comments = $cache_value['found_comments'];
}
if ( $this->found_comments && $this->query_vars['number'] ) {
$this->max_num_pages = ceil( $this->found_comments / $this->query_vars['number'] );
} }
// If querying for a count only, there's nothing more to do. // If querying for a count only, there's nothing more to do.
@ -410,21 +425,6 @@ class WP_Comment_Query {
$comment_ids = array_map( 'intval', $comment_ids ); $comment_ids = array_map( 'intval', $comment_ids );
if ( $comment_ids && $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
/**
* Filters the query used to retrieve found comment count.
*
* @since 4.4.0
*
* @param string $found_comments_query SQL query. Default 'SELECT FOUND_ROWS()'.
* @param WP_Comment_Query $comment_query The `WP_Comment_Query` instance.
*/
$found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this );
$this->found_comments = (int) $wpdb->get_var( $found_comments_query );
$this->max_num_pages = ceil( $this->found_comments / $this->query_vars['number'] );
}
if ( 'ids' == $this->query_vars['fields'] ) { if ( 'ids' == $this->query_vars['fields'] ) {
$this->comments = $comment_ids; $this->comments = $comment_ids;
return $this->comments; return $this->comments;
@ -900,6 +900,32 @@ class WP_Comment_Query {
} }
} }
/**
* Populates found_comments and max_num_pages properties for the current query if the limit clause was used.
*
* @since 4.6.0
* @access private
*
* @global wpdb $wpdb WordPress database abstraction object.
*/
private function set_found_comments() {
global $wpdb;
if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
/**
* Filters the query used to retrieve found comment count.
*
* @since 4.4.0
*
* @param string $found_comments_query SQL query. Default 'SELECT FOUND_ROWS()'.
* @param WP_Comment_Query $comment_query The `WP_Comment_Query` instance.
*/
$found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this );
$this->found_comments = (int) $wpdb->get_var( $found_comments_query );
}
}
/** /**
* Fetch descendants for located comments. * Fetch descendants for located comments.
* *

View File

@ -2151,6 +2151,29 @@ class Tests_Comment_Query extends WP_UnitTestCase {
$this->assertEquals( 2, $q->max_num_pages ); $this->assertEquals( 2, $q->max_num_pages );
} }
/**
* @ticket 37184
*/
public function test_found_rows_should_be_fetched_from_the_cache() {
$comments = self::factory()->comment->create_many( 3, array( 'comment_post_ID' => self::$post_id ) );
// Prime cache.
new WP_Comment_Query( array(
'post_id' => self::$post_id,
'number' => 2,
'no_found_rows' => false,
) );
$q = new WP_Comment_Query( array(
'post_id' => self::$post_id,
'number' => 2,
'no_found_rows' => false,
) );
$this->assertEquals( 3, $q->found_comments );
$this->assertEquals( 2, $q->max_num_pages );
}
/** /**
* @ticket 8071 * @ticket 8071
*/ */