Query: Cache comments feeds in `WP_Query`.

Cache queries to the comments table in `WP_Query` for various comments feeds. Only comment IDs are stored for each feeds cache to avoid doubling up caching with each individual comment's cache.

Props spacedmonkey, boonebgorges, pbearne.
Fixes #36904.


Built from https://develop.svn.wordpress.org/trunk@53065


git-svn-id: http://core.svn.wordpress.org/trunk@52654 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2022-04-05 01:55:03 +00:00
parent 2a7f0aabed
commit 4127a17a0a
2 changed files with 29 additions and 6 deletions

View File

@ -2720,10 +2720,22 @@ class WP_Query {
$corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
$climits = ( ! empty( $climits ) ) ? $climits : '';
$comments = (array) $wpdb->get_results( "SELECT $distinct {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits" );
$comments_request = "SELECT $distinct {$wpdb->comments}.comment_ID FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits";
$key = md5( $comments_request );
$last_changed = wp_cache_get_last_changed( 'comment' ) . ':' . wp_cache_get_last_changed( 'posts' );
$cache_key = "comment_feed:$key:$last_changed";
$comment_ids = wp_cache_get( $cache_key, 'comment' );
if ( false === $comment_ids ) {
$comment_ids = $wpdb->get_col( $comments_request );
wp_cache_add( $cache_key, $comment_ids, 'comment' );
}
_prime_comment_caches( $comment_ids, false );
// Convert to WP_Comment.
/** @var WP_Comment[] */
$this->comments = array_map( 'get_comment', $comments );
$this->comments = array_map( 'get_comment', $comment_ids );
$this->comment_count = count( $this->comments );
$post_ids = array();
@ -3164,11 +3176,22 @@ class WP_Query {
/** This filter is documented in wp-includes/query.php */
$climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option( 'posts_per_rss' ), &$this ) );
$comments_request = "SELECT {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits";
$comments = $wpdb->get_results( $comments_request );
$comments_request = "SELECT {$wpdb->comments}.comment_ID FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits";
$key = md5( $comments_request );
$last_changed = wp_cache_get_last_changed( 'comment' );
$cache_key = "comment_feed:$key:$last_changed";
$comment_ids = wp_cache_get( $cache_key, 'comment' );
if ( false === $comment_ids ) {
$comment_ids = $wpdb->get_col( $comments_request );
wp_cache_add( $cache_key, $comment_ids, 'comment' );
}
_prime_comment_caches( $comment_ids, false );
// Convert to WP_Comment.
/** @var WP_Comment[] */
$this->comments = array_map( 'get_comment', $comments );
$this->comments = array_map( 'get_comment', $comment_ids );
$this->comment_count = count( $this->comments );
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.0-alpha-53064';
$wp_version = '6.0-alpha-53065';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.