PluginProbe ʕ •ᴥ•ʔ
Asset CleanUp: Page Speed Booster / 1.4.0.2
Asset CleanUp: Page Speed Booster v1.4.0.2
trunk 1.1.4.6 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.4.1 1.2.4.2 1.2.4.3 1.2.4.4 1.2.5 1.2.5.1 1.2.5.2 1.2.5.3 1.2.6 1.2.6.1 1.2.6.2 1.2.6.3 1.2.6.4 1.2.6.5 1.2.6.6 1.2.6.7 1.2.6.8 1.2.6.9 1.2.7 1.2.7.1 1.2.7.2 1.2.7.3 1.2.7.4 1.2.7.5 1.2.7.6 1.2.7.7 1.2.7.8 1.2.7.9 1.2.8 1.2.8.1 1.2.8.2 1.2.8.3 1.2.8.4 1.2.8.5 1.2.8.6 1.2.8.7 1.2.8.8 1.2.8.9 1.2.9 1.2.9.1 1.2.9.2 1.2.9.3 1.2.9.4 1.2.9.5 1.2.9.6 1.2.9.7 1.2.9.8 1.2.9.9 1.3 1.3.1 1.3.2 1.3.2.1 1.3.2.2 1.3.2.3 1.3.2.4 1.3.2.5 1.3.2.6 1.3.2.7 1.3.2.8 1.3.2.9 1.3.3.0 1.3.3.1 1.3.3.2 1.3.3.3 1.3.3.4 1.3.3.5 1.3.3.6 1.3.3.7 1.3.3.8 1.3.3.9 1.3.4.0 1.3.4.1 1.3.4.2 1.3.4.3 1.3.4.4 1.3.4.5 1.3.4.6 1.3.4.7 1.3.4.8 1.3.4.9 1.3.5.0 1.3.5.1 1.3.5.2 1.3.5.3 1.3.5.4 1.3.5.5 1.3.5.6 1.3.5.7 1.3.5.8 1.3.5.9 1.3.6.0 1.3.6.1 1.3.6.2 1.3.6.3 1.3.6.4 1.3.6.5 1.3.6.6 1.3.6.7 1.3.6.8 1.3.6.9 1.3.7.0 1.3.7.1 1.3.7.2 1.3.7.3 1.3.7.4 1.3.7.5 1.3.7.6 1.3.7.7 1.3.7.8 1.3.7.9 1.3.8.0 1.3.8.1 1.3.8.2 1.3.8.2.1 1.3.8.3 1.3.8.4 1.3.8.5 1.3.8.6 1.3.8.7 1.3.8.8 1.3.8.9 1.3.9.0 1.3.9.1 1.3.9.2 1.3.9.3 1.3.9.4 1.3.9.5 1.3.9.6 1.3.9.7 1.3.9.8 1.3.9.9 1.4 1.4.0.1 1.4.0.2 1.4.0.3 1.4.0.4
wp-asset-clean-up / classes / ObjectCache.php
wp-asset-clean-up / classes Last commit date
Admin 1 year ago OptimiseAssets 1 year ago ThirdParty 1 year ago AdminBar.php 1 year ago AssetsManager.php 1 year ago BulkChanges.php 1 year ago CleanUp.php 1 year ago Debug.php 1 year ago FileSystem.php 1 year ago HardcodedAssets.php 1 year ago Lite.php 1 year ago Main.php 1 year ago MainFront.php 1 year ago Maintenance.php 1 year ago Menu.php 1 year ago MetaBoxes.php 1 year ago Misc.php 1 year ago ObjectCache.php 1 year ago OwnAssets.php 1 year ago PluginNotifications.php 1 year ago PluginTracking.php 1 year ago Preloads.php 1 year ago Settings.php 1 year ago Tips.php 1 year ago Update.php 1 year ago
ObjectCache.php
791 lines
1 <?php
2 /** @noinspection ALL */
3 /** @noinspection MultipleReturnStatementsInspection */
4
5 namespace WpAssetCleanUp;
6
7 /**
8 * NOTE: This is from the original core file located /wp-includes/class-wp-object-cache.php
9 * Avoid the WordPress core $wp_object_cache global variable which is sometimes altered by 3rd party plugins
10 * This would make this plugin compatible with plugins such as "Redis Object Cache"
11 *
12 * Object Cache API: ObjectCache class
13 *
14 * @package WordPress
15 * @subpackage Cache
16 * @since 5.4.0
17 */
18
19 /**
20 * Core class that implements an object cache.
21 *
22 * The WordPress Object Cache is used to save on trips to the database. The
23 * Object Cache stores all the cache data to memory and makes the cache
24 * contents available by using a key, which is used to name and later retrieve
25 * the cache contents.
26 *
27 * The Object Cache can be replaced by other caching mechanisms by placing files
28 * in the wp-content folder which is looked at in wp-settings. If that file
29 * exists, then this file will not be included.
30 *
31 * @since 2.0.0
32 */
33 class ObjectCache {
34
35 /**
36 * Holds the cached objects.
37 *
38 * @since 2.0.0
39 * @var array
40 */
41 private $cache = array();
42
43 /**
44 * The amount of times the cache data was already stored in the cache.
45 *
46 * @since 2.5.0
47 * @var int
48 */
49 public $cache_hits = 0;
50
51 /**
52 * Amount of times the cache did not have the request in cache.
53 *
54 * @since 2.0.0
55 * @var int
56 */
57 public $cache_misses = 0;
58
59 /**
60 * List of global cache groups.
61 *
62 * @since 3.0.0
63 * @var array
64 */
65 protected $global_groups = array();
66
67 /**
68 * The blog prefix to prepend to keys in non-global groups.
69 *
70 * @since 3.5.0
71 * @var string
72 */
73 private $blog_prefix;
74
75 /**
76 * Holds the value of is_multisite().
77 *
78 * @since 3.5.0
79 * @var bool
80 */
81 private $multisite;
82
83 /**
84 * @var string|void
85 */
86 public static $objNotInitErrorMsg;
87
88 /**
89 * Sets up object properties; PHP 5 style constructor.
90 *
91 * @since 2.0.8
92 */
93 public function __construct() {
94 self::$objNotInitErrorMsg = self::showTextDomainObjNotInitErrorMsg();
95
96 $this->multisite = is_multisite();
97 $this->blog_prefix = $this->multisite ? get_current_blog_id() . ':' : '';
98 }
99
100 /**
101 * @return string|null
102 */
103 public static function showTextDomainObjNotInitErrorMsg()
104 {
105 if (did_action('after_setup_theme')) {
106 return __('Asset CleanUp\'s object cache is not valid (from method "[method]").', 'wp-asset-clean-up');
107 } else {
108 return 'Asset CleanUp\'s object cache is not valid (from method "[method]").';
109 }
110 }
111
112 /**
113 * Makes private properties readable for backward compatibility.
114 *
115 * @since 4.0.0
116 *
117 * @param string $name Property to get.
118 * @return mixed Property.
119 */
120 public function __get( $name ) {
121 return $this->$name;
122 }
123
124 /**
125 * Makes private properties settable for backward compatibility.
126 *
127 * @since 4.0.0
128 *
129 * @param string $name Property to set.
130 * @param mixed $value Property value.
131 * @return mixed Newly-set property.
132 */
133 public function __set( $name, $value ) {
134 return $this->$name = $value;
135 }
136
137 /**
138 * Makes private properties checkable for backward compatibility.
139 *
140 * @since 4.0.0
141 *
142 * @param string $name Property to check if set.
143 * @return bool Whether the property is set.
144 */
145 public function __isset( $name ) {
146 return isset( $this->$name );
147 }
148
149 /**
150 * Makes private properties un-settable for backward compatibility.
151 *
152 * @since 4.0.0
153 *
154 * @param string $name Property to unset.
155 */
156 public function __unset( $name ) {
157 unset( $this->$name );
158 }
159
160 /**
161 * Adds data to the cache if it doesn't already exist.
162 *
163 * @since 2.0.0
164 *
165 * @uses ObjectCache::_exists() Checks to see if the cache already has data.
166 * @uses ObjectCache::set() Sets the data after the checking the cache
167 * contents existence.
168 *
169 * @param int|string $key What to call the contents in the cache.
170 * @param mixed $data The contents to store in the cache.
171 * @param string $group Optional. Where to group the cache contents. Default 'default'.
172 * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration).
173 * @return bool True on success, false if cache key and group already exist.
174 */
175 public function add( $key, $data, $group = 'default', $expire = 0 ) {
176 if ( wp_suspend_cache_addition() ) {
177 return false;
178 }
179
180 if ( empty( $group ) ) {
181 $group = 'default';
182 }
183
184 $id = $key;
185 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
186 $id = $this->blog_prefix . $key;
187 }
188
189 if ( $this->_exists( $id, $group ) ) {
190 return false;
191 }
192
193 return $this->set( $key, $data, $group, (int) $expire );
194 }
195
196 /**
197 * Sets the list of global cache groups.
198 *
199 * @since 3.0.0
200 *
201 * @param array $groups List of groups that are global.
202 */
203 public function add_global_groups( $groups ) {
204 $groups = (array) $groups;
205
206 $groups = array_fill_keys( $groups, true );
207 $this->global_groups = array_merge( $this->global_groups, $groups );
208 }
209
210 /**
211 * Decrements numeric cache item's value.
212 *
213 * @since 3.3.0
214 *
215 * @param int|string $key The cache key to decrement.
216 * @param int $offset Optional. The amount by which to decrement the item's value. Default 1.
217 * @param string $group Optional. The group the key is in. Default 'default'.
218 * @return int|false The item's new value on success, false on failure.
219 */
220 public function decr( $key, $offset = 1, $group = 'default' ) {
221 if ( empty( $group ) ) {
222 $group = 'default';
223 }
224
225 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
226 $key = $this->blog_prefix . $key;
227 }
228
229 if ( ! $this->_exists( $key, $group ) ) {
230 return false;
231 }
232
233 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) {
234 $this->cache[ $group ][ $key ] = 0;
235 }
236
237 $offset = (int) $offset;
238
239 $this->cache[ $group ][ $key ] -= $offset;
240
241 if ( $this->cache[ $group ][ $key ] < 0 ) {
242 $this->cache[ $group ][ $key ] = 0;
243 }
244
245 return $this->cache[ $group ][ $key ];
246 }
247
248 /**
249 * Removes the contents of the cache key in the group.
250 *
251 * If the cache key does not exist in the group, then nothing will happen.
252 *
253 * @since 2.0.0
254 *
255 * @param int|string $key What the contents in the cache are called.
256 * @param string $group Optional. Where the cache contents are grouped. Default 'default'.
257 * @param bool $deprecated Optional. Unused. Default false.
258 * @return bool False if the contents weren't deleted and true on success.
259 */
260 public function delete( $key, $group = 'default', $deprecated = false ) {
261 if ( empty( $group ) ) {
262 $group = 'default';
263 }
264
265 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
266 $key = $this->blog_prefix . $key;
267 }
268
269 if ( ! $this->_exists( $key, $group ) ) {
270 return false;
271 }
272
273 unset( $this->cache[ $group ][ $key ] );
274 return true;
275 }
276
277 /**
278 * Clears the object cache of all data.
279 *
280 * @since 2.0.0
281 *
282 * @return true Always returns true.
283 */
284 public function flush() {
285 $this->cache = array();
286
287 return true;
288 }
289
290 /**
291 * Retrieves the cache contents, if it exists.
292 *
293 * The contents will be first attempted to be retrieved by searching by the
294 * key in the cache group. If the cache is hit (success) then the contents
295 * are returned.
296 *
297 * On failure, the number of cache misses will be incremented.
298 *
299 * @since 2.0.0
300 *
301 * @param int|string $key What the contents in the cache are called.
302 * @param string $group Optional. Where the cache contents are grouped. Default 'default'.
303 * @param bool $force Optional. Unused. Whether to force a refetch rather than relying on the local
304 * cache. Default false.
305 * @param bool $found Optional. Whether the key was found in the cache (passed by reference).
306 * Disambiguates a return of false, a storable value. Default null.
307 * @return mixed|false The cache contents on success, false on failure to retrieve contents.
308 */
309 public function get( $key, $group = 'default', $force = false, &$found = null ) {
310 if ( empty( $group ) ) {
311 $group = 'default';
312 }
313
314 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
315 $key = $this->blog_prefix . $key;
316 }
317
318 if ( $this->_exists( $key, $group ) ) {
319 $found = true;
320 $this->cache_hits += 1;
321 if ( is_object( $this->cache[ $group ][ $key ] ) ) {
322 return clone $this->cache[ $group ][ $key ];
323 } else {
324 return $this->cache[ $group ][ $key ];
325 }
326 }
327
328 $found = false;
329 $this->cache_misses += 1;
330 return false;
331 }
332
333 /**
334 * Increments numeric cache item's value.
335 *
336 * @since 3.3.0
337 *
338 * @param int|string $key The cache key to increment
339 * @param int $offset Optional. The amount by which to increment the item's value. Default 1.
340 * @param string $group Optional. The group the key is in. Default 'default'.
341 * @return int|false The item's new value on success, false on failure.
342 */
343 public function incr( $key, $offset = 1, $group = 'default' ) {
344 if ( empty( $group ) ) {
345 $group = 'default';
346 }
347
348 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
349 $key = $this->blog_prefix . $key;
350 }
351
352 if ( ! $this->_exists( $key, $group ) ) {
353 return false;
354 }
355
356 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) {
357 $this->cache[ $group ][ $key ] = 0;
358 }
359
360 $offset = (int) $offset;
361
362 $this->cache[ $group ][ $key ] += $offset;
363
364 if ( $this->cache[ $group ][ $key ] < 0 ) {
365 $this->cache[ $group ][ $key ] = 0;
366 }
367
368 return $this->cache[ $group ][ $key ];
369 }
370
371 /**
372 * Replaces the contents in the cache, if contents already exist.
373 *
374 * @since 2.0.0
375 *
376 * @see ObjectCache::set()
377 *
378 * @param int|string $key What to call the contents in the cache.
379 * @param mixed $data The contents to store in the cache.
380 * @param string $group Optional. Where to group the cache contents. Default 'default'.
381 * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration).
382 * @return bool False if not exists, true if contents were replaced.
383 */
384 public function replace( $key, $data, $group = 'default', $expire = 0 ) {
385 if ( empty( $group ) ) {
386 $group = 'default';
387 }
388
389 $id = $key;
390 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
391 $id = $this->blog_prefix . $key;
392 }
393
394 if ( ! $this->_exists( $id, $group ) ) {
395 return false;
396 }
397
398 return $this->set( $key, $data, $group, (int) $expire );
399 }
400
401 /**
402 * Resets cache keys.
403 *
404 * @since 3.0.0
405 *
406 * @deprecated 3.5.0 Use switch_to_blog()
407 * @see switch_to_blog()
408 */
409 public function reset() {
410 _deprecated_function( __FUNCTION__, '3.5.0', 'switch_to_blog()' );
411
412 // Clear out non-global caches since the blog ID has changed.
413 foreach ( array_keys( $this->cache ) as $group ) {
414 if ( ! isset( $this->global_groups[ $group ] ) ) {
415 unset( $this->cache[ $group ] );
416 }
417 }
418 }
419
420 /**
421 * Sets the data contents into the cache.
422 *
423 * The cache contents are grouped by the $group parameter followed by the
424 * $key. This allows for duplicate ids in unique groups. Therefore, naming of
425 * the group should be used with care and should follow normal function
426 * naming guidelines outside of core WordPress usage.
427 *
428 * The $expire parameter is not used, because the cache will automatically
429 * expire for each time a page is accessed and PHP finishes. The method is
430 * more for cache plugins which use files.
431 *
432 * @since 2.0.0
433 *
434 * @param int|string $key What to call the contents in the cache.
435 * @param mixed $data The contents to store in the cache.
436 * @param string $group Optional. Where to group the cache contents. Default 'default'.
437 * @param int $expire Not Used.
438 * @return true Always returns true.
439 */
440 public function set( $key, $data, $group = 'default', $expire = 0 ) {
441 if ( empty( $group ) ) {
442 $group = 'default';
443 }
444
445 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
446 $key = $this->blog_prefix . $key;
447 }
448
449 if ( is_object( $data ) ) {
450 $data = clone $data;
451 }
452
453 $this->cache[ $group ][ $key ] = $data;
454 return true;
455 }
456
457 /**
458 * Echoes the stats of the caching.
459 *
460 * Gives the cache hits, and cache misses. Also prints every cached group,
461 * key and the data.
462 *
463 * @since 2.0.0
464 */
465 public function stats() {
466 echo '<p>';
467 echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
468 echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
469 echo '</p>';
470 echo '<ul>';
471 foreach ( $this->cache as $group => $cache ) {
472 echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>';
473 }
474 echo '</ul>';
475 }
476
477 /**
478 * Switches the internal blog ID.
479 *
480 * This changes the blog ID used to create keys in blog specific groups.
481 *
482 * @since 3.5.0
483 *
484 * @param int $blog_id Blog ID.
485 */
486 public function switch_to_blog( $blog_id ) {
487 $blog_id = (int) $blog_id;
488 $this->blog_prefix = $this->multisite ? $blog_id . ':' : '';
489 }
490
491 /**
492 * Serves as a utility function to determine whether a key exists in the cache.
493 *
494 * @since 3.4.0
495 *
496 * @param int|string $key Cache key to check for existence.
497 * @param string $group Cache group for the key existence check.
498 * @return bool Whether the key exists in the cache for the given group.
499 */
500 protected function _exists( $key, $group ) {
501 return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) );
502 }
503
504 /**
505 * [START] Functions to call (reference: /wp-includes/cache.php)
506 */
507 /**
508 * Adds data to the cache, if the cache key doesn't already exist.
509 *
510 * @since 2.0.0
511 *
512 * @see ObjectCache::add()
513 * @global ObjectCache $wpacu_object_cache Object cache global instance.
514 *
515 * @param int|string $key The cache key to use for retrieval later.
516 * @param mixed $data The data to add to the cache.
517 * @param string $group Optional. The group to add the cache to. Enables the same key
518 * to be used across groups. Default empty.
519 * @param int $expire Optional. When the cache data should expire, in seconds.
520 * Default 0 (no expiration).
521 * @return bool True on success, false if cache key and group already exist.
522 */
523 public static function wpacu_cache_add( $key, $data, $group = '', $expire = 0 ) {
524 global $wpacu_object_cache;
525
526 return $wpacu_object_cache->add( $key, $data, $group, (int) $expire );
527 }
528
529 /**
530 * Closes the cache.
531 *
532 * This function has ceased to do anything since WordPress 2.5. The
533 * functionality was removed along with the rest of the persistent cache.
534 *
535 * This does not mean that plugins can't implement this function when they need
536 * to make sure that the cache is cleaned up after WordPress no longer needs it.
537 *
538 * @since 2.0.0
539 *
540 * @return true Always returns true.
541 */
542 public static function wpacu_cache_close() {
543 return true;
544 }
545
546 /**
547 * Decrements numeric cache item's value.
548 *
549 * @since 3.3.0
550 *
551 * @see ObjectCache::decr()
552 * @global ObjectCache $wpacu_object_cache Object cache global instance.
553 *
554 * @param int|string $key The cache key to decrement.
555 * @param int $offset Optional. The amount by which to decrement the item's value. Default 1.
556 * @param string $group Optional. The group the key is in. Default empty.
557 * @return int|false The item's new value on success, false on failure.
558 */
559 public static function wpacu_cache_decr( $key, $offset = 1, $group = '' ) {
560 if ( ! self::isValidObjectCache() ) { error_log(str_replace('[method]', __METHOD__, self::$objNotInitErrorMsg)); return; }
561 global $wpacu_object_cache;
562
563 return $wpacu_object_cache->decr( $key, $offset, $group );
564 }
565
566 /**
567 * Removes the cache contents matching key and group.
568 *
569 * @since 2.0.0
570 *
571 * @see ObjectCache::delete()
572 * @global ObjectCache $wpacu_object_cache Object cache global instance.
573 *
574 * @param int|string $key What the contents in the cache are called.
575 * @param string $group Optional. Where the cache contents are grouped. Default empty.
576 * @return bool True on successful removal, false on failure.
577 */
578 public static function wpacu_cache_delete( $key, $group = '' ) {
579 if ( ! self::isValidObjectCache() ) { error_log(str_replace('[method]', __METHOD__, self::$objNotInitErrorMsg)); return; }
580 global $wpacu_object_cache;
581
582 return $wpacu_object_cache->delete( $key, $group );
583 }
584
585 /**
586 * Removes all cache items.
587 *
588 * @since 2.0.0
589 *
590 * @see ObjectCache::flush()
591 * @global ObjectCache $wpacu_object_cache Object cache global instance.
592 *
593 * @return bool True on success, false on failure.
594 */
595 public static function wpacu_cache_flush() {
596 if ( ! self::isValidObjectCache() ) { error_log(str_replace('[method]', __METHOD__, self::$objNotInitErrorMsg)); return; }
597 global $wpacu_object_cache;
598
599 return $wpacu_object_cache->flush();
600 }
601
602 /**
603 * Retrieves the cache contents from the cache by key and group.
604 *
605 * @since 2.0.0
606 *
607 * @see ObjectCache::get()
608 * @global ObjectCache $wpacu_object_cache Object cache global instance.
609 *
610 * @param int|string $key The key under which the cache contents are stored.
611 * @param string $group Optional. Where the cache contents are grouped. Default empty.
612 * @param bool $force Optional. Whether to force an update of the local cache from the persistent
613 * cache. Default false.
614 * @param bool $found Optional. Whether the key was found in the cache (passed by reference).
615 * Disambiguates a return of false, a storable value. Default null.
616 * @return bool|mixed False on failure to retrieve contents or the cache
617 * contents on success
618 */
619 public static function wpacu_cache_get( $key, $group = '', $force = false, &$found = null ) {
620 if ( ! self::isValidObjectCache() ) { error_log(str_replace('[method]', __METHOD__, self::$objNotInitErrorMsg)); return; }
621 global $wpacu_object_cache;
622
623 return $wpacu_object_cache->get( $key, $group, $force, $found );
624 }
625
626 /**
627 * Increment numeric cache item's value
628 *
629 * @since 3.3.0
630 *
631 * @see ObjectCache::incr()
632 * @global ObjectCache $wpacu_object_cache Object cache global instance.
633 *
634 * @param int|string $key The key for the cache contents that should be incremented.
635 * @param int $offset Optional. The amount by which to increment the item's value. Default 1.
636 * @param string $group Optional. The group the key is in. Default empty.
637 * @return int|false The item's new value on success, false on failure.
638 */
639 public static function wpacu_cache_incr( $key, $offset = 1, $group = '' ) {
640 if ( ! self::isValidObjectCache() ) { error_log(str_replace('[method]', __METHOD__, self::$objNotInitErrorMsg)); return; }
641 global $wpacu_object_cache;
642
643 return $wpacu_object_cache->incr( $key, $offset, $group );
644 }
645
646 /**
647 * Sets up Object Cache Global and assigns it.
648 *
649 * @since 2.0.0
650 *
651 * @global ObjectCache $wpacu_object_cache
652 */
653 public static function wpacu_cache_init() {
654 $GLOBALS['wpacu_object_cache'] = new self();
655 }
656
657 /**
658 * Replaces the contents of the cache with new data.
659 *
660 * @since 2.0.0
661 *
662 * @see ObjectCache::replace()
663 * @global ObjectCache $wpacu_object_cache Object cache global instance.
664 *
665 * @param int|string $key The key for the cache data that should be replaced.
666 * @param mixed $data The new data to store in the cache.
667 * @param string $group Optional. The group for the cache data that should be replaced.
668 * Default empty.
669 * @param int $expire Optional. When to expire the cache contents, in seconds.
670 * Default 0 (no expiration).
671 * @return bool False if original value does not exist, true if contents were replaced
672 */
673 public static function wpacu_cache_replace( $key, $data, $group = '', $expire = 0 ) {
674 if ( ! self::isValidObjectCache() ) { error_log(str_replace('[method]', __METHOD__, self::$objNotInitErrorMsg)); return; }
675 global $wpacu_object_cache;
676
677 return $wpacu_object_cache->replace( $key, $data, $group, (int) $expire );
678 }
679
680 /**
681 * Saves the data to the cache.
682 *
683 * Differs from ObjectCache::wpacu_cache_add() and wp_cache_replace() in that it will always write data.
684 *
685 * @since 2.0.0
686 *
687 * @see ObjectCache::set()
688 * @global ObjectCache $wpacu_object_cache Object cache global instance.
689 *
690 * @param int|string $key The cache key to use for retrieval later.
691 * @param mixed $data The contents to store in the cache.
692 * @param string $group Optional. Where to group the cache contents. Enables the same key
693 * to be used across groups. Default empty.
694 * @param int $expire Optional. When to expire the cache contents, in seconds.
695 * Default 0 (no expiration).
696 * @return bool True on success, false on failure.
697 */
698 public static function wpacu_cache_set( $key, $data, $group = '', $expire = 0 ) {
699 global $wpacu_object_cache;
700
701 return $wpacu_object_cache->set( $key, $data, $group, (int) $expire );
702 }
703
704 /**
705 * Switches the internal blog ID.
706 *
707 * This changes the blog id used to create keys in blog specific groups.
708 *
709 * @since 3.5.0
710 *
711 * @see ObjectCache::switch_to_blog()
712 * @global ObjectCache $wpacu_object_cache Object cache global instance.
713 *
714 * @param int $blog_id Site ID.
715 */
716 public static function wpacu_cache_switch_to_blog( $blog_id ) {
717 global $wpacu_object_cache;
718
719 $wpacu_object_cache->switch_to_blog( $blog_id );
720 }
721
722 /**
723 * Adds a group or set of groups to the list of global groups.
724 *
725 * @since 2.6.0
726 *
727 * @see ObjectCache::add_global_groups()
728 * @global ObjectCache $wpacu_object_cache Object cache global instance.
729 *
730 * @param string|array $groups A group or an array of groups to add.
731 */
732 public static function wpacu_cache_add_global_groups( $groups ) {
733 global $wpacu_object_cache;
734
735 $wpacu_object_cache->add_global_groups( $groups );
736 }
737
738 /**
739 * Adds a group or set of groups to the list of non-persistent groups.
740 *
741 * @since 2.6.0
742 *
743 * @param string|array $groups A group or an array of groups to add.
744 */
745 public static function wpacu_cache_add_non_persistent_groups( $groups ) {
746 // Default cache doesn't persist so nothing to do here.
747 }
748
749 /**
750 * Reset internal cache keys and structures.
751 *
752 * If the cache back end uses global blog or site IDs as part of its cache keys,
753 * this function instructs the back end to reset those keys and perform any cleanup
754 * since blog or site IDs have changed since cache init.
755 *
756 * This function is deprecated. Use wp_cache_switch_to_blog() instead of this
757 * function when preparing the cache for a blog switch. For clearing the cache
758 * during unit tests, consider using wp_cache_init(). wp_cache_init() is not
759 * recommended outside of unit tests as the performance penalty for using it is
760 * high.
761 *
762 * @since 2.6.0
763 * @deprecated 3.5.0 ObjectCache::reset()
764 * @see ObjectCache::reset()
765 *
766 * @global ObjectCache $wpacu_object_cache Object cache global instance.
767 */
768 public static function wpacu_cache_reset() {
769 _deprecated_function( __FUNCTION__, '3.5.0', 'ObjectCache::reset()' );
770
771 if ( ! self::isValidObjectCache() ) { error_log(str_replace('[method]', __METHOD__, self::$objNotInitErrorMsg)); return; }
772 global $wpacu_object_cache;
773
774 $wpacu_object_cache->reset();
775 }
776
777 /**
778 * Main purpose: Avoid errors such as "PHP Fatal error: Uncaught Error: Call to a member function get() on null"
779 *
780 * @return bool
781 */
782 public static function isValidObjectCache()
783 {
784 global $wpacu_object_cache;
785 return isset( $wpacu_object_cache ) && ! is_null( $wpacu_object_cache );
786 }
787 /**
788 * [END] Functions to call (reference: /wp-includes/cache.php)
789 */
790 }
791