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
This commit is contained in:
Peter Wilson 2023-10-12 23:41:23 +00:00
parent 929aa8b970
commit 4258f31a3b
3 changed files with 40 additions and 12 deletions

View File

@ -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;
@ -3247,6 +3252,7 @@ class WP_Query {
/** @var int[] */
$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(

View File

@ -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' );
}
}
}

View File

@ -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.