From f12dff21f9173083c61fc3133aab99686c9cadb9 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 18 Jul 2023 13:02:53 +0100 Subject: [PATCH 1/2] WP-r55607: Comments: Use wp_cache_get_multiple in `WP_Comment_Query`. In the `fill_descendants` method in `WP_Comment_Query`, there is a loop the calls `wp_cache_get` to get `child comments. Instead of getting one key at a time, use `wp_cache_get_multiple` and get all keys at once. WP:Props spacedmonkey, tillkruess, mukesh27. Fixes https://core.trac.wordpress.org/ticket/57803. Conflicts: - src/wp-includes/class-wp-comment-query.php --- Merges https://core.trac.wordpress.org/changeset/55607 / WordPress/wordpress-develop@c46a30eb1f to ClassicPress. --- src/wp-includes/class-wp-comment-query.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/wp-includes/class-wp-comment-query.php b/src/wp-includes/class-wp-comment-query.php index 4447b1169b..bf59acfade 100644 --- a/src/wp-includes/class-wp-comment-query.php +++ b/src/wp-includes/class-wp-comment-query.php @@ -1036,15 +1036,26 @@ protected function fill_descendants( $comments ) { $child_ids = array(); $uncached_parent_ids = array(); $_parent_ids = $levels[ $level ]; + if ( $_parent_ids ) { + $cache_keys = array(); foreach ( $_parent_ids as $parent_id ) { +<<<<<<< HEAD $cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed"; $parent_child_ids = wp_cache_get( $cache_key, 'comment' ); +======= + $cache_keys[ $parent_id ] = "get_comment_child_ids:$parent_id:$key:$last_changed"; + } + $cache_data = wp_cache_get_multiple( array_values( $cache_keys ), 'comment-queries' ); + foreach ( $_parent_ids as $parent_id ) { + $parent_child_ids = $cache_data[ $cache_keys[ $parent_id ] ]; +>>>>>>> c46a30eb1f (Comments: Use wp_cache_get_multiple in `WP_Comment_Query`.) if ( false !== $parent_child_ids ) { $child_ids = array_merge( $child_ids, $parent_child_ids ); } else { $uncached_parent_ids[] = $parent_id; } } + } if ( $uncached_parent_ids ) { // Fetch this level of comments. From 755b6e0c856d50ebe09e18c50f608318d994f54d Mon Sep 17 00:00:00 2001 From: mattyrob Date: Tue, 18 Jul 2023 13:33:49 +0100 Subject: [PATCH 2/2] Fix merge conflicts from 55607 --- src/wp-includes/class-wp-comment-query.php | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/wp-includes/class-wp-comment-query.php b/src/wp-includes/class-wp-comment-query.php index bf59acfade..9a481b1bda 100644 --- a/src/wp-includes/class-wp-comment-query.php +++ b/src/wp-includes/class-wp-comment-query.php @@ -1038,24 +1038,19 @@ protected function fill_descendants( $comments ) { $_parent_ids = $levels[ $level ]; if ( $_parent_ids ) { $cache_keys = array(); - foreach ( $_parent_ids as $parent_id ) { -<<<<<<< HEAD - $cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed"; - $parent_child_ids = wp_cache_get( $cache_key, 'comment' ); -======= + foreach ( $_parent_ids as $parent_id ) { $cache_keys[ $parent_id ] = "get_comment_child_ids:$parent_id:$key:$last_changed"; } $cache_data = wp_cache_get_multiple( array_values( $cache_keys ), 'comment-queries' ); foreach ( $_parent_ids as $parent_id ) { $parent_child_ids = $cache_data[ $cache_keys[ $parent_id ] ]; ->>>>>>> c46a30eb1f (Comments: Use wp_cache_get_multiple in `WP_Comment_Query`.) - if ( false !== $parent_child_ids ) { - $child_ids = array_merge( $child_ids, $parent_child_ids ); - } else { - $uncached_parent_ids[] = $parent_id; + if ( false !== $parent_child_ids ) { + $child_ids = array_merge( $child_ids, $parent_child_ids ); + } else { + $uncached_parent_ids[] = $parent_id; + } } } - } if ( $uncached_parent_ids ) { // Fetch this level of comments.