From 3b58a4828da571c9f1142593073117cbef5be118 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Thu, 29 Sep 2022 10:09:11 +0000 Subject: [PATCH] Query: Save excessive cache add and sets in `WP_Query`. In [53941] database query caching was added to `WP_Query`. However on sites with persistent object caching enabled, this resulted in a high number of unnecessary cache set and adds being run on every request. Caches are not set, if the query cache already exists and is cached. Replace usage of `update_post_caches` with `_prime_post_caches` to ensure that only posts that are not in cache are primed. Props spacedmonkey, peterwilsoncc, mukesh27. See #22176. Built from https://develop.svn.wordpress.org/trunk@54352 git-svn-id: http://core.svn.wordpress.org/trunk@53911 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-query.php | 11 ++++++----- wp-includes/version.php | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/wp-includes/class-wp-query.php b/wp-includes/class-wp-query.php index 4b17618907..7af38f135b 100644 --- a/wp-includes/class-wp-query.php +++ b/wp-includes/class-wp-query.php @@ -3099,10 +3099,10 @@ class WP_Query { $last_changed .= wp_cache_get_last_changed( 'terms' ); } - $cache_key = "wp_query:$key:$last_changed"; - + $cache_key = "wp_query:$key:$last_changed"; + $cache_found = false; if ( null === $this->posts ) { - $cached_results = wp_cache_get( $cache_key, 'posts' ); + $cached_results = wp_cache_get( $cache_key, 'posts', false, $cache_found ); if ( $cached_results ) { if ( 'ids' === $q['fields'] ) { @@ -3256,7 +3256,7 @@ class WP_Query { $this->posts = array_map( 'get_post', $this->posts ); } - if ( $q['cache_results'] && $id_query_is_cacheable ) { + if ( $q['cache_results'] && $id_query_is_cacheable && ! $cache_found ) { $post_ids = wp_list_pluck( $this->posts, 'ID' ); $cache_value = array( @@ -3455,7 +3455,8 @@ class WP_Query { $this->posts = array_map( 'get_post', $this->posts ); if ( $q['cache_results'] ) { - update_post_caches( $this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache'] ); + $post_ids = wp_list_pluck( $this->posts, 'ID' ); + _prime_post_caches( $post_ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] ); } /** @var WP_Post */ diff --git a/wp-includes/version.php b/wp-includes/version.php index 0f8bbecfa9..56b982cd92 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.1-beta2-54351'; +$wp_version = '6.1-beta2-54352'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.