From 761fb95954cf0546ac158db6ccfeda0c7ab42023 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Thu, 7 Apr 2022 17:57:59 +0000 Subject: [PATCH] Networks and Sites: Improve cache key generation in `WP_Network_Query` class. Improve cache key generation in the `WP_Network_Query` class by removing `update_network_cache` element in the array used to generate the cache key. This element does not affect that cache and by removing it, it improves the likelihood of reusing an existing cache. Props Spacedmonkey, furi3r, johnbillion, johnjamesjacoby, flixos90. Fixes #55461. git-svn-id: https://develop.svn.wordpress.org/trunk@53098 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-network-query.php | 4 +- .../tests/multisite/wpNetworkQuery.php | 54 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-network-query.php b/src/wp-includes/class-wp-network-query.php index 429143976c..ffdb956ad6 100644 --- a/src/wp-includes/class-wp-network-query.php +++ b/src/wp-includes/class-wp-network-query.php @@ -242,8 +242,8 @@ class WP_Network_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_network_cache arguments as the queried result will be the same regardless. + unset( $_args['fields'], $_args['update_network_cache'] ); $key = md5( serialize( $_args ) ); $last_changed = wp_cache_get_last_changed( 'networks' ); diff --git a/tests/phpunit/tests/multisite/wpNetworkQuery.php b/tests/phpunit/tests/multisite/wpNetworkQuery.php index 42fcc21f96..29b2b8f5f7 100644 --- a/tests/phpunit/tests/multisite/wpNetworkQuery.php +++ b/tests/phpunit/tests/multisite/wpNetworkQuery.php @@ -509,6 +509,60 @@ if ( is_multisite() ) : $this->assertSame( $number_of_queries + 1, $wpdb->num_queries ); } + /** + * @ticket 55461 + */ + public function test_wp_network_query_cache_with_same_fields_same_cache_field() { + $q = new WP_Network_Query(); + $query_1 = $q->query( + array( + 'fields' => 'all', + 'number' => 3, + 'order' => 'ASC', + 'update_network_cache' => true, + ) + ); + $number_of_queries = get_num_queries(); + + $query_2 = $q->query( + array( + 'fields' => 'all', + 'number' => 3, + 'order' => 'ASC', + 'update_network_cache' => true, + ) + ); + + $this->assertSame( $number_of_queries, get_num_queries() ); + } + + /** + * @ticket 55461 + */ + public function test_wp_network_query_cache_with_same_fields_different_cache_field() { + $q = new WP_Network_Query(); + $query_1 = $q->query( + array( + 'fields' => 'all', + 'number' => 3, + 'order' => 'ASC', + 'update_network_cache' => true, + ) + ); + $number_of_queries = get_num_queries(); + + $query_2 = $q->query( + array( + 'fields' => 'all', + 'number' => 3, + 'order' => 'ASC', + 'update_network_cache' => false, + ) + ); + + $this->assertSame( $number_of_queries, get_num_queries() ); + } + /** * @ticket 45749 * @ticket 47599