Skip to content

Commit 6ee3468

Browse files
committed
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 git-svn-id: https://develop.svn.wordpress.org/trunk@56763 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 53eff06 commit 6ee3468

5 files changed

Lines changed: 282 additions & 18 deletions

File tree

src/wp-includes/class-wp-query.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3179,36 +3179,36 @@ public function get_posts() {
31793179
$cached_results = wp_cache_get( $cache_key, 'post-queries', false, $cache_found );
31803180

31813181
if ( $cached_results ) {
3182-
if ( 'ids' === $q['fields'] ) {
3183-
/** @var int[] */
3184-
$this->posts = array_map( 'intval', $cached_results['posts'] );
3185-
} else {
3186-
_prime_post_caches( $cached_results['posts'], $q['update_post_term_cache'], $q['update_post_meta_cache'] );
3187-
/** @var WP_Post[] */
3188-
$this->posts = array_map( 'get_post', $cached_results['posts'] );
3189-
}
3182+
/** @var int[] */
3183+
$post_ids = array_map( 'intval', $cached_results['posts'] );
31903184

3191-
$this->post_count = count( $this->posts );
3185+
$this->post_count = count( $post_ids );
31923186
$this->found_posts = $cached_results['found_posts'];
31933187
$this->max_num_pages = $cached_results['max_num_pages'];
31943188

31953189
if ( 'ids' === $q['fields'] ) {
3190+
$this->posts = $post_ids;
3191+
31963192
return $this->posts;
31973193
} elseif ( 'id=>parent' === $q['fields'] ) {
3194+
_prime_post_parents_caches( $post_ids );
3195+
31983196
/** @var int[] */
3199-
$post_parents = array();
3197+
$post_parents = wp_cache_get_multiple( $post_ids, 'post_parent' );
32003198

3201-
foreach ( $this->posts as $key => $post ) {
3199+
foreach ( $post_parents as $id => $post_parent ) {
32023200
$obj = new stdClass();
3203-
$obj->ID = (int) $post->ID;
3204-
$obj->post_parent = (int) $post->post_parent;
3205-
3206-
$this->posts[ $key ] = $obj;
3201+
$obj->ID = (int) $id;
3202+
$obj->post_parent = (int) $post_parent;
32073203

3208-
$post_parents[ $obj->ID ] = $obj->post_parent;
3204+
$this->posts[] = $obj;
32093205
}
32103206

32113207
return $post_parents;
3208+
} else {
3209+
_prime_post_caches( $post_ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
3210+
/** @var WP_Post[] */
3211+
$this->posts = array_map( 'get_post', $post_ids );
32123212
}
32133213
}
32143214
}
@@ -3256,6 +3256,8 @@ public function get_posts() {
32563256
$post_parents[ (int) $post->ID ] = (int) $post->post_parent;
32573257
$post_ids[] = (int) $post->ID;
32583258
}
3259+
// Prime post parent caches, so that on second run, there is not another database query.
3260+
wp_cache_add_multiple( $post_parents, 'post_parent' );
32593261

32603262
if ( $q['cache_results'] && $id_query_is_cacheable ) {
32613263
$cache_value = array(

src/wp-includes/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7169,7 +7169,7 @@ function _get_non_cached_ids( $object_ids, $cache_group ) {
71697169
$cache_values = wp_cache_get_multiple( $object_ids, $cache_group );
71707170

71717171
foreach ( $cache_values as $id => $value ) {
7172-
if ( ! $value ) {
7172+
if ( false === $value ) {
71737173
$non_cached_ids[] = (int) $id;
71747174
}
71757175
}

src/wp-includes/post.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7262,6 +7262,7 @@ function clean_post_cache( $post ) {
72627262

72637263
wp_cache_delete( $post->ID, 'posts' );
72647264
wp_cache_delete( $post->ID, 'post_meta' );
7265+
wp_cache_delete( $post->ID, 'post_parent' );
72657266

72667267
clean_object_term_cache( $post->ID, $post->post_type );
72677268

@@ -7795,6 +7796,31 @@ function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache
77957796
}
77967797
}
77977798

7799+
/**
7800+
* Prime post parent caches.
7801+
*
7802+
* @global wpdb $wpdb WordPress database abstraction object.
7803+
*
7804+
* @param int[] $ids ID list.
7805+
*/
7806+
function _prime_post_parents_caches( array $ids ) {
7807+
global $wpdb;
7808+
7809+
$non_cached_ids = _get_non_cached_ids( $ids, 'post_parent' );
7810+
if ( ! empty( $non_cached_ids ) ) {
7811+
$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 ) ) );
7812+
7813+
if ( $fresh_posts ) {
7814+
$post_parent_data = array();
7815+
foreach ( $fresh_posts as $fresh_post ) {
7816+
$post_parent_data[ (int) $fresh_post->ID ] = (int) $fresh_post->post_parent;
7817+
}
7818+
7819+
wp_cache_add_multiple( $post_parent_data, 'post_parent' );
7820+
}
7821+
}
7822+
}
7823+
77987824
/**
77997825
* Adds a suffix if any trashed posts have a given slug.
78007826
*
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
/**
3+
* Test `_prime_post_parents_caches()`.
4+
*
5+
* @package WordPress
6+
*/
7+
8+
/**
9+
* Test class for `_prime_post_parents_caches()`.
10+
*
11+
* @group post
12+
* @group cache
13+
*
14+
* @covers ::_prime_post_parents_caches
15+
*/
16+
class Tests_Post_PrimePostParentsCaches extends WP_UnitTestCase {
17+
18+
/**
19+
* Post IDs.
20+
*
21+
* @var int[]
22+
*/
23+
public static $posts;
24+
25+
/**
26+
* Set up test resources before the class.
27+
*
28+
* @param WP_UnitTest_Factory $factory The unit test factory.
29+
*/
30+
public static function wpSetupBeforeClass( WP_UnitTest_Factory $factory ) {
31+
self::$posts = $factory->post->create_many( 3 );
32+
}
33+
34+
/**
35+
* @ticket 59188
36+
*/
37+
public function test_prime_post_parents_caches() {
38+
$post_id = self::$posts[0];
39+
40+
$before_num_queries = get_num_queries();
41+
_prime_post_parents_caches( array( $post_id ) );
42+
$num_queries = get_num_queries() - $before_num_queries;
43+
44+
$this->assertSame( 1, $num_queries, 'Unexpected number of queries.' );
45+
$this->assertSameSets( array( 0 ), wp_cache_get_multiple( array( $post_id ), 'post_parent' ), 'Array of parent ids' );
46+
}
47+
48+
/**
49+
* @ticket 59188
50+
*/
51+
public function test_prime_post_parents_caches_multiple() {
52+
$before_num_queries = get_num_queries();
53+
_prime_post_parents_caches( self::$posts );
54+
$num_queries = get_num_queries() - $before_num_queries;
55+
56+
$this->assertSame( 1, $num_queries, 'Unexpected number of queries.' );
57+
$this->assertSameSets( array( 0, 0, 0 ), wp_cache_get_multiple( self::$posts, 'post_parent' ), 'Array of parent ids' );
58+
}
59+
60+
/**
61+
* @ticket 59188
62+
*/
63+
public function test_prime_post_parents_caches_multiple_runs() {
64+
_prime_post_parents_caches( self::$posts );
65+
$before_num_queries = get_num_queries();
66+
_prime_post_parents_caches( self::$posts );
67+
$num_queries = get_num_queries() - $before_num_queries;
68+
69+
$this->assertSame( 0, $num_queries, 'Unexpected number of queries.' );
70+
}
71+
72+
/**
73+
* @ticket 59188
74+
*/
75+
public function test_prime_post_parents_caches_update() {
76+
$page_id = self::factory()->post->create(
77+
array(
78+
'post_type' => 'page',
79+
'post_parent' => self::$posts[0],
80+
)
81+
);
82+
$before_num_queries = get_num_queries();
83+
_prime_post_parents_caches( array( $page_id ) );
84+
$num_queries = get_num_queries() - $before_num_queries;
85+
86+
$this->assertSame( 1, $num_queries, 'Unexpected number of queries on first run' );
87+
$this->assertSameSets( array( self::$posts[0] ), wp_cache_get_multiple( array( $page_id ), 'post_parent' ), 'Array of parent ids with post 0 as parent' );
88+
89+
wp_update_post(
90+
array(
91+
'ID' => $page_id,
92+
'post_parent' => self::$posts[1],
93+
)
94+
);
95+
96+
$before_num_queries = get_num_queries();
97+
_prime_post_parents_caches( array( $page_id ) );
98+
$num_queries = get_num_queries() - $before_num_queries;
99+
100+
$this->assertSame( 1, $num_queries, 'Unexpected number of queries on second run' );
101+
$this->assertSameSets( array( self::$posts[1] ), wp_cache_get_multiple( array( $page_id ), 'post_parent' ), 'Array of parent ids with post 1 as parent' );
102+
103+
}
104+
105+
/**
106+
* @ticket 59188
107+
*/
108+
public function test_prime_post_parents_caches_delete() {
109+
$parent_page_id = self::factory()->post->create(
110+
array(
111+
'post_type' => 'page',
112+
)
113+
);
114+
$page_id = self::factory()->post->create(
115+
array(
116+
'post_type' => 'page',
117+
'post_parent' => $parent_page_id,
118+
)
119+
);
120+
$before_num_queries = get_num_queries();
121+
_prime_post_parents_caches( array( $page_id ) );
122+
$num_queries = get_num_queries() - $before_num_queries;
123+
124+
$this->assertSame( 1, $num_queries, 'Unexpected number of queries on first run' );
125+
$this->assertSameSets( array( $parent_page_id ), wp_cache_get_multiple( array( $page_id ), 'post_parent' ), 'Array of parent ids with post 0 as parent' );
126+
127+
wp_delete_post( $parent_page_id, true );
128+
129+
$this->assertSame( 1, $num_queries, 'Unexpected number of queries on second run' );
130+
$this->assertSameSets( array( false ), wp_cache_get_multiple( array( $page_id ), 'post_parent' ), 'Array of parent ids with false values' );
131+
}
132+
}

tests/phpunit/tests/query/cacheResults.php

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ public function test_query_cache_different_fields() {
710710
$query2->query( $args );
711711
$queries_after = get_num_queries();
712712

713-
$this->assertSame( $queries_before, $queries_after );
713+
$this->assertSame( 1, $queries_after - $queries_before );
714714
$this->assertCount( 5, $query1->posts );
715715
$this->assertCount( 5, $query2->posts );
716716
$this->assertSame( $query1->found_posts, $query2->found_posts );
@@ -726,6 +726,110 @@ public function test_query_cache_different_fields() {
726726
$this->assertNotEquals( $query1->posts, $query2->posts );
727727
}
728728

729+
730+
/**
731+
* @ticket 59188
732+
*/
733+
public function test_query_cache_unprimed_parents() {
734+
$args = array(
735+
'cache_results' => true,
736+
'fields' => 'id=>parent',
737+
);
738+
$query1 = new WP_Query();
739+
$query1->query( $args );
740+
741+
$post_ids = wp_list_pluck( $query1->posts, 'ID' );
742+
wp_cache_delete_multiple( $post_ids, 'post_parent' );
743+
744+
$queries_before = get_num_queries();
745+
$query2 = new WP_Query();
746+
$query2->query( $args );
747+
$queries_after = get_num_queries();
748+
749+
$this->assertSame( 1, $queries_after - $queries_before, 'There should be only one query to prime parents' );
750+
$this->assertCount( 5, $query1->posts, 'There should be only 5 posts returned on first query' );
751+
$this->assertCount( 5, $query2->posts, 'There should be only 5 posts returned on second query' );
752+
$this->assertSame( $query1->found_posts, $query2->found_posts, 'Found posts should match on second query' );
753+
}
754+
755+
/**
756+
* @ticket 59188
757+
*/
758+
public function test_query_cache_update_parent() {
759+
$page_id = self::factory()->post->create(
760+
array(
761+
'post_type' => 'page',
762+
'post_parent' => self::$pages[0],
763+
)
764+
);
765+
$args = array(
766+
'cache_results' => true,
767+
'post_type' => 'page',
768+
'fields' => 'id=>parent',
769+
'post__in' => array(
770+
$page_id,
771+
),
772+
);
773+
$query1 = new WP_Query();
774+
$query1->query( $args );
775+
776+
wp_update_post(
777+
array(
778+
'ID' => $page_id,
779+
'post_parent' => self::$pages[1],
780+
)
781+
);
782+
783+
$queries_before = get_num_queries();
784+
$query2 = new WP_Query();
785+
$query2->query( $args );
786+
$queries_after = get_num_queries();
787+
788+
$this->assertSame( self::$pages[0], $query1->posts[0]->post_parent, 'Check post parent on first query' );
789+
$this->assertSame( self::$pages[1], $query2->posts[0]->post_parent, 'Check post parent on second query' );
790+
$this->assertSame( 2, $queries_after - $queries_before, 'There should be 2 queries, one for id=>parent' );
791+
$this->assertSame( $query1->found_posts, $query2->found_posts, 'Found posts should match on second query' );
792+
}
793+
794+
/**
795+
* @ticket 59188
796+
*/
797+
public function test_query_cache_delete_parent() {
798+
$parent_page_id = self::factory()->post->create(
799+
array(
800+
'post_type' => 'page',
801+
)
802+
);
803+
$page_id = self::factory()->post->create(
804+
array(
805+
'post_type' => 'page',
806+
'post_parent' => $parent_page_id,
807+
)
808+
);
809+
$args = array(
810+
'cache_results' => true,
811+
'post_type' => 'page',
812+
'fields' => 'id=>parent',
813+
'post__in' => array(
814+
$page_id,
815+
),
816+
);
817+
$query1 = new WP_Query();
818+
$query1->query( $args );
819+
820+
wp_delete_post( $parent_page_id, true );
821+
822+
$queries_before = get_num_queries();
823+
$query2 = new WP_Query();
824+
$query2->query( $args );
825+
$queries_after = get_num_queries();
826+
827+
$this->assertSame( $parent_page_id, $query1->posts[0]->post_parent, 'Check post parent on first query' );
828+
$this->assertSame( 0, $query2->posts[0]->post_parent, 'Check post parent on second query' );
829+
$this->assertSame( 2, $queries_after - $queries_before, 'There should be 2 queries, one for id=>parent' );
830+
$this->assertSame( $query1->found_posts, $query2->found_posts, 'Found posts should match on second query' );
831+
}
832+
729833
/**
730834
* @ticket 22176
731835
*/

0 commit comments

Comments
 (0)