From 4b177da5dac3e8fd2c2b99c8b40ae9f5e0e40681 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 7 Apr 2022 17:44:34 +0000 Subject: [PATCH] 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 --- src/wp-includes/class-wp-site-query.php | 4 +- tests/phpunit/tests/multisite/wpSiteQuery.php | 66 ++++++++++++++++++- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-site-query.php b/src/wp-includes/class-wp-site-query.php index 1a4df65d99..af227c572e 100644 --- a/src/wp-includes/class-wp-site-query.php +++ b/src/wp-includes/class-wp-site-query.php @@ -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' ); diff --git a/tests/phpunit/tests/multisite/wpSiteQuery.php b/tests/phpunit/tests/multisite/wpSiteQuery.php index f48667f23f..8d1d84c1f0 100644 --- a/tests/phpunit/tests/multisite/wpSiteQuery.php +++ b/tests/phpunit/tests/multisite/wpSiteQuery.php @@ -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