From eb2ce329f13ca4365776cbbdb520d65a95d3d4ad Mon Sep 17 00:00:00 2001 From: Jeremy Felt Date: Sun, 26 Jun 2016 12:45:29 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@37868 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-site-query.php | 69 ++++++++++++++++--------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/src/wp-includes/class-wp-site-query.php b/src/wp-includes/class-wp-site-query.php index 12fa1df39c..b75d0cd6e8 100644 --- a/src/wp-includes/class-wp-site-query.php +++ b/src/wp-includes/class-wp-site-query.php @@ -231,13 +231,9 @@ class WP_Site_Query { * @since 4.6.0 * @access public * - * @global wpdb $wpdb WordPress database abstraction object. - * * @return array|int List of sites, or number of sites when 'count' is passed as a query var. */ public function get_sites() { - global $wpdb; - $this->parse_query(); /** @@ -256,12 +252,26 @@ class WP_Site_Query { $last_changed = microtime(); wp_cache_set( 'last_changed', $last_changed, 'sites' ); } - $cache_key = "get_site_ids:$key:$last_changed"; - $site_ids = wp_cache_get( $cache_key, 'sites' ); - if ( false === $site_ids ) { + $cache_key = "get_sites:$key:$last_changed"; + $cache_value = wp_cache_get( $cache_key, 'sites' ); + + if ( false === $cache_value ) { $site_ids = $this->get_site_ids(); - wp_cache_add( $cache_key, $site_ids, 'sites' ); + if ( $site_ids ) { + $this->set_found_sites(); + } + + $cache_value = array( + 'site_ids' => $site_ids, + 'found_sites' => $this->found_sites, + 'max_num_pages' => $this->max_num_pages, + ); + wp_cache_add( $cache_key, $cache_value, 'sites' ); + } else { + $site_ids = $cache_value['site_ids']; + $this->found_sites = $cache_value['found_sites']; + $this->max_num_pages = $cache_value['max_num_pages']; } // If querying for a count only, there's nothing more to do. @@ -274,21 +284,6 @@ class WP_Site_Query { $this->site_count = count( $this->sites ); - if ( $site_ids && $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) { - /** - * Filters the query used to retrieve found site count. - * - * @since 4.6.0 - * - * @param string $found_sites_query SQL query. Default 'SELECT FOUND_ROWS()'. - * @param WP_Site_Query $site_query The `WP_Site_Query` instance. - */ - $found_sites_query = apply_filters( 'found_sites_query', 'SELECT FOUND_ROWS()', $this ); - - $this->found_sites = (int) $wpdb->get_var( $found_sites_query ); - $this->max_num_pages = ceil( $this->found_sites / $this->query_vars['number'] ); - } - if ( 'ids' == $this->query_vars['fields'] ) { $this->sites = $site_ids; @@ -571,6 +566,34 @@ class WP_Site_Query { return array_map( 'intval', $site_ids ); } + /** + * Populates found_sites and max_num_pages properties for the current query + * if the limit clause was used. + * + * @since 4.6.0 + * @access private + * + * @global wpdb $wpdb WordPress database abstraction object. + */ + private function set_found_sites() { + global $wpdb; + + if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) { + /** + * Filters the query used to retrieve found site count. + * + * @since 4.6.0 + * + * @param string $found_sites_query SQL query. Default 'SELECT FOUND_ROWS()'. + * @param WP_Site_Query $site_query The `WP_Site_Query` instance. + */ + $found_sites_query = apply_filters( 'found_sites_query', 'SELECT FOUND_ROWS()', $this ); + + $this->found_sites = (int) $wpdb->get_var( $found_sites_query ); + $this->max_num_pages = ceil( $this->found_sites / $this->query_vars['number'] ); + } + } + /** * Used internally to generate an SQL string for searching across multiple columns. *