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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
<?php
namespace AutomateWoo;
/** * Cache class. * * Wrapper class for WP transients and object cache. * * @since 2.1.0 */ class Cache {
/** * Is cache enabled? * * @var bool */ public static $enabled = true;
/** * Get default transient expiration value in hours. * * @return int */ public static function get_default_transient_expiration() { return apply_filters( 'automatewoo_cache_default_expiration', 6 ); }
/** * Set a transient value. * * @param string $key * @param mixed $value * @param bool|int $expiration In hours. Optional. * * @return bool */ public static function set_transient( $key, $value, $expiration = false ) { if ( ! self::$enabled ) { return false; } if ( ! $expiration ) { $expiration = self::get_default_transient_expiration(); } return set_transient( 'aw_cache_' . $key, $value, $expiration * HOUR_IN_SECONDS ); }
/** * Get the value of a transient. * * @param string $key * * @return bool|mixed */ public static function get_transient( $key ) { if ( ! self::$enabled ) { return false; } return get_transient( 'aw_cache_' . $key ); }
/** * Delete a transient. * * @param string $key */ public static function delete_transient( $key ) { delete_transient( 'aw_cache_' . $key ); }
/** * Sets a value in cache. * * Only sets if key is not falsy. * * @param string $key * @param mixed $value * @param string $group */ public static function set( $key, $value, $group ) { if ( ! $key || ! self::$enabled ) { return; } wp_cache_set( self::prefix_key( $key, $group ), $value, "automatewoo_{$group}" ); }
/** * Retrieves the cache contents from the cache by key and group. * * @param string $key * @param string $group * * @return bool|mixed */ public static function get( $key, $group ) { if ( ! $key || ! self::$enabled ) { return false; } return wp_cache_get( self::prefix_key( $key, $group ), "automatewoo_{$group}" ); }
/** * Checks if a cache key and group value exists. * * @param string $key * @param string $group * * @return bool */ public static function exists( $key, $group ) { if ( ! $key || ! self::$enabled ) { return false; } $found = false; wp_cache_get( self::prefix_key( $key, $group ), "automatewoo_{$group}", false, $found ); return $found; }
/** * Remove the item from the cache. * * @param string $key * @param string $group */ public static function delete( $key, $group ) { if ( ! $key || ! self::$enabled ) { return; } wp_cache_delete( self::prefix_key( $key, $group ), "automatewoo_{$group}" ); }
/** * Flush a cache group. * * @param string $group Group of cache to flush. * * @since 6.1.6 */ public static function flush_group( string $group ) {
// Use fallback if flush group is not supported. if ( ! self::cache_supports_flush_group() ) { self::invalidate_cache_group( "automatewoo_{$group}" ); return; }
wp_cache_flush_group( "automatewoo_{$group}" ); }
/** * Checks if the cache solution supports flush_group. * Result is filtered to allow overriding this value. * * @return boolean */ private static function cache_supports_flush_group(): bool { $supports_flush_group = function_exists( 'wp_cache_supports' ) && wp_cache_supports( 'flush_group' ); return (bool) apply_filters( 'automatewoo_cache_supports_flush_group', $supports_flush_group ); }
/** * Get prefix for use with wp_cache_ functions. Allows all cache in a group to be invalidated at once. * * @param string $group Group of cache to get (already prefixed with `automatewoo_`). * @return string Prefix. */ private static function get_cache_prefix( string $group ): string { // Get cache key - uses cache key aw_<group>_cache_prefix to invalidate when needed. $prefix = wp_cache_get( "{$group}_cache_prefix", $group );
if ( false === $prefix ) { $prefix = microtime(); wp_cache_set( "{$group}_cache_prefix", $prefix, $group ); }
return "automatewoo_{$prefix}_"; }
/** * Invalidate cache group. * * @param string $group Group of cache to clear (already prefixed with `automatewoo_`). * @return bool */ private static function invalidate_cache_group( string $group ): bool { return wp_cache_set( "{$group}_cache_prefix", microtime(), $group ); }
/** * Return a prefixed key. * If flush group is not supported by the cache implementation, it returns a unique prefix. * * @param string $key Cache key to prefix. * @param string $group Cache group to use for unique prefix. * * @return string */ private static function prefix_key( $key, string $group ): string { if ( ! self::cache_supports_flush_group() ) { return self::get_cache_prefix( "automatewoo_{$group}" ) . (string) $key; }
return 'automatewoo_' . (string) $key; } }
|