Categories
4 weeks ago
Coupons
4 weeks ago
Customers
2 months ago
Downloads
1 year ago
Export
3 years ago
Import
3 years ago
Orders
4 weeks ago
PerformanceIndicators
9 months ago
Products
4 weeks ago
Revenue
3 months ago
Stock
4 months ago
Taxes
4 weeks ago
Variations
4 weeks ago
Cache.php
4 years ago
Controller.php
11 months ago
DataStore.php
4 weeks ago
DataStoreInterface.php
4 years ago
ExportableInterface.php
4 years ago
ExportableTraits.php
4 years ago
FilteredGetDataTrait.php
1 year ago
GenericController.php
1 year ago
GenericQuery.php
1 year ago
GenericStatsController.php
1 year ago
OrderAwareControllerTrait.php
1 year ago
ParameterException.php
4 years ago
Query.php
1 year ago
Segmenter.php
1 year ago
SqlQuery.php
3 years ago
StatsDataStoreTrait.php
1 year ago
TimeInterval.php
2 years ago
Cache.php
78 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Reports Cache. |
| 4 | * |
| 5 | * Handles report data object caching. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\API\Reports; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * REST API Reports Cache class. |
| 14 | */ |
| 15 | class Cache { |
| 16 | /** |
| 17 | * Cache version. Used to invalidate all cached values. |
| 18 | */ |
| 19 | const VERSION_OPTION = 'woocommerce_reports'; |
| 20 | |
| 21 | /** |
| 22 | * Invalidate cache. |
| 23 | */ |
| 24 | public static function invalidate() { |
| 25 | \WC_Cache_Helper::get_transient_version( self::VERSION_OPTION, true ); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Get cache version number. |
| 30 | * |
| 31 | * @return string |
| 32 | */ |
| 33 | public static function get_version() { |
| 34 | $version = \WC_Cache_Helper::get_transient_version( self::VERSION_OPTION ); |
| 35 | |
| 36 | return $version; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get cached value. |
| 41 | * |
| 42 | * @param string $key Cache key. |
| 43 | * @return mixed |
| 44 | */ |
| 45 | public static function get( $key ) { |
| 46 | $transient_version = self::get_version(); |
| 47 | $transient_value = get_transient( $key ); |
| 48 | |
| 49 | if ( |
| 50 | isset( $transient_value['value'], $transient_value['version'] ) && |
| 51 | $transient_value['version'] === $transient_version |
| 52 | ) { |
| 53 | return $transient_value['value']; |
| 54 | } |
| 55 | |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Update cached value. |
| 61 | * |
| 62 | * @param string $key Cache key. |
| 63 | * @param mixed $value New value. |
| 64 | * @return bool |
| 65 | */ |
| 66 | public static function set( $key, $value ) { |
| 67 | $transient_version = self::get_version(); |
| 68 | $transient_value = array( |
| 69 | 'version' => $transient_version, |
| 70 | 'value' => $value, |
| 71 | ); |
| 72 | |
| 73 | $result = set_transient( $key, $transient_value, WEEK_IN_SECONDS ); |
| 74 | |
| 75 | return $result; |
| 76 | } |
| 77 | } |
| 78 |