Networks and Sites: Improve cache key generation in WP_Site_Query class.

Improve cache key generation in the `WP_Site_Query` class by removing `update_site_cache` and `update_site_meta_cache` elements in the array used to generate 
the cache key.  These elements do not affect that cache and by removing them, improve the likelihood of reusing an existing cache. 

Props Spacedmonkey, furi3r, johnbillion, johnjamesjacoby, flixos90.
Fixes #55462.



git-svn-id: https://develop.svn.wordpress.org/trunk@53097 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris 2022-04-07 17:44:34 +00:00
parent 7f26a0b5ce
commit 4b177da5da
2 changed files with 67 additions and 3 deletions

View File

@ -345,8 +345,8 @@ class WP_Site_Query {
// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
$_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'] );
// Ignore the $fields, $update_site_cache, $update_site_meta_cache argument as the queried result will be the same regardless.
unset( $_args['fields'], $_args['update_site_cache'], $_args['update_site_meta_cache'] );
$key = md5( serialize( $_args ) );
$last_changed = wp_cache_get_last_changed( 'sites' );

View File

@ -849,7 +849,7 @@ if ( is_multisite() ) :
)
);
$number_of_queries = $wpdb->num_queries;
$number_of_queries = get_num_queries();
$query_2 = $q->query(
array(
@ -863,6 +863,70 @@ if ( is_multisite() ) :
$this->assertSame( $number_of_queries + 1, $wpdb->num_queries );
}
/**
* @ticket 55462
*/
public function test_wp_site_query_cache_with_same_fields_same_cache_fields() {
$q = new WP_Site_Query();
$query_1 = $q->query(
array(
'fields' => 'ids',
'network_id' => self::$network_ids['wordpress.org/'],
'number' => 3,
'order' => 'ASC',
'update_site_cache' => true,
'update_site_meta_cache' => true,
)
);
$number_of_queries = get_num_queries();
$query_2 = $q->query(
array(
'fields' => 'ids',
'network_id' => self::$network_ids['wordpress.org/'],
'number' => 3,
'order' => 'ASC',
'update_site_cache' => true,
'update_site_meta_cache' => true,
)
);
$this->assertSame( $number_of_queries, get_num_queries() );
}
/**
* @ticket 55462
*/
public function test_wp_site_query_cache_with_same_fields_different_cache_fields() {
$q = new WP_Site_Query();
$query_1 = $q->query(
array(
'fields' => 'ids',
'network_id' => self::$network_ids['wordpress.org/'],
'number' => 3,
'order' => 'ASC',
'update_site_cache' => true,
'update_site_meta_cache' => true,
)
);
$number_of_queries = get_num_queries();
$query_2 = $q->query(
array(
'fields' => 'ids',
'network_id' => self::$network_ids['wordpress.org/'],
'number' => 3,
'order' => 'ASC',
'update_site_cache' => false,
'update_site_meta_cache' => false,
)
);
$this->assertSame( $number_of_queries, get_num_queries() );
}
/**
* @ticket 40229
* @dataProvider data_wp_site_query_meta_query