-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathObjectCache_WpObjectCache.php
394 lines (353 loc) · 10.2 KB
/
ObjectCache_WpObjectCache.php
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
/**
* File: ObjectCache_WpObjectCache.php
*
* @package W3TC
*
* phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore
* phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
*/
namespace W3TC;
/**
* W3 Object Cache object
*/
class ObjectCache_WpObjectCache {
/**
* Config
*
* @var object|null
*/
private $_config = null;
/**
* Default cache
*
* @var object
*/
private $_default_cache;
/**
* Caches
*
* @var array
*/
private $_caches = array();
/**
* Cache by group
*
* @var array
*/
private $_cache_by_group = array();
/**
* Supported features
*
* @var array
*/
private $supported_features = array(
'flush_runtime',
'flush_group',
'add_multiple',
'set_multiple',
'get_multiple',
'delete_multiple',
'incr',
'decr',
'groups',
'global_groups',
'non_persistent',
'persistent',
);
/**
* Constructor for the ObjectCache_WpObjectCache class.
*
* @return void
*/
public function __construct() {
$this->_config = Dispatcher::config();
$this->_default_cache = Dispatcher::component( 'ObjectCache_WpObjectCache_Regular' );
$this->_caches[] = $this->_default_cache;
}
/**
* Registers a new cache engine to be used for object groups.
*
* @param object $cache The cache engine to register.
* @param array $use_for_object_groups Array of object groups this cache should be used for.
*
* @return void
*/
public function register_cache( $cache, $use_for_object_groups ) {
$this->_caches[] = $cache;
foreach ( $use_for_object_groups as $group ) {
$this->_cache_by_group[ $group ] = $cache;
}
}
/**
* Retrieves a cached value by its ID from the specified group.
*
* @param string $id The cache key.
* @param string $group The cache group.
* @param bool $force Whether to force a cache retrieval, bypassing the cache expiration.
* @param mixed $found A reference to the variable that will store whether the value was found.
*
* @return mixed The cached value if found, otherwise false.
*/
public function get( $id, $group = 'default', $force = false, &$found = null ) {
$cache = $this->_get_engine( $group );
return $cache->get( $id, $group, $force, $found );
}
/**
* Retrieves multiple cached values by their IDs from the specified group.
*
* @since 2.2.8
*
* @param array $ids The cache keys.
* @param string $group The cache group.
* @param bool $force Whether to force a cache retrieval, bypassing the cache expiration.
*
* @return array An array of cached values, indexed by their respective IDs.
*/
public function get_multiple( $ids, $group = 'default', $force = false ) {
$cache = $this->_get_engine( $group );
return $cache->get_multiple( $ids, $group, $force );
}
/**
* Sets a cache value for a given ID and group.
*
* @param string $id The cache key.
* @param mixed $data The data to store in the cache.
* @param string $group The cache group.
* @param int $expire The cache expiration time in seconds.
*
* @return bool True if the data was successfully cached, otherwise false.
*/
public function set( $id, $data, $group = 'default', $expire = 0 ) {
$cache = $this->_get_engine( $group );
return $cache->set( $id, $data, $group, $expire );
}
/**
* Sets multiple cache values for their respective IDs and groups.
*
* @since 2.2.8
*
* @param array $data An array of data indexed by cache key.
* @param string $group The cache group.
* @param int $expire The cache expiration time in seconds.
*
* @return bool True if the data was successfully cached, otherwise false.
*/
public function set_multiple( $data, $group = 'default', $expire = 0 ) {
$cache = $this->_get_engine( $group );
return $cache->set_multiple( $data, $group, $expire );
}
/**
* Deletes a cached value by its ID from the specified group.
*
* @param string $id The cache key.
* @param string $group The cache group.
* @param bool $force Whether to forcefully delete the cache.
*
* @return bool True if the cache was successfully deleted, otherwise false.
*/
public function delete( $id, $group = 'default', $force = false ) {
$cache = $this->_get_engine( $group );
return $cache->delete( $id, $group, $force );
}
/**
* Deletes multiple cached values by their IDs from the specified group.
*
* @since 2.2.8
*
* @param array $keys The cache keys.
* @param string $group The cache group.
*
* @return bool True if the caches were successfully deleted, otherwise false.
*/
public function delete_multiple( $keys, $group = 'default' ) {
$cache = $this->_get_engine( $group );
return $cache->delete_multiple( $keys, $group );
}
/**
* Adds a new value to the cache if it does not already exist for the given ID and group.
*
* @param string $id The cache key.
* @param mixed $data The data to store in the cache.
* @param string $group The cache group.
* @param int $expire The cache expiration time in seconds.
*
* @return bool True if the data was added, otherwise false.
*/
public function add( $id, $data, $group = 'default', $expire = 0 ) {
$cache = $this->_get_engine( $group );
return $cache->add( $id, $data, $group, $expire );
}
/**
* Adds multiple new values to the cache, ensuring they do not overwrite existing data.
*
* @since 2.2.8
*
* @param array $data An array of data indexed by cache key.
* @param string $group The cache group.
* @param int $expire The cache expiration time in seconds.
*
* @return bool True if the data was successfully added, otherwise false.
*/
public function add_multiple( array $data, $group = '', $expire = 0 ) {
$cache = $this->_get_engine( $group );
return $cache->add_multiple( $data, $group, $expire );
}
/**
* Replaces a cache value for the given ID and group.
*
* @param string $id The cache key.
* @param mixed $data The data to store in the cache.
* @param string $group The cache group.
* @param int $expire The cache expiration time in seconds.
*
* @return bool True if the data was successfully replaced, otherwise false.
*/
public function replace( $id, $data, $group = 'default', $expire = 0 ) {
$cache = $this->_get_engine( $group );
return $cache->replace( $id, $data, $group, $expire );
}
/**
* Resets the cache, clearing all stored data.
*
* @return bool True if the cache was successfully reset, otherwise false.
*/
public function reset() {
$result = true;
foreach ( $this->_caches as $engine ) {
$result = $result && $engine->reset();
}
return $result;
}
/**
* Flushes all cached data across all cache engines.
*
* @return bool True if the cache was successfully flushed, otherwise false.
*/
public function flush() {
$result = true;
foreach ( $this->_caches as $engine ) {
$result = $result && $engine->flush();
}
return $result;
}
/**
* Flushes the cached data for a specific group.
*
* @param string $group The cache group.
*
* @return bool True if the cache for the group was successfully flushed, otherwise false.
*/
public function flush_group( $group ) {
$result = true;
foreach ( $this->_caches as $engine ) {
$result = $result && $engine->flush_group( $group );
}
return $result;
}
/**
* Flushes runtime cache data that is temporary and non-persistent.
*
* @return bool True if the runtime cache was successfully flushed, otherwise false.
*/
public function flush_runtime() {
$result = true;
foreach ( $this->_caches as $engine ) {
$result = $result && $engine->flush_runtime();
}
return $result;
}
/**
* Checks if a given cache feature is supported.
*
* @param string $feature The feature to check.
*
* @return bool True if the feature is supported, otherwise false.
*/
public function supports( string $feature ) {
return in_array( $feature, $this->supported_features, true );
}
/**
* Adds global cache groups to the cache engine.
*
* @param mixed $groups An array or string of cache groups to add as global.
*
* @return void
*/
public function add_global_groups( $groups ) {
if ( ! is_array( $groups ) ) {
$groups = array( $groups );
}
foreach ( $groups as $group ) {
$cache = $this->_get_engine( $group );
$cache->add_global_groups( array( $group ) );
}
}
/**
* Adds non-persistent cache groups to the cache engine.
*
* @param mixed $groups An array or string of cache groups to add as non-persistent.
*
* @return void
*/
public function add_nonpersistent_groups( $groups ) {
if ( ! is_array( $groups ) ) {
$groups = array( $groups );
}
foreach ( $groups as $group ) {
$cache = $this->_get_engine( $group );
$cache->add_nonpersistent_groups( array( $group ) );
}
}
/**
* Retrieves the appropriate cache engine based on the group.
*
* @param string $group The cache group.
*
* @return object The cache engine for the specified group.
*/
private function _get_engine( $group = '' ) {
if ( isset( $this->_cache_by_group[ $group ] ) ) {
return $this->_cache_by_group[ $group ];
}
return $this->_default_cache;
}
/**
* Decreases the cached value of a given ID by a specified offset.
*
* @param string $id The cache key.
* @param int $offset The value to decrease by.
* @param string $group The cache group.
*
* @return mixed The updated value if successful, otherwise false.
*/
public function decr( $id, $offset = 1, $group = 'default' ) {
$cache = $this->_get_engine( $group );
return $cache->decr( $id, $offset, $group );
}
/**
* Increases the cached value of a given ID by a specified offset.
*
* @param string $id The cache key.
* @param int $offset The value to increase by.
* @param string $group The cache group.
*
* @return mixed The updated value if successful, otherwise false.
*/
public function incr( $id, $offset = 1, $group = 'default' ) {
$cache = $this->_get_engine( $group );
return $cache->incr( $id, $offset, $group );
}
/**
* Switches to a different blog context in a multisite environment.
*
* @param int $blog_id The blog ID to switch to.
*
* @return void
*/
public function switch_to_blog( $blog_id ) {
foreach ( $this->_caches as $cache ) {
$cache->switch_blog( $blog_id );
}
}
}