Multisite: Improve caching in `WP_Site_Query` by ignoring the `$fields` argument.

Prior to this change there were two different cache keys used for the same query. That is because regardless of the `$fields` argument, the query response will be the same.

Props spacedmonkey.
Fixes #41197.


git-svn-id: https://develop.svn.wordpress.org/trunk@41059 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Felix Arntz 2017-07-14 21:24:03 +00:00
parent d18c6696c2
commit c8e811cc7e
2 changed files with 82 additions and 1 deletions

View File

@ -251,7 +251,12 @@ class WP_Site_Query {
do_action_ref_array( 'pre_get_sites', array( &$this ) );
// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
$key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
// Ignore the $fields argument as the queried result will be the same regardless.
unset( $_args['fields'] );
$key = md5( serialize( $_args ) );
$last_changed = wp_cache_get_last_changed( 'sites' );
$cache_key = "get_sites:$key:$last_changed";

View File

@ -661,6 +661,82 @@ class Tests_Multisite_Site_Query extends WP_UnitTestCase {
$this->assertEqualSets( $expected, $found );
}
/**
* @ticket 41197
*/
public function test_wp_site_query_cache_with_different_fields_no_count() {
global $wpdb;
$q = new WP_Site_Query();
$query_1 = $q->query( array(
'fields' => 'all',
'network_id' => self::$network_ids['wordpress.org/'],
'number' => 3,
'order' => 'ASC',
) );
$number_of_queries = $wpdb->num_queries;
$query_2 = $q->query( array(
'fields' => 'ids',
'network_id' => self::$network_ids['wordpress.org/'],
'number' => 3,
'order' => 'ASC',
) );
$this->assertEquals( $number_of_queries, $wpdb->num_queries );
}
/**
* @ticket 41197
*/
public function test_wp_site_query_cache_with_different_fields_active_count() {
global $wpdb;
$q = new WP_Site_Query();
$query_1 = $q->query( array(
'fields' => 'all',
'network_id' => self::$network_ids['wordpress.org/'],
'number' => 3,
'order' => 'ASC',
'count' => true,
) );
$number_of_queries = $wpdb->num_queries;
$query_2 = $q->query( array(
'fields' => 'ids',
'network_id' => self::$network_ids['wordpress.org/'],
'number' => 3,
'order' => 'ASC',
'count' => true,
) );
$this->assertEquals( $number_of_queries, $wpdb->num_queries );
}
/**
* @ticket 41197
*/
public function test_wp_site_query_cache_with_same_fields_different_count() {
global $wpdb;
$q = new WP_Site_Query();
$query_1 = $q->query( array(
'fields' => 'ids',
'network_id' => self::$network_ids['wordpress.org/'],
'number' => 3,
'order' => 'ASC',
) );
$number_of_queries = $wpdb->num_queries;
$query_2 = $q->query( array(
'fields' => 'ids',
'network_id' => self::$network_ids['wordpress.org/'],
'number' => 3,
'order' => 'ASC',
'count' => true,
) );
$this->assertEquals( $number_of_queries + 1, $wpdb->num_queries );
}
}
endif;