From eea4810c4905238f6b66f84c9181937b4805f6a8 Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Wed, 5 Sep 2018 11:46:38 -0300 Subject: [PATCH] Revert change to defer transient cleanup This commit reverts commits 2f8a3eae49949c9957d3408213b1339f12019498 and 17e97c2580a17af4fd70633f7fad98ea0d264142 that were created to defer transient cleanup (see #20537) and avoid deadlocks on the wp_options table (see #20528 and #17632). The problem is that deferring transient cleanup to a cron job created an issue when creating or importing multiple products at once (see #21100 and https://github.com/woocommerce/wc-smooth-generator/issues/14#issuecomment-413342136) and has the potential to impact the checkout as well if we start using more versioned transients for orders. This problem is happening because when importing or creating multiple products at once, for each product that is created or imported, WooCommerce core enqueues a few 'delete_version_transients' cron events. Events are enqueued faster than they are executed and after a few hundred products are generated, the size of the cron queue, which is stored in a single wp_options entry, starts to impact WordPress performance in general. To reduce the chance of deadlocks happening again after this change, I already created another PR to optimize the query used to delete transients (#21274) by avoiding an unnecessary filesort, and I'm planning, on a subsequent commit, to improve it further by prefixing the transient name with its version instead of suffixing it as it is currently done. But the ultimate solution for high traffic stores is to use a persistent cache plugin. --- includes/class-wc-cache-helper.php | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/includes/class-wc-cache-helper.php b/includes/class-wc-cache-helper.php index a57b85066ef..5873e0e952b 100644 --- a/includes/class-wc-cache-helper.php +++ b/includes/class-wc-cache-helper.php @@ -134,33 +134,16 @@ class WC_Cache_Helper { public static function get_transient_version( $group, $refresh = false ) { $transient_name = $group . '-transient-version'; $transient_value = get_transient( $transient_name ); - $transient_value = strval( $transient_value ? $transient_value : '' ); - if ( '' === $transient_value || true === $refresh ) { - $old_transient_value = $transient_value; - $transient_value = (string) time(); + if ( false === $transient_value || true === $refresh ) { + self::delete_version_transients( $transient_value ); - if ( $old_transient_value === $transient_value ) { - // Time did not change but transient needs flushing now. - self::delete_version_transients( $transient_value ); - } else { - self::queue_delete_version_transients( $transient_value ); - } + $transient_value = (string) time(); set_transient( $transient_name, $transient_value ); } - return $transient_value; - } - /** - * Queues a cleanup event for version transients. - * - * @param string $version Version of the transient to remove. - */ - protected static function queue_delete_version_transients( $version = '' ) { - if ( ! wp_using_ext_object_cache() && ! empty( $version ) ) { - wp_schedule_single_event( time() + 30, 'delete_version_transients', array( $version ) ); - } + return $transient_value; } /** @@ -185,7 +168,7 @@ class WC_Cache_Helper { // If affected rows is equal to limit, there are more rows to delete. Delete in 30 secs. if ( $affected === $limit ) { - self::queue_delete_version_transients( $version ); + wp_schedule_single_event( time() + 30, 'delete_version_transients', array( $version ) ); } } }