From 90c424f73b620f11b32f508cd9c62ac15ca69a4d Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 29 Aug 2019 12:42:56 +0000 Subject: [PATCH] Multisite: Improve performance by caching not found lookups for sites and networks. With this change, the result of a site or network lookup by ID will be cached even if the ID does not exist. When a new site or network is created, the cache for the respective new ID is cleared. Props mnelson4, nielsdeblaauw. Fixes #42251. Built from https://develop.svn.wordpress.org/trunk@45910 git-svn-id: http://core.svn.wordpress.org/trunk@45721 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/schema.php | 6 ++++++ wp-includes/class-wp-network.php | 14 +++++++++----- wp-includes/class-wp-site.php | 8 ++++++-- wp-includes/ms-site.php | 4 ++-- wp-includes/version.php | 2 +- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/wp-admin/includes/schema.php b/wp-admin/includes/schema.php index 92fd6d6619..1d1cbcbc2f 100644 --- a/wp-admin/includes/schema.php +++ b/wp-admin/includes/schema.php @@ -1159,6 +1159,12 @@ function populate_network_meta( $network_id, array $meta = array() ) { } } + if ( function_exists( 'clean_network_cache' ) ) { + clean_network_cache( $network_id ); + } else { + wp_cache_delete( $network_id, 'networks' ); + } + wp_cache_delete( 'networks_have_paths', 'site-options' ); if ( ! is_multisite() ) { diff --git a/wp-includes/class-wp-network.php b/wp-includes/class-wp-network.php index 4926c0d8b5..849dfc1271 100644 --- a/wp-includes/class-wp-network.php +++ b/wp-includes/class-wp-network.php @@ -101,16 +101,20 @@ class WP_Network { $_network = wp_cache_get( $network_id, 'networks' ); - if ( ! $_network ) { + if ( false === $_network ) { $_network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->site} WHERE id = %d LIMIT 1", $network_id ) ); if ( empty( $_network ) || is_wp_error( $_network ) ) { - return false; + $_network = -1; } wp_cache_add( $network_id, $_network, 'networks' ); } + if ( is_numeric( $_network ) ) { + return false; + } + return new WP_Network( $_network ); } @@ -231,8 +235,8 @@ class WP_Network { return (int) $this->blog_id; } - if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && $this->domain === DOMAIN_CURRENT_SITE && $this->path === PATH_CURRENT_SITE ) - || ( defined( 'SITE_ID_CURRENT_SITE' ) && $this->id == SITE_ID_CURRENT_SITE ) ) { + if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && DOMAIN_CURRENT_SITE === $this->domain && PATH_CURRENT_SITE === $this->path ) + || ( defined( 'SITE_ID_CURRENT_SITE' ) && SITE_ID_CURRENT_SITE == $this->id ) ) { if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) { $this->blog_id = (string) BLOG_ID_CURRENT_SITE; @@ -457,7 +461,7 @@ class WP_Network { break; } } - if ( $network->path === '/' ) { + if ( '/' === $network->path ) { $found = true; break; } diff --git a/wp-includes/class-wp-site.php b/wp-includes/class-wp-site.php index b0459be2d4..e1839fed9e 100644 --- a/wp-includes/class-wp-site.php +++ b/wp-includes/class-wp-site.php @@ -162,16 +162,20 @@ final class WP_Site { $_site = wp_cache_get( $site_id, 'sites' ); - if ( ! $_site ) { + if ( false === $_site ) { $_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d LIMIT 1", $site_id ) ); if ( empty( $_site ) || is_wp_error( $_site ) ) { - return false; + $_site = -1; } wp_cache_add( $site_id, $_site, 'sites' ); } + if ( is_numeric( $_site ) ) { + return false; + } + return new WP_Site( $_site ); } diff --git a/wp-includes/ms-site.php b/wp-includes/ms-site.php index 9526973510..039eb488d9 100644 --- a/wp-includes/ms-site.php +++ b/wp-includes/ms-site.php @@ -69,14 +69,14 @@ function wp_insert_site( array $data ) { return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error ); } + clean_blog_cache( $wpdb->insert_id ); + $new_site = get_site( $wpdb->insert_id ); if ( ! $new_site ) { return new WP_Error( 'get_site_error', __( 'Could not retrieve site data.' ) ); } - clean_blog_cache( $new_site ); - /** * Fires once a site has been inserted into the database. * diff --git a/wp-includes/version.php b/wp-includes/version.php index cb4882a584..62c71860d8 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.3-alpha-45909'; +$wp_version = '5.3-alpha-45910'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.