178 |
178 |
179 return $wp_object_cache->set($key, $data, $group, $expire); |
179 return $wp_object_cache->set($key, $data, $group, $expire); |
180 } |
180 } |
181 |
181 |
182 /** |
182 /** |
|
183 * Switch the interal blog id. |
|
184 * |
|
185 * This changes the blog id used to create keys in blog specific groups. |
|
186 * |
|
187 * @since 3.5.0 |
|
188 * |
|
189 * @param int $blog_id Blog ID |
|
190 */ |
|
191 function wp_cache_switch_to_blog( $blog_id ) { |
|
192 global $wp_object_cache; |
|
193 |
|
194 return $wp_object_cache->switch_to_blog( $blog_id ); |
|
195 } |
|
196 |
|
197 /** |
183 * Adds a group or set of groups to the list of global groups. |
198 * Adds a group or set of groups to the list of global groups. |
184 * |
199 * |
185 * @since 2.6.0 |
200 * @since 2.6.0 |
186 * |
201 * |
187 * @param string|array $groups A group or an array of groups to add |
202 * @param string|array $groups A group or an array of groups to add |
188 */ |
203 */ |
189 function wp_cache_add_global_groups( $groups ) { |
204 function wp_cache_add_global_groups( $groups ) { |
190 global $wp_object_cache; |
205 global $wp_object_cache; |
191 |
206 |
192 return $wp_object_cache->add_global_groups($groups); |
207 return $wp_object_cache->add_global_groups( $groups ); |
193 } |
208 } |
194 |
209 |
195 /** |
210 /** |
196 * Adds a group or set of groups to the list of non-persistent groups. |
211 * Adds a group or set of groups to the list of non-persistent groups. |
197 * |
212 * |
203 // Default cache doesn't persist so nothing to do here. |
218 // Default cache doesn't persist so nothing to do here. |
204 return; |
219 return; |
205 } |
220 } |
206 |
221 |
207 /** |
222 /** |
208 * Reset internal cache keys and structures. If the cache backend uses global blog or site IDs as part of its cache keys, |
223 * Reset internal cache keys and structures. If the cache backend uses global |
209 * this function instructs the backend to reset those keys and perform any cleanup since blog or site IDs have changed since cache init. |
224 * blog or site IDs as part of its cache keys, this function instructs the |
|
225 * backend to reset those keys and perform any cleanup since blog or site IDs |
|
226 * have changed since cache init. |
|
227 * |
|
228 * This function is deprecated. Use wp_cache_switch_to_blog() instead of this |
|
229 * function when preparing the cache for a blog switch. For clearing the cache |
|
230 * during unit tests, consider using wp_cache_init(). wp_cache_init() is not |
|
231 * recommended outside of unit tests as the performance penality for using it is |
|
232 * high. |
210 * |
233 * |
211 * @since 2.6.0 |
234 * @since 2.6.0 |
|
235 * @deprecated 3.5.0 |
212 */ |
236 */ |
213 function wp_cache_reset() { |
237 function wp_cache_reset() { |
|
238 _deprecated_function( __FUNCTION__, '3.5' ); |
|
239 |
214 global $wp_object_cache; |
240 global $wp_object_cache; |
215 |
241 |
216 return $wp_object_cache->reset(); |
242 return $wp_object_cache->reset(); |
217 } |
243 } |
218 |
244 |
290 return false; |
325 return false; |
291 |
326 |
292 if ( empty( $group ) ) |
327 if ( empty( $group ) ) |
293 $group = 'default'; |
328 $group = 'default'; |
294 |
329 |
295 if ( $this->_exists($key, $group) ) |
330 $id = $key; |
|
331 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
|
332 $id = $this->blog_prefix . $key; |
|
333 |
|
334 if ( $this->_exists( $id, $group ) ) |
296 return false; |
335 return false; |
297 |
336 |
298 return $this->set($key, $data, $group, $expire); |
337 return $this->set($key, $data, $group, $expire); |
299 } |
338 } |
300 |
339 |
306 * @param array $groups List of groups that are global. |
345 * @param array $groups List of groups that are global. |
307 */ |
346 */ |
308 function add_global_groups( $groups ) { |
347 function add_global_groups( $groups ) { |
309 $groups = (array) $groups; |
348 $groups = (array) $groups; |
310 |
349 |
311 $this->global_groups = array_merge($this->global_groups, $groups); |
350 $groups = array_fill_keys( $groups, true ); |
312 $this->global_groups = array_unique($this->global_groups); |
351 $this->global_groups = array_merge( $this->global_groups, $groups ); |
313 } |
352 } |
314 |
353 |
315 /** |
354 /** |
316 * Decrement numeric cache item's value |
355 * Decrement numeric cache item's value |
317 * |
356 * |
321 * @param int $offset The amount by which to decrement the item's value. Default is 1. |
360 * @param int $offset The amount by which to decrement the item's value. Default is 1. |
322 * @param string $group The group the key is in. |
361 * @param string $group The group the key is in. |
323 * @return false|int False on failure, the item's new value on success. |
362 * @return false|int False on failure, the item's new value on success. |
324 */ |
363 */ |
325 function decr( $key, $offset = 1, $group = 'default' ) { |
364 function decr( $key, $offset = 1, $group = 'default' ) { |
|
365 if ( empty( $group ) ) |
|
366 $group = 'default'; |
|
367 |
|
368 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
|
369 $key = $this->blog_prefix . $key; |
|
370 |
326 if ( ! $this->_exists( $key, $group ) ) |
371 if ( ! $this->_exists( $key, $group ) ) |
327 return false; |
372 return false; |
328 |
373 |
329 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) |
374 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) |
330 $this->cache[ $group ][ $key ] = 0; |
375 $this->cache[ $group ][ $key ] = 0; |
356 */ |
401 */ |
357 function delete($key, $group = 'default', $force = false) { |
402 function delete($key, $group = 'default', $force = false) { |
358 if ( empty( $group ) ) |
403 if ( empty( $group ) ) |
359 $group = 'default'; |
404 $group = 'default'; |
360 |
405 |
|
406 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
|
407 $key = $this->blog_prefix . $key; |
|
408 |
361 if ( ! $force && ! $this->_exists( $key, $group ) ) |
409 if ( ! $force && ! $this->_exists( $key, $group ) ) |
362 return false; |
410 return false; |
363 |
411 |
364 unset( $this->cache[$group][$key] ); |
412 unset( $this->cache[$group][$key] ); |
365 return true; |
413 return true; |
396 * contents on success |
444 * contents on success |
397 */ |
445 */ |
398 function get( $key, $group = 'default', $force = false, &$found = null ) { |
446 function get( $key, $group = 'default', $force = false, &$found = null ) { |
399 if ( empty( $group ) ) |
447 if ( empty( $group ) ) |
400 $group = 'default'; |
448 $group = 'default'; |
|
449 |
|
450 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
|
451 $key = $this->blog_prefix . $key; |
401 |
452 |
402 if ( $this->_exists( $key, $group ) ) { |
453 if ( $this->_exists( $key, $group ) ) { |
403 $found = true; |
454 $found = true; |
404 $this->cache_hits += 1; |
455 $this->cache_hits += 1; |
405 if ( is_object($this->cache[$group][$key]) ) |
456 if ( is_object($this->cache[$group][$key]) ) |
425 */ |
476 */ |
426 function incr( $key, $offset = 1, $group = 'default' ) { |
477 function incr( $key, $offset = 1, $group = 'default' ) { |
427 if ( empty( $group ) ) |
478 if ( empty( $group ) ) |
428 $group = 'default'; |
479 $group = 'default'; |
429 |
480 |
|
481 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
|
482 $key = $this->blog_prefix . $key; |
|
483 |
430 if ( ! $this->_exists( $key, $group ) ) |
484 if ( ! $this->_exists( $key, $group ) ) |
431 return false; |
485 return false; |
432 |
486 |
433 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) |
487 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) |
434 $this->cache[ $group ][ $key ] = 0; |
488 $this->cache[ $group ][ $key ] = 0; |
453 * @param mixed $data The contents to store in the cache |
507 * @param mixed $data The contents to store in the cache |
454 * @param string $group Where to group the cache contents |
508 * @param string $group Where to group the cache contents |
455 * @param int $expire When to expire the cache contents |
509 * @param int $expire When to expire the cache contents |
456 * @return bool False if not exists, true if contents were replaced |
510 * @return bool False if not exists, true if contents were replaced |
457 */ |
511 */ |
458 function replace($key, $data, $group = 'default', $expire = '') { |
512 function replace( $key, $data, $group = 'default', $expire = '' ) { |
459 if ( empty( $group ) ) |
513 if ( empty( $group ) ) |
460 $group = 'default'; |
514 $group = 'default'; |
461 |
515 |
462 if ( ! $this->_exists( $key, $group ) ) |
516 $id = $key; |
|
517 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
|
518 $id = $this->blog_prefix . $key; |
|
519 |
|
520 if ( ! $this->_exists( $id, $group ) ) |
463 return false; |
521 return false; |
464 |
522 |
465 return $this->set($key, $data, $group, $expire); |
523 return $this->set( $key, $data, $group, $expire ); |
466 } |
524 } |
467 |
525 |
468 /** |
526 /** |
469 * Reset keys |
527 * Reset keys |
470 * |
528 * |
471 * @since 3.0.0 |
529 * @since 3.0.0 |
|
530 * @deprecated 3.5.0 |
472 */ |
531 */ |
473 function reset() { |
532 function reset() { |
|
533 _deprecated_function( __FUNCTION__, '3.5', 'switch_to_blog()' ); |
|
534 |
474 // Clear out non-global caches since the blog ID has changed. |
535 // Clear out non-global caches since the blog ID has changed. |
475 foreach ( array_keys($this->cache) as $group ) { |
536 foreach ( array_keys( $this->cache ) as $group ) { |
476 if ( !in_array($group, $this->global_groups) ) |
537 if ( ! isset( $this->global_groups[ $group ] ) ) |
477 unset($this->cache[$group]); |
538 unset( $this->cache[ $group ] ); |
478 } |
539 } |
479 } |
540 } |
480 |
541 |
481 /** |
542 /** |
482 * Sets the data contents into the cache |
543 * Sets the data contents into the cache |
500 */ |
561 */ |
501 function set($key, $data, $group = 'default', $expire = '') { |
562 function set($key, $data, $group = 'default', $expire = '') { |
502 if ( empty( $group ) ) |
563 if ( empty( $group ) ) |
503 $group = 'default'; |
564 $group = 'default'; |
504 |
565 |
505 if ( is_object($data) ) |
566 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) |
|
567 $key = $this->blog_prefix . $key; |
|
568 |
|
569 if ( is_object( $data ) ) |
506 $data = clone $data; |
570 $data = clone $data; |
507 |
571 |
508 $this->cache[$group][$key] = $data; |
572 $this->cache[$group][$key] = $data; |
509 return true; |
573 return true; |
510 } |
574 } |
528 } |
592 } |
529 echo '</ul>'; |
593 echo '</ul>'; |
530 } |
594 } |
531 |
595 |
532 /** |
596 /** |
|
597 * Switch the interal blog id. |
|
598 * |
|
599 * This changes the blog id used to create keys in blog specific groups. |
|
600 * |
|
601 * @since 3.5.0 |
|
602 * |
|
603 * @param int $blog_id Blog ID |
|
604 */ |
|
605 function switch_to_blog( $blog_id ) { |
|
606 $blog_id = (int) $blog_id; |
|
607 $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; |
|
608 } |
|
609 |
|
610 /** |
533 * Utility function to determine whether a key exists in the cache. |
611 * Utility function to determine whether a key exists in the cache. |
534 * @access private |
612 * |
535 */ |
613 * @since 3.4.0 |
536 protected function _exists($key, $group) { |
614 * |
537 return isset( $this->cache[$group] ) && is_array( $this->cache[$group] ) && array_key_exists( $key, $this->cache[$group] ); |
615 * @access protected |
|
616 */ |
|
617 protected function _exists( $key, $group ) { |
|
618 return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) ); |
538 } |
619 } |
539 |
620 |
540 /** |
621 /** |
541 * Sets up object properties; PHP 5 style constructor |
622 * Sets up object properties; PHP 5 style constructor |
542 * |
623 * |
543 * @since 2.0.8 |
624 * @since 2.0.8 |
544 * @return null|WP_Object_Cache If cache is disabled, returns null. |
625 * @return null|WP_Object_Cache If cache is disabled, returns null. |
545 */ |
626 */ |
546 function __construct() { |
627 function __construct() { |
|
628 global $blog_id; |
|
629 |
|
630 $this->multisite = is_multisite(); |
|
631 $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; |
|
632 |
|
633 |
547 /** |
634 /** |
548 * @todo This should be moved to the PHP4 style constructor, PHP5 |
635 * @todo This should be moved to the PHP4 style constructor, PHP5 |
549 * already calls __destruct() |
636 * already calls __destruct() |
550 */ |
637 */ |
551 register_shutdown_function(array(&$this, "__destruct")); |
638 register_shutdown_function( array( $this, '__destruct' ) ); |
552 } |
639 } |
553 |
640 |
554 /** |
641 /** |
555 * Will save the object cache before object is completely destroyed. |
642 * Will save the object cache before object is completely destroyed. |
556 * |
643 * |