Skip to content

Commit 92a8bd8

Browse files
committed
Query: Save excessive cache add and sets in WP_Query.
In [53941] database query caching was added to `WP_Query`. However on sites with persistent object caching enabled, this resulted in a high number of unnecessary cache set and adds being run on every request. Caches are not set, if the query cache already exists and is cached. Replace usage of `update_post_caches` with `_prime_post_caches` to ensure that only posts that are not in cache are primed. Props spacedmonkey, peterwilsoncc, mukesh27. See #22176. git-svn-id: https://develop.svn.wordpress.org/trunk@54352 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 7a74026 commit 92a8bd8

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3099,10 +3099,10 @@ public function get_posts() {
30993099
$last_changed .= wp_cache_get_last_changed( 'terms' );
31003100
}
31013101

3102-
$cache_key = "wp_query:$key:$last_changed";
3103-
3102+
$cache_key = "wp_query:$key:$last_changed";
3103+
$cache_found = false;
31043104
if ( null === $this->posts ) {
3105-
$cached_results = wp_cache_get( $cache_key, 'posts' );
3105+
$cached_results = wp_cache_get( $cache_key, 'posts', false, $cache_found );
31063106

31073107
if ( $cached_results ) {
31083108
if ( 'ids' === $q['fields'] ) {
@@ -3256,7 +3256,7 @@ public function get_posts() {
32563256
$this->posts = array_map( 'get_post', $this->posts );
32573257
}
32583258

3259-
if ( $q['cache_results'] && $id_query_is_cacheable ) {
3259+
if ( $q['cache_results'] && $id_query_is_cacheable && ! $cache_found ) {
32603260
$post_ids = wp_list_pluck( $this->posts, 'ID' );
32613261

32623262
$cache_value = array(
@@ -3455,7 +3455,8 @@ public function get_posts() {
34553455
$this->posts = array_map( 'get_post', $this->posts );
34563456

34573457
if ( $q['cache_results'] ) {
3458-
update_post_caches( $this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
3458+
$post_ids = wp_list_pluck( $this->posts, 'ID' );
3459+
_prime_post_caches( $post_ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
34593460
}
34603461

34613462
/** @var WP_Post */

tests/phpunit/tests/query/cacheResults.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,4 +984,46 @@ public function test_query_cache_should_not_exclude_post_when_excluded_term_is_r
984984
$this->assertContains( $post_id, $post_ids_q2, 'Second query does not include the post ID.' );
985985
$this->assertNotSame( $num_queries, get_num_queries(), 'Removing term does not invalidate previous cache.' );
986986
}
987+
988+
/**
989+
* @ticket 22176
990+
* @dataProvider data_query_cache_with_empty_result_set
991+
*/
992+
public function test_query_cache_with_empty_result_set( $fields_q1, $fields_q2 ) {
993+
_delete_all_posts();
994+
995+
$args_q1 = array(
996+
'fields' => $fields_q1,
997+
);
998+
999+
$query_1 = new WP_Query();
1000+
$posts_q1 = $query_1->query( $args_q1 );
1001+
$this->assertEmpty( $posts_q1, 'First query does not return an empty result set.' );
1002+
1003+
$args_q2 = array(
1004+
'fields' => $fields_q2,
1005+
);
1006+
1007+
$num_queries = get_num_queries();
1008+
$query_2 = new WP_Query();
1009+
$posts_q2 = $query_2->query( $args_q2 );
1010+
$this->assertEmpty( $posts_q2, 'Second query does not return an empty result set.' );
1011+
$this->assertSame( $num_queries, get_num_queries(), 'Second query is not cached.' );
1012+
}
1013+
1014+
public function data_query_cache_with_empty_result_set() {
1015+
return array(
1016+
array( '', '' ),
1017+
array( '', 'ids' ),
1018+
array( '', 'id=>parent' ),
1019+
1020+
array( 'ids', '' ),
1021+
array( 'ids', 'ids' ),
1022+
array( 'ids', 'id=>parent' ),
1023+
1024+
array( 'id=>parent', '' ),
1025+
array( 'id=>parent', 'ids' ),
1026+
array( 'id=>parent', 'id=>parent' ),
1027+
);
1028+
}
9871029
}

0 commit comments

Comments
 (0)