Skip to content

Commit

Permalink
Improve performance of is_installed check (#374)
Browse files Browse the repository at this point in the history
* Use information_schema for table existence check to improve performance
* Also cache `false` is_installed value for improved performance when not active

---------

Co-authored-by: Caleb Burks <[email protected]>
  • Loading branch information
noahtallen and WPprodigy authored Feb 7, 2024
1 parent f5e9e00 commit 0a9d13d
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions includes/class-events-store.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ public static function is_installed(): bool {

// Can't rely on the DB_VERSION_OPTION here due to subsite copy/paste scenarios.
// Must truly check that the table is installed.
if ( wp_cache_get( 'is_installed', 'cron-control' ) ) {
return true;
$is_installed = wp_cache_get( 'is_installed', 'cron-control', false, $cache_exists );
if ( $cache_exists ) {
return $is_installed;
}

$table_name = $wpdb->prefix . self::TABLE_SUFFIX;
$is_installed = 1 === count( $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ) ) );
$is_installed = 1 === count( $wpdb->get_col( $wpdb->prepare( 'SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_NAME = %s', $table_name ) ) );

if ( $is_installed ) {
wp_cache_set( 'is_installed', true, 'cron-control' );
}
// Cache the results, will be overridden by _prepare_table() during installation.
wp_cache_add( 'is_installed', $is_installed, 'cron-control' );

return $is_installed;
}
Expand Down Expand Up @@ -147,10 +147,10 @@ protected function _prepare_table() {
dbDelta( $schema, true );

// Confirm that the table was created, and set the option to prevent further updates.
$table_count = count( $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ) ) );
$is_installed = 1 === count( $wpdb->get_col( $wpdb->prepare( 'SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_NAME = %s', $table_name ) ) );
wp_cache_set( 'is_installed', $is_installed, 'cron-control' );

if ( 1 === $table_count ) {
wp_cache_set( 'is_installed', true, 'cron-control' );
if ( $is_installed ) {
update_option( self::DB_VERSION_OPTION, self::DB_VERSION );
}

Expand Down

0 comments on commit 0a9d13d

Please sign in to comment.