From 11002a2a000b31f7d6b018fec094345c4e928d39 Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Wed, 22 Dec 2021 10:52:27 -0700 Subject: [PATCH 1/2] Clear out cache instead of rebuilding it when site data changes --- .../NetworkSiteConnection.php | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/includes/classes/InternalConnections/NetworkSiteConnection.php b/includes/classes/InternalConnections/NetworkSiteConnection.php index 5ee228266..3fc7cecda 100644 --- a/includes/classes/InternalConnections/NetworkSiteConnection.php +++ b/includes/classes/InternalConnections/NetworkSiteConnection.php @@ -581,8 +581,8 @@ public static function bootstrap() { add_action( 'untrash_post', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'connect_syndicated_on_untrash' ) ); add_action( 'clean_site_cache', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'set_sites_last_changed_time' ) ); add_action( 'wp_insert_site', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'set_sites_last_changed_time' ) ); - add_action( 'add_user_to_blog', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'rebuild_user_authorized_sites_cache' ) ); - add_action( 'remove_user_from_blog', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'rebuild_user_authorized_sites_cache' ) ); + add_action( 'add_user_to_blog', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'clear_authorized_sites_cache' ) ); + add_action( 'remove_user_from_blog', array( '\Distributor\InternalConnections\NetworkSiteConnection', 'clear_authorized_sites_cache' ) ); } /** @@ -822,8 +822,7 @@ public static function build_available_authorized_sites( $user_id = false, $cont $last_changed = get_site_option( 'last_changed_sites' ); if ( ! $last_changed ) { - $last_changed = microtime(); - self::set_sites_last_changed_time(); + $last_changed = self::set_sites_last_changed_time(); } $cache_key = "authorized_sites:$user_id:$context:$last_changed"; @@ -890,22 +889,29 @@ public static function build_available_authorized_sites( $user_id = false, $cont * will have caching enabled, so we also store it * in a site option. * - * @return void + * @return string */ public static function set_sites_last_changed_time() { - update_site_option( 'last_changed_sites', microtime() ); + $time = microtime(); + update_site_option( 'last_changed_sites', $time ); + + return $time; } /** - * Rebuild the authorized sites cache for a specific user. + * Clear the authorized sites cache for a specific user. * * @param int $user_id Current user ID. - * - * @return void */ - public static function rebuild_user_authorized_sites_cache( $user_id ) { - self::build_available_authorized_sites( $user_id, 'push', true ); - self::build_available_authorized_sites( $user_id, 'pull', true ); + public static function clear_authorized_sites_cache( $user_id = false ) { + $last_changed = get_site_option( 'last_changed_sites' ); + + if ( ! $last_changed ) { + $last_changed = self::set_sites_last_changed_time(); + } + + delete_transient( "authorized_sites:$user_id:push:$last_changed" ); + delete_transient( "authorized_sites:$user_id:pull:$last_changed" ); } /** From d0312635e9234b0aaba4b763a65406a920744443 Mon Sep 17 00:00:00 2001 From: Darin Kotter Date: Fri, 14 Jan 2022 13:04:40 -0700 Subject: [PATCH 2/2] Only delete a transient if we have a proper last_changed_sites value. Otherwise we either don't have a proper transient to begin with or the transient will be invalid due to updating the last_changed_sites value, so no need to delete the transient --- .../classes/InternalConnections/NetworkSiteConnection.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/classes/InternalConnections/NetworkSiteConnection.php b/includes/classes/InternalConnections/NetworkSiteConnection.php index 3fc7cecda..56d83230a 100644 --- a/includes/classes/InternalConnections/NetworkSiteConnection.php +++ b/includes/classes/InternalConnections/NetworkSiteConnection.php @@ -907,11 +907,11 @@ public static function clear_authorized_sites_cache( $user_id = false ) { $last_changed = get_site_option( 'last_changed_sites' ); if ( ! $last_changed ) { - $last_changed = self::set_sites_last_changed_time(); + self::set_sites_last_changed_time(); + } else { + delete_transient( "authorized_sites:$user_id:push:$last_changed" ); + delete_transient( "authorized_sites:$user_id:pull:$last_changed" ); } - - delete_transient( "authorized_sites:$user_id:push:$last_changed" ); - delete_transient( "authorized_sites:$user_id:pull:$last_changed" ); } /**