OrderCache.php
1 year ago
OrderCacheController.php
1 year ago
OrderCountCache.php
4 weeks ago
OrderCountCacheService.php
4 weeks ago
OrderCacheController.php
85 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Caches; |
| 4 | |
| 5 | use Automattic\WooCommerce\Internal\Features\FeaturesController; |
| 6 | use Automattic\WooCommerce\Utilities\OrderUtil; |
| 7 | |
| 8 | /** |
| 9 | * A class to control the usage of the orders cache. |
| 10 | */ |
| 11 | class OrderCacheController { |
| 12 | |
| 13 | /** |
| 14 | * The orders cache to use. |
| 15 | * |
| 16 | * @var OrderCache |
| 17 | */ |
| 18 | private $order_cache; |
| 19 | |
| 20 | /** |
| 21 | * The orders cache to use. |
| 22 | * |
| 23 | * @var FeaturesController |
| 24 | */ |
| 25 | private $features_controller; |
| 26 | |
| 27 | /** |
| 28 | * The backup value of the cache usage enable status, stored while the cache is temporarily disabled. |
| 29 | * |
| 30 | * @var null|bool |
| 31 | */ |
| 32 | private $orders_cache_usage_backup = null; |
| 33 | |
| 34 | /** |
| 35 | * Class initialization, invoked by the DI container. |
| 36 | * |
| 37 | * @internal |
| 38 | * @param OrderCache $order_cache The order cache engine to use. |
| 39 | */ |
| 40 | final public function init( OrderCache $order_cache ) { |
| 41 | $this->order_cache = $order_cache; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Whether order cache usage is enabled. Currently, linked to custom orders' table usage. |
| 46 | * |
| 47 | * @return bool True if the order cache is enabled. |
| 48 | */ |
| 49 | public function orders_cache_usage_is_enabled(): bool { |
| 50 | return OrderUtil::custom_orders_table_usage_is_enabled(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Temporarily disable the order cache if it's enabled. |
| 55 | * |
| 56 | * This is a purely in-memory operation: a variable is created with the value |
| 57 | * of the current enable status for the feature, and this variable |
| 58 | * is checked by orders_cache_usage_is_enabled. In the next request the |
| 59 | * feature will be again enabled or not depending on how the feature is set. |
| 60 | */ |
| 61 | public function temporarily_disable_orders_cache_usage(): void { |
| 62 | if ( $this->orders_cache_usage_is_temporarly_disabled() ) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | $this->orders_cache_usage_backup = $this->orders_cache_usage_is_enabled(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Check if the order cache has been temporarily disabled. |
| 71 | * |
| 72 | * @return bool True if the order cache is currently temporarily disabled. |
| 73 | */ |
| 74 | public function orders_cache_usage_is_temporarly_disabled(): bool { |
| 75 | return null !== $this->orders_cache_usage_backup; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Restore the order cache usage that had been temporarily disabled. |
| 80 | */ |
| 81 | public function maybe_restore_orders_cache_usage(): void { |
| 82 | $this->orders_cache_usage_backup = null; |
| 83 | } |
| 84 | } |
| 85 |