From f97c60dc08520903328288761e19803fba23ef4c Mon Sep 17 00:00:00 2001 From: Nicolas Lemoine Date: Mon, 23 Jun 2025 09:32:45 +0200 Subject: [PATCH] feat: Improve `stop_the_insanity` Makes resetting the object cache compatible with more implementations. See: https://github.com/alleyinteractive/wp-bulk-task/blob/12b832863bbd8379a6b15ba13c9fc51a8d37e9aa/src/class-bulk-task.php#L85-L121 --- inc/class-revisions-cli.php | 38 +++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/inc/class-revisions-cli.php b/inc/class-revisions-cli.php index f748152..c430373 100644 --- a/inc/class-revisions-cli.php +++ b/inc/class-revisions-cli.php @@ -500,21 +500,39 @@ private function supports_revisions() { * Clear all of the caches for memory management */ protected function stop_the_insanity() { - global $wpdb, $wp_object_cache; + global $wp_object_cache; - $wpdb->queries = array(); + // Reset query cache. + if ( function_exists( 'vip_reset_db_query_log' ) ) { + vip_reset_db_query_log(); + } else { + global $wpdb; - if ( ! is_object( $wp_object_cache ) ) { - return; + $wpdb->queries = []; } - $wp_object_cache->group_ops = array(); - $wp_object_cache->stats = array(); - $wp_object_cache->memcache_debug = array(); - $wp_object_cache->cache = array(); + // Reset object cache. + if ( function_exists( 'vip_reset_local_object_cache' ) ) { + vip_reset_local_object_cache(); + } elseif ( $wp_object_cache instanceof \RedisCachePro\ObjectCaches\ObjectCacheInterface && method_exists( $wp_object_cache, 'flush_runtime' ) ) { // @phpstan-ignore-line + $wp_object_cache->flush_runtime(); // @phpstan-ignore-line + } elseif ( is_object( $wp_object_cache ) ) { - if ( is_callable( $wp_object_cache, '__remoteset' ) ) { - $wp_object_cache->__remoteset(); // important + if ( isset( $wp_object_cache->group_ops ) ) { + $wp_object_cache->group_ops = []; + } + + if ( isset( $wp_object_cache->memcache_debug ) ) { + $wp_object_cache->memcache_debug = []; + } + + if ( isset( $wp_object_cache->cache ) ) { + $wp_object_cache->cache = []; + } + + if ( method_exists( $wp_object_cache, '__remoteset' ) ) { + $wp_object_cache->__remoteset(); + } } }