Make WordPress Core

Changeset 37868


Ignore:
Timestamp:
06/26/2016 12:45:29 PM (8 years ago)
Author:
jeremyfelt
Message:

Multisite: Cache found_sites and max_num_pages in WP_Site_Query.

This avoids a second uncached query used to determine found rows. Instead, we can cache the number of found sites and the max number of pages for reuse when the same query is requested in the future.

Props spacedmonkey.
See #35791.

File:
1 edited

Legend:

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

    r37735 r37868  
    232232     * @access public
    233233     *
    234      * @global wpdb $wpdb WordPress database abstraction object.
    235      *
    236234     * @return array|int List of sites, or number of sites when 'count' is passed as a query var.
    237235     */
    238236    public function get_sites() {
    239         global $wpdb;
    240 
    241237        $this->parse_query();
    242238
     
    257253            wp_cache_set( 'last_changed', $last_changed, 'sites' );
    258254        }
    259         $cache_key = "get_site_ids:$key:$last_changed";
    260 
    261         $site_ids = wp_cache_get( $cache_key, 'sites' );
    262         if ( false === $site_ids ) {
     255
     256        $cache_key = "get_sites:$key:$last_changed";
     257        $cache_value = wp_cache_get( $cache_key, 'sites' );
     258
     259        if ( false === $cache_value ) {
    263260            $site_ids = $this->get_site_ids();
    264             wp_cache_add( $cache_key, $site_ids, 'sites' );
     261            if ( $site_ids ) {
     262                $this->set_found_sites();
     263            }
     264
     265            $cache_value = array(
     266                'site_ids' => $site_ids,
     267                'found_sites' => $this->found_sites,
     268                'max_num_pages' => $this->max_num_pages,
     269            );
     270            wp_cache_add( $cache_key, $cache_value, 'sites' );
     271        } else {
     272            $site_ids = $cache_value['site_ids'];
     273            $this->found_sites = $cache_value['found_sites'];
     274            $this->max_num_pages = $cache_value['max_num_pages'];
    265275        }
    266276
     
    274284
    275285        $this->site_count = count( $this->sites );
    276 
    277         if ( $site_ids && $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
    278             /**
    279              * Filters the query used to retrieve found site count.
    280              *
    281              * @since 4.6.0
    282              *
    283              * @param string        $found_sites_query SQL query. Default 'SELECT FOUND_ROWS()'.
    284              * @param WP_Site_Query $site_query        The `WP_Site_Query` instance.
    285              */
    286             $found_sites_query = apply_filters( 'found_sites_query', 'SELECT FOUND_ROWS()', $this );
    287 
    288             $this->found_sites = (int) $wpdb->get_var( $found_sites_query );
    289             $this->max_num_pages = ceil( $this->found_sites / $this->query_vars['number'] );
    290         }
    291286
    292287        if ( 'ids' == $this->query_vars['fields'] ) {
     
    570565
    571566        return array_map( 'intval', $site_ids );
     567    }
     568
     569    /**
     570     * Populates found_sites and max_num_pages properties for the current query
     571     * if the limit clause was used.
     572     *
     573     * @since 4.6.0
     574     * @access private
     575     *
     576     * @global wpdb $wpdb WordPress database abstraction object.
     577     */
     578    private function set_found_sites() {
     579        global $wpdb;
     580
     581        if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
     582            /**
     583             * Filters the query used to retrieve found site count.
     584             *
     585             * @since 4.6.0
     586             *
     587             * @param string        $found_sites_query SQL query. Default 'SELECT FOUND_ROWS()'.
     588             * @param WP_Site_Query $site_query        The `WP_Site_Query` instance.
     589             */
     590            $found_sites_query = apply_filters( 'found_sites_query', 'SELECT FOUND_ROWS()', $this );
     591
     592            $this->found_sites = (int) $wpdb->get_var( $found_sites_query );
     593            $this->max_num_pages = ceil( $this->found_sites / $this->query_vars['number'] );
     594        }
    572595    }
    573596
Note: See TracChangeset for help on using the changeset viewer.