From 3f155c9a6300e6c0730407e6e1b3aa8eb1070bcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9stor=20Soriano?= Date: Fri, 7 Oct 2022 09:16:24 +0200 Subject: [PATCH] [COT] Add the orders cache (#34396) * Reverse the order of "$id" and "$object" in ObjectCache::set * Add the ObjectCache::update_if_cached method * Modify ObjectCache:set to validate object before invoking get_id * Add a temporary TransientsEngine class. This is temporary! Must be removed before merging to trunk. * Add the OrderCache class. This class uses a TransientsEngine instance as the caching engine. This is temporary and must be undone (get_cache_engine_instance method must be removed) before merging to trunk! * Use the new OrdersCache class - When an order is retrieved, cache it - When an order is saved, update it if it was cached already - When an order is trashed or deleted, remove it from cache - When the authoritative table for orders changes, flush the cache * Remove the hardcoded usage of TransientEngine in OrderCache It will make things easier later before merging. The transients engine can still be used via the wc_object_cache_get_engine hook. * Add changelog file * Fix failing unit test The test was failing because the order is cached by reference when being saved in the test, and then when being deleted by the REST API code it gets its id set to 0. * Add a setting to enable/disable the orders cache Also added a mechanism to temporarily disable the orders cache while syncrhonization is in progress. * Adjustments in the mechanism to temporarily disable the orders cache usage * OrderCacheController: backup enable option is now stored in memory. * Convert conditions to Yoda :-( * Add missing $ * Use the new features engine to declare the cache as an experimental feature Also decouple the orders cache mechanism from the COT feature, it can be now used indepently of the COT feature and independently of whether the new orders table is in use or not. * Removed unused import and transient class. Co-authored-by: Vedanshu Jain --- plugins/woocommerce/changelog/add-order_cache | 4 + .../includes/abstracts/abstract-wc-order.php | 7 + .../admin/class-wc-admin-settings.php | 3 + .../includes/class-wc-order-factory.php | 46 +++++- plugins/woocommerce/src/Caches/OrderCache.php | 44 ++++++ .../src/Caches/OrderCacheController.php | 121 ++++++++++++++++ .../woocommerce/src/Caching/ObjectCache.php | 74 ++++++++-- .../Orders/CustomOrdersTableController.php | 49 ++++++- .../DataStores/Orders/DataSynchronizer.php | 31 ++++- .../COTMigrationServiceProvider.php | 3 +- .../OrdersDataStoreServiceProvider.php | 19 ++- .../Internal/Features/FeaturesController.php | 10 +- .../woocommerce/src/Utilities/OrderUtil.php | 11 ++ .../class-wc-rest-orders-controller-tests.php | 7 +- .../tests/php/src/Caching/ObjectCacheTest.php | 131 ++++++++++++++---- 15 files changed, 503 insertions(+), 57 deletions(-) create mode 100644 plugins/woocommerce/changelog/add-order_cache create mode 100644 plugins/woocommerce/src/Caches/OrderCache.php create mode 100644 plugins/woocommerce/src/Caches/OrderCacheController.php diff --git a/plugins/woocommerce/changelog/add-order_cache b/plugins/woocommerce/changelog/add-order_cache new file mode 100644 index 00000000000..1b800aff214 --- /dev/null +++ b/plugins/woocommerce/changelog/add-order_cache @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Add a cache for orders, to use when custom order tables are enabled diff --git a/plugins/woocommerce/includes/abstracts/abstract-wc-order.php b/plugins/woocommerce/includes/abstracts/abstract-wc-order.php index 74069a01f2d..cc899b5bb8a 100644 --- a/plugins/woocommerce/includes/abstracts/abstract-wc-order.php +++ b/plugins/woocommerce/includes/abstracts/abstract-wc-order.php @@ -10,9 +10,11 @@ * @package WooCommerce\Classes */ +use Automattic\WooCommerce\Caches\OrderCache; use Automattic\WooCommerce\Proxies\LegacyProxy; use Automattic\WooCommerce\Utilities\ArrayUtil; use Automattic\WooCommerce\Utilities\NumberUtil; +use Automattic\WooCommerce\Utilities\OrderUtil; defined( 'ABSPATH' ) || exit; @@ -203,6 +205,11 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { $this->save_items(); + if ( OrderUtil::orders_cache_usage_is_enabled() ) { + $order_cache = wc_get_container()->get( OrderCache::class ); + $order_cache->update_if_cached( $this ); + } + /** * Trigger action after saving to the DB. * diff --git a/plugins/woocommerce/includes/admin/class-wc-admin-settings.php b/plugins/woocommerce/includes/admin/class-wc-admin-settings.php index 329a6a0d79e..5004f51c9c3 100644 --- a/plugins/woocommerce/includes/admin/class-wc-admin-settings.php +++ b/plugins/woocommerce/includes/admin/class-wc-admin-settings.php @@ -488,6 +488,8 @@ if ( ! class_exists( 'WC_Admin_Settings', false ) ) : $visibility_class[] = 'show_options_if_checked'; } + $must_disable = ArrayUtil::get_value_or_default( $value, 'disabled', false ); + if ( ! isset( $value['checkboxgroup'] ) || 'start' === $value['checkboxgroup'] ) { ?> @@ -510,6 +512,7 @@ if ( ! class_exists( 'WC_Admin_Settings', false ) ) : ?>