Make WordPress Core

Changeset 41063


Ignore:
Timestamp:
07/17/2017 08:15:27 PM (7 years ago)
Author:
flixos90
Message:

Multisite: Improve caching in WP_Network_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. This was already fixed for WP_Site_Query in [41059].

Props spacedmonkey.
Fixes #41347.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-network-query.php

    r41005 r41063  
    209209
    210210        // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
    211         $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
     211        $_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
     212
     213        // Ignore the $fields argument as the queried result will be the same regardless.
     214        unset( $_args['fields'] );
     215
     216        $key = md5( serialize( $_args ) );
    212217        $last_changed = wp_cache_get_last_changed( 'networks' );
    213218
  • trunk/tests/phpunit/tests/multisite/networkQuery.php

    r38102 r41063  
    380380        $this->assertEquals( $expected, $found );
    381381    }
     382
     383    /**
     384     * @ticket 41347
     385     */
     386    public function test_wp_network_query_cache_with_different_fields_no_count() {
     387        global $wpdb;
     388
     389        $q                 = new WP_Network_Query();
     390        $query_1           = $q->query( array(
     391            'fields'     => 'all',
     392            'number'     => 3,
     393            'order'      => 'ASC',
     394        ) );
     395        $number_of_queries = $wpdb->num_queries;
     396
     397        $query_2 = $q->query( array(
     398            'fields'     => 'ids',
     399            'number'     => 3,
     400            'order'      => 'ASC',
     401        ) );
     402
     403        $this->assertEquals( $number_of_queries, $wpdb->num_queries );
     404    }
     405
     406    /**
     407     * @ticket 41347
     408     */
     409    public function test_wp_network_query_cache_with_different_fields_active_count() {
     410        global $wpdb;
     411
     412        $q = new WP_Network_Query();
     413
     414        $query_1           = $q->query( array(
     415            'fields'     => 'all',
     416            'number'     => 3,
     417            'order'      => 'ASC',
     418            'count'      => true,
     419        ) );
     420        $number_of_queries = $wpdb->num_queries;
     421
     422        $query_2 = $q->query( array(
     423            'fields'     => 'ids',
     424            'number'     => 3,
     425            'order'      => 'ASC',
     426            'count'      => true,
     427        ) );
     428        $this->assertEquals( $number_of_queries, $wpdb->num_queries );
     429    }
     430
     431    /**
     432     * @ticket 41347
     433     */
     434    public function test_wp_network_query_cache_with_same_fields_different_count() {
     435        global $wpdb;
     436
     437        $q = new WP_Network_Query();
     438
     439        $query_1 = $q->query( array(
     440            'fields'     => 'ids',
     441            'number'     => 3,
     442            'order'      => 'ASC',
     443        ) );
     444
     445        $number_of_queries = $wpdb->num_queries;
     446
     447        $query_2 = $q->query( array(
     448            'fields'     => 'ids',
     449            'number'     => 3,
     450            'order'      => 'ASC',
     451            'count'      => true,
     452        ) );
     453        $this->assertEquals( $number_of_queries + 1, $wpdb->num_queries );
     454    }
    382455}
    383456
Note: See TracChangeset for help on using the changeset viewer.