From 4127a17a0ae18378ce1c2c3e0511fae24c5b034c Mon Sep 17 00:00:00 2001 From: Peter Wilson <wilson@peterwilson.cc> Date: Tue, 5 Apr 2022 01:55:03 +0000 Subject: [PATCH] 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 --- wp-includes/class-wp-query.php | 33 ++++++++++++++++++++++++++++----- wp-includes/version.php | 2 +- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/wp-includes/class-wp-query.php b/wp-includes/class-wp-query.php index 35b29a902d..fbba6a5f31 100644 --- a/wp-includes/class-wp-query.php +++ b/wp-includes/class-wp-query.php @@ -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 ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index 4857db98d0..21e719ae14 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.