From 4258f31a3b6a44e195f78e3a45c9282e0693715c Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Thu, 12 Oct 2023 23:41:23 +0000 Subject: [PATCH] Query: Cache post parent IDs in `posts` group. Move the cache of post parent IDs from the dedicated group `post_parents` to `posts`. This maintains backward compatibility for clearing all post object related data by calling `wp_cache_flush_group( 'posts' )`. Post parent IDs are now cached with with the prefix `post_parent:` in the `posts` group. Follow up to [56763]. Props spacedmonkey, joemcgill, peterwilsoncc. See #59188. Built from https://develop.svn.wordpress.org/trunk@56925 git-svn-id: http://core.svn.wordpress.org/trunk@56436 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-query.php | 22 +++++++++++++++------- wp-includes/post.php | 28 ++++++++++++++++++++++++---- wp-includes/version.php | 2 +- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/wp-includes/class-wp-query.php b/wp-includes/class-wp-query.php index e79f460fc5..a9ed269d72 100644 --- a/wp-includes/class-wp-query.php +++ b/wp-includes/class-wp-query.php @@ -3192,12 +3192,17 @@ class WP_Query { } elseif ( 'id=>parent' === $q['fields'] ) { _prime_post_parent_id_caches( $post_ids ); - /** @var int[] */ - $post_parents = wp_cache_get_multiple( $post_ids, 'post_parent' ); + $post_parent_cache_keys = array(); + foreach ( $post_ids as $post_id ) { + $post_parent_cache_keys[] = 'post_parent:' . (string) $post_id; + } - foreach ( $post_parents as $id => $post_parent ) { + /** @var int[] */ + $post_parents = wp_cache_get_multiple( $post_parent_cache_keys, 'posts' ); + + foreach ( $post_parents as $cache_key => $post_parent ) { $obj = new stdClass(); - $obj->ID = (int) $id; + $obj->ID = (int) str_replace( 'post_parent:', '', $cache_key ); $obj->post_parent = (int) $post_parent; $this->posts[] = $obj; @@ -3245,8 +3250,9 @@ class WP_Query { $this->set_found_posts( $q, $limits ); /** @var int[] */ - $post_parents = array(); - $post_ids = array(); + $post_parents = array(); + $post_ids = array(); + $post_parents_cache = array(); foreach ( $this->posts as $key => $post ) { $this->posts[ $key ]->ID = (int) $post->ID; @@ -3254,9 +3260,11 @@ class WP_Query { $post_parents[ (int) $post->ID ] = (int) $post->post_parent; $post_ids[] = (int) $post->ID; + + $post_parents_cache[ 'post_parent:' . (string) $post->ID ] = (int) $post->post_parent; } // Prime post parent caches, so that on second run, there is not another database query. - wp_cache_add_multiple( $post_parents, 'post_parent' ); + wp_cache_add_multiple( $post_parents_cache, 'posts' ); if ( $q['cache_results'] && $id_query_is_cacheable ) { $cache_value = array( diff --git a/wp-includes/post.php b/wp-includes/post.php index c3910d2b3a..edad5218fa 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -7270,8 +7270,8 @@ function clean_post_cache( $post ) { } wp_cache_delete( $post->ID, 'posts' ); + wp_cache_delete( 'post_parent:' . (string) $post->ID, 'posts' ); wp_cache_delete( $post->ID, 'post_meta' ); - wp_cache_delete( $post->ID, 'post_parent' ); clean_object_term_cache( $post->ID, $post->post_type ); @@ -7817,17 +7817,37 @@ function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache function _prime_post_parent_id_caches( array $ids ) { global $wpdb; - $non_cached_ids = _get_non_cached_ids( $ids, 'post_parent' ); + $ids = array_filter( $ids, '_validate_cache_id' ); + $ids = array_unique( array_map( 'intval', $ids ), SORT_NUMERIC ); + + if ( empty( $ids ) ) { + return; + } + + $cache_keys = array(); + foreach ( $ids as $id ) { + $cache_keys[ $id ] = 'post_parent:' . (string) $id; + } + + $cached_data = wp_cache_get_multiple( array_values( $cache_keys ), 'posts' ); + + $non_cached_ids = array(); + foreach ( $cache_keys as $id => $cache_key ) { + if ( false === $cached_data[ $cache_key ] ) { + $non_cached_ids[] = $id; + } + } + 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; + $post_parent_data[ 'post_parent:' . (string) $fresh_post->ID ] = (int) $fresh_post->post_parent; } - wp_cache_add_multiple( $post_parent_data, 'post_parent' ); + wp_cache_add_multiple( $post_parent_data, 'posts' ); } } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 6c571591b5..16a9fdd027 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.4-beta4-56923'; +$wp_version = '6.4-beta4-56925'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.