Skip to content

Commit

Permalink
Multisite: Improve performance by caching not found lookups for sites…
Browse files Browse the repository at this point in the history
… 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.


git-svn-id: https://develop.svn.wordpress.org/trunk@45910 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
felixarntz committed Aug 29, 2019
1 parent 65f3124 commit 4572bb0
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/wp-admin/includes/schema.php
Expand Up @@ -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' ); wp_cache_delete( 'networks_have_paths', 'site-options' );


if ( ! is_multisite() ) { if ( ! is_multisite() ) {
Expand Down
14 changes: 9 additions & 5 deletions src/wp-includes/class-wp-network.php
Expand Up @@ -101,16 +101,20 @@ public static function get_instance( $network_id ) {


$_network = wp_cache_get( $network_id, 'networks' ); $_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 ) ); $_network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->site} WHERE id = %d LIMIT 1", $network_id ) );


if ( empty( $_network ) || is_wp_error( $_network ) ) { if ( empty( $_network ) || is_wp_error( $_network ) ) {
return false; $_network = -1;
} }


wp_cache_add( $network_id, $_network, 'networks' ); wp_cache_add( $network_id, $_network, 'networks' );
} }


if ( is_numeric( $_network ) ) {
return false;
}

return new WP_Network( $_network ); return new WP_Network( $_network );
} }


Expand Down Expand Up @@ -231,8 +235,8 @@ private function get_main_site_id() {
return (int) $this->blog_id; return (int) $this->blog_id;
} }


if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && $this->domain === DOMAIN_CURRENT_SITE && $this->path === PATH_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' ) && $this->id == SITE_ID_CURRENT_SITE ) ) { || ( defined( 'SITE_ID_CURRENT_SITE' ) && SITE_ID_CURRENT_SITE == $this->id ) ) {
if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) { if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
$this->blog_id = (string) BLOG_ID_CURRENT_SITE; $this->blog_id = (string) BLOG_ID_CURRENT_SITE;


Expand Down Expand Up @@ -457,7 +461,7 @@ public static function get_by_path( $domain = '', $path = '', $segments = null )
break; break;
} }
} }
if ( $network->path === '/' ) { if ( '/' === $network->path ) {
$found = true; $found = true;
break; break;
} }
Expand Down
8 changes: 6 additions & 2 deletions src/wp-includes/class-wp-site.php
Expand Up @@ -162,16 +162,20 @@ public static function get_instance( $site_id ) {


$_site = wp_cache_get( $site_id, 'sites' ); $_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 ) ); $_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 ) ) { if ( empty( $_site ) || is_wp_error( $_site ) ) {
return false; $_site = -1;
} }


wp_cache_add( $site_id, $_site, 'sites' ); wp_cache_add( $site_id, $_site, 'sites' );
} }


if ( is_numeric( $_site ) ) {
return false;
}

return new WP_Site( $_site ); return new WP_Site( $_site );
} }


Expand Down
4 changes: 2 additions & 2 deletions src/wp-includes/ms-site.php
Expand Up @@ -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 ); 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 ); $new_site = get_site( $wpdb->insert_id );


if ( ! $new_site ) { if ( ! $new_site ) {
return new WP_Error( 'get_site_error', __( 'Could not retrieve site data.' ) ); 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. * Fires once a site has been inserted into the database.
* *
Expand Down
43 changes: 43 additions & 0 deletions tests/phpunit/tests/multisite/network.php
Expand Up @@ -613,6 +613,49 @@ public function test_network_blog_id_set() {


$this->assertSame( (string) self::$different_site_ids[0], $network->blog_id ); $this->assertSame( (string) self::$different_site_ids[0], $network->blog_id );
} }

/**
* @ticket 42251
*/
public function test_get_network_not_found_cache() {
global $wpdb;

$new_network_id = $this->_get_next_network_id();
$this->assertNull( get_network( $new_network_id ) );

$num_queries = $wpdb->num_queries;
$this->assertNull( get_network( $new_network_id ) );
$this->assertSame( $num_queries, $wpdb->num_queries );
}

/**
* @ticket 42251
*/
public function test_get_network_not_found_cache_clear() {
$new_network_id = $this->_get_next_network_id();
$this->assertNull( get_network( $new_network_id ) );

$new_network = $this->factory()->network->create_and_get();

// Double-check we got the ID of the new network correct.
$this->assertEquals( $new_network_id, $new_network->id );

// Verify that if we fetch the network now, it's no longer false.
$fetched_network = get_network( $new_network_id );
$this->assertInstanceOf( 'WP_Network', $fetched_network );
$this->assertEquals( $new_network_id, $fetched_network->id );
}

/**
* Gets the ID of the site with the highest ID
* @return int
*/
protected function _get_next_network_id() {
global $wpdb;
//create an extra network, just to make sure we know the ID of the following one
static::factory()->network->create();
return (int) $wpdb->get_var( 'SELECT id FROM ' . $wpdb->site . ' ORDER BY id DESC LIMIT 1' ) + 1;
}
} }


endif; endif;
45 changes: 45 additions & 0 deletions tests/phpunit/tests/multisite/site.php
Expand Up @@ -2383,6 +2383,51 @@ public function test_wpmu_new_blog_action_backward_compatible( $meta, $expected_
$this->wp_initialize_site_meta = array(); $this->wp_initialize_site_meta = array();
} }


/**
* @ticket 42251
*/
public function test_get_site_not_found_cache() {
global $wpdb;

$new_site_id = $this->_get_next_site_id();
$this->assertNull( get_site( $new_site_id ) );

$num_queries = $wpdb->num_queries;
$this->assertNull( get_site( $new_site_id ) );
$this->assertSame( $num_queries, $wpdb->num_queries );
}

/**
* @ticket 42251
*/
public function test_get_site_not_found_cache_clear() {
$new_site_id = $this->_get_next_site_id();
$this->assertNull( get_site( $new_site_id ) );

$new_site = $this->factory()->blog->create_and_get();

// Double-check we got the ID of the new site correct.
$this->assertEquals( $new_site_id, $new_site->blog_id );

// Verify that if we fetch the site now, it's no longer false.
$fetched_site = get_site( $new_site_id );
$this->assertInstanceOf( 'WP_Site', $fetched_site );
$this->assertEquals( $new_site_id, $fetched_site->blog_id );

}

/**
* Gets the ID of the next site that will get inserted
* @return int
*/
protected function _get_next_site_id() {
global $wpdb;
//create an entry
static::factory()->blog->create();
//get the ID after it
return (int) $wpdb->get_var( 'SELECT blog_id FROM ' . $wpdb->blogs . ' ORDER BY blog_ID DESC LIMIT 1' ) + 1;
}

/** /**
* Capture the $meta value passed to the wpmu_new_blog action and compare it. * Capture the $meta value passed to the wpmu_new_blog action and compare it.
*/ */
Expand Down

0 comments on commit 4572bb0

Please sign in to comment.