Query: Improve caching behavior for WP_Query when retrieving id=>parent fields

In [53941], the addition of query caching to `WP_Query` brought about an unintended issue when querying for fields equal to id=>parent. Specifically, on websites with object caching enabled and a substantial number of pages, the second run of this query triggered the `_prime_post_caches` function for id=>parent. This led to the unnecessary priming of post, meta, and term caches, even when only id and parent information were requested.

This commit addresses this issue by introducing a new function, `_prime_post_parents_caches`, which primes a dedicated cache for post parents. This cache is primed during the initial query execution. Subsequently, the `wp_cache_get_multiple` function is employed to retrieve all post parent data in a single object cache request, optimizing performance.

Additionally, this commit extends the coverage of existing unit tests to ensure the reliability of the changes.

Props kevinfodness, joemcgill, peterwilsoncc, LinSoftware, thekt12, spacedmonkey.
Fixes #59188
Built from https://develop.svn.wordpress.org/trunk@56763


git-svn-id: http://core.svn.wordpress.org/trunk@56275 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2023-10-03 15:01:21 +00:00
parent ddef9ed1f7
commit 5283300aac
4 changed files with 46 additions and 18 deletions

View File

@ -3179,36 +3179,36 @@ class WP_Query {
$cached_results = wp_cache_get( $cache_key, 'post-queries', false, $cache_found ); $cached_results = wp_cache_get( $cache_key, 'post-queries', false, $cache_found );
if ( $cached_results ) { if ( $cached_results ) {
if ( 'ids' === $q['fields'] ) { /** @var int[] */
/** @var int[] */ $post_ids = array_map( 'intval', $cached_results['posts'] );
$this->posts = array_map( 'intval', $cached_results['posts'] );
} else {
_prime_post_caches( $cached_results['posts'], $q['update_post_term_cache'], $q['update_post_meta_cache'] );
/** @var WP_Post[] */
$this->posts = array_map( 'get_post', $cached_results['posts'] );
}
$this->post_count = count( $this->posts ); $this->post_count = count( $post_ids );
$this->found_posts = $cached_results['found_posts']; $this->found_posts = $cached_results['found_posts'];
$this->max_num_pages = $cached_results['max_num_pages']; $this->max_num_pages = $cached_results['max_num_pages'];
if ( 'ids' === $q['fields'] ) { if ( 'ids' === $q['fields'] ) {
$this->posts = $post_ids;
return $this->posts; return $this->posts;
} elseif ( 'id=>parent' === $q['fields'] ) { } elseif ( 'id=>parent' === $q['fields'] ) {
_prime_post_parents_caches( $post_ids );
/** @var int[] */ /** @var int[] */
$post_parents = array(); $post_parents = wp_cache_get_multiple( $post_ids, 'post_parent' );
foreach ( $this->posts as $key => $post ) { foreach ( $post_parents as $id => $post_parent ) {
$obj = new stdClass(); $obj = new stdClass();
$obj->ID = (int) $post->ID; $obj->ID = (int) $id;
$obj->post_parent = (int) $post->post_parent; $obj->post_parent = (int) $post_parent;
$this->posts[ $key ] = $obj; $this->posts[] = $obj;
$post_parents[ $obj->ID ] = $obj->post_parent;
} }
return $post_parents; return $post_parents;
} else {
_prime_post_caches( $post_ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
/** @var WP_Post[] */
$this->posts = array_map( 'get_post', $post_ids );
} }
} }
} }
@ -3256,6 +3256,8 @@ class WP_Query {
$post_parents[ (int) $post->ID ] = (int) $post->post_parent; $post_parents[ (int) $post->ID ] = (int) $post->post_parent;
$post_ids[] = (int) $post->ID; $post_ids[] = (int) $post->ID;
} }
// Prime post parent caches, so that on second run, there is not another database query.
wp_cache_add_multiple( $post_parents, 'post_parent' );
if ( $q['cache_results'] && $id_query_is_cacheable ) { if ( $q['cache_results'] && $id_query_is_cacheable ) {
$cache_value = array( $cache_value = array(

View File

@ -7169,7 +7169,7 @@ function _get_non_cached_ids( $object_ids, $cache_group ) {
$cache_values = wp_cache_get_multiple( $object_ids, $cache_group ); $cache_values = wp_cache_get_multiple( $object_ids, $cache_group );
foreach ( $cache_values as $id => $value ) { foreach ( $cache_values as $id => $value ) {
if ( ! $value ) { if ( false === $value ) {
$non_cached_ids[] = (int) $id; $non_cached_ids[] = (int) $id;
} }
} }

View File

@ -7262,6 +7262,7 @@ function clean_post_cache( $post ) {
wp_cache_delete( $post->ID, 'posts' ); wp_cache_delete( $post->ID, 'posts' );
wp_cache_delete( $post->ID, 'post_meta' ); wp_cache_delete( $post->ID, 'post_meta' );
wp_cache_delete( $post->ID, 'post_parent' );
clean_object_term_cache( $post->ID, $post->post_type ); clean_object_term_cache( $post->ID, $post->post_type );
@ -7795,6 +7796,31 @@ function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache
} }
} }
/**
* Prime post parent caches.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int[] $ids ID list.
*/
function _prime_post_parents_caches( array $ids ) {
global $wpdb;
$non_cached_ids = _get_non_cached_ids( $ids, 'post_parent' );
if ( ! empty( $non_cached_ids ) ) {
$fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.ID, $wpdb->posts.post_parent FROM $wpdb->posts WHERE ID IN (%s)", implode( ',', $non_cached_ids ) ) );
if ( $fresh_posts ) {
$post_parent_data = array();
foreach ( $fresh_posts as $fresh_post ) {
$post_parent_data[ (int) $fresh_post->ID ] = (int) $fresh_post->post_parent;
}
wp_cache_add_multiple( $post_parent_data, 'post_parent' );
}
}
}
/** /**
* Adds a suffix if any trashed posts have a given slug. * Adds a suffix if any trashed posts have a given slug.
* *

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.4-beta1-56762'; $wp_version = '6.4-beta1-56763';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.