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.


git-svn-id: https://develop.svn.wordpress.org/trunk@56925 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson
2023-10-12 23:39:05 +00:00
parent 9fb592eaa1
commit ac0bae2359
4 changed files with 61 additions and 19 deletions

View File

@@ -42,7 +42,7 @@ class Tests_Post_PrimePostParentIdCaches extends WP_UnitTestCase {
$num_queries = get_num_queries() - $before_num_queries;
$this->assertSame( 1, $num_queries, 'Unexpected number of queries.' );
$this->assertSameSets( array( 0 ), wp_cache_get_multiple( array( $post_id ), 'post_parent' ), 'Array of parent ids' );
$this->assertSameSets( array( 0 ), wp_cache_get_multiple( array( "post_parent:{$post_id}" ), 'posts' ), 'Array of parent ids' );
}
/**
@@ -53,8 +53,15 @@ class Tests_Post_PrimePostParentIdCaches extends WP_UnitTestCase {
_prime_post_parent_id_caches( self::$posts );
$num_queries = get_num_queries() - $before_num_queries;
$cache_keys = array_map(
function ( $post_id ) {
return "post_parent:{$post_id}";
},
self::$posts
);
$this->assertSame( 1, $num_queries, 'Unexpected number of queries.' );
$this->assertSameSets( array( 0, 0, 0 ), wp_cache_get_multiple( self::$posts, 'post_parent' ), 'Array of parent ids' );
$this->assertSameSets( array( 0, 0, 0 ), wp_cache_get_multiple( $cache_keys, 'posts' ), 'Array of parent ids' );
}
/**
@@ -84,7 +91,7 @@ class Tests_Post_PrimePostParentIdCaches extends WP_UnitTestCase {
$num_queries = get_num_queries() - $before_num_queries;
$this->assertSame( 1, $num_queries, 'Unexpected number of queries on first run' );
$this->assertSameSets( array( self::$posts[0] ), wp_cache_get_multiple( array( $page_id ), 'post_parent' ), 'Array of parent ids with post 0 as parent' );
$this->assertSameSets( array( self::$posts[0] ), wp_cache_get_multiple( array( "post_parent:{$page_id}" ), 'posts' ), 'Array of parent ids with post 0 as parent' );
wp_update_post(
array(
@@ -98,7 +105,7 @@ class Tests_Post_PrimePostParentIdCaches extends WP_UnitTestCase {
$num_queries = get_num_queries() - $before_num_queries;
$this->assertSame( 1, $num_queries, 'Unexpected number of queries on second run' );
$this->assertSameSets( array( self::$posts[1] ), wp_cache_get_multiple( array( $page_id ), 'post_parent' ), 'Array of parent ids with post 1 as parent' );
$this->assertSameSets( array( self::$posts[1] ), wp_cache_get_multiple( array( "post_parent:{$page_id}" ), 'posts' ), 'Array of parent ids with post 1 as parent' );
}
/**
@@ -121,11 +128,11 @@ class Tests_Post_PrimePostParentIdCaches extends WP_UnitTestCase {
$num_queries = get_num_queries() - $before_num_queries;
$this->assertSame( 1, $num_queries, 'Unexpected number of queries on first run' );
$this->assertSameSets( array( $parent_page_id ), wp_cache_get_multiple( array( $page_id ), 'post_parent' ), 'Array of parent ids with post 0 as parent' );
$this->assertSameSets( array( $parent_page_id ), wp_cache_get_multiple( array( "post_parent:{$page_id}" ), 'posts' ), 'Array of parent ids with post 0 as parent' );
wp_delete_post( $parent_page_id, true );
$this->assertSame( 1, $num_queries, 'Unexpected number of queries on second run' );
$this->assertSameSets( array( false ), wp_cache_get_multiple( array( $page_id ), 'post_parent' ), 'Array of parent ids with false values' );
$this->assertSameSets( array( false ), wp_cache_get_multiple( array( "post_parent:{$page_id}" ), 'posts' ), 'Array of parent ids with false values' );
}
}

View File

@@ -738,8 +738,15 @@ class Test_Query_CacheResults extends WP_UnitTestCase {
$query1 = new WP_Query();
$query1->query( $args );
$post_ids = wp_list_pluck( $query1->posts, 'ID' );
wp_cache_delete_multiple( $post_ids, 'post_parent' );
$post_ids = wp_list_pluck( $query1->posts, 'ID' );
$cache_keys = array_map(
function ( $post_id ) {
return "post_parent:{$post_id}";
},
$post_ids
);
wp_cache_delete_multiple( $cache_keys, 'posts' );
$queries_before = get_num_queries();
$query2 = new WP_Query();