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