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
This commit is contained in:
Jonny Harris
2022-09-29 10:07:34 +00:00
parent 7a74026b21
commit 92a8bd811f
2 changed files with 48 additions and 5 deletions

View File

@@ -984,4 +984,46 @@ class Test_Query_CacheResults extends WP_UnitTestCase {
$this->assertContains( $post_id, $post_ids_q2, 'Second query does not include the post ID.' );
$this->assertNotSame( $num_queries, get_num_queries(), 'Removing term does not invalidate previous cache.' );
}
/**
* @ticket 22176
* @dataProvider data_query_cache_with_empty_result_set
*/
public function test_query_cache_with_empty_result_set( $fields_q1, $fields_q2 ) {
_delete_all_posts();
$args_q1 = array(
'fields' => $fields_q1,
);
$query_1 = new WP_Query();
$posts_q1 = $query_1->query( $args_q1 );
$this->assertEmpty( $posts_q1, 'First query does not return an empty result set.' );
$args_q2 = array(
'fields' => $fields_q2,
);
$num_queries = get_num_queries();
$query_2 = new WP_Query();
$posts_q2 = $query_2->query( $args_q2 );
$this->assertEmpty( $posts_q2, 'Second query does not return an empty result set.' );
$this->assertSame( $num_queries, get_num_queries(), 'Second query is not cached.' );
}
public function data_query_cache_with_empty_result_set() {
return array(
array( '', '' ),
array( '', 'ids' ),
array( '', 'id=>parent' ),
array( 'ids', '' ),
array( 'ids', 'ids' ),
array( 'ids', 'id=>parent' ),
array( 'id=>parent', '' ),
array( 'id=>parent', 'ids' ),
array( 'id=>parent', 'id=>parent' ),
);
}
}