Revert change to defer transient cleanup

This commit reverts commits 2f8a3eae49 and 17e97c2580 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.
This commit is contained in:
Rodrigo Primo 2018-09-05 11:46:38 -03:00 committed by Mike Jolley
parent 08d7e319b6
commit eea4810c49
1 changed files with 5 additions and 22 deletions

View File

@ -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 ) );
}
}
}