/wp-includes/cache.php

  1. <?php 
  2. /** 
  3. * Object Cache API 
  4. * 
  5. * @link https://codex.wordpress.org/Class_Reference/WP_Object_Cache 
  6. * 
  7. * @package WordPress 
  8. * @subpackage Cache 
  9. */ 
  10.  
  11. /** 
  12. * Adds data to the cache, if the cache key doesn't already exist. 
  13. * 
  14. * @since 2.0.0 
  15. * 
  16. * @see WP_Object_Cache::add() 
  17. * @global WP_Object_Cache $wp_object_cache Object cache global instance. 
  18. * 
  19. * @param int|string $key The cache key to use for retrieval later. 
  20. * @param mixed $data The data to add to the cache. 
  21. * @param string $group Optional. The group to add the cache to. Enables the same key 
  22. * to be used across groups. Default empty. 
  23. * @param int $expire Optional. When the cache data should expire, in seconds. 
  24. * Default 0 (no expiration). 
  25. * @return bool False if cache key and group already exist, true on success. 
  26. */ 
  27. function wp_cache_add( $key, $data, $group = '', $expire = 0 ) { 
  28. global $wp_object_cache; 
  29.  
  30. return $wp_object_cache->add( $key, $data, $group, (int) $expire ); 
  31.  
  32. /** 
  33. * Closes the cache. 
  34. * 
  35. * This function has ceased to do anything since WordPress 2.5. The 
  36. * functionality was removed along with the rest of the persistent cache. 
  37. * 
  38. * This does not mean that plugins can't implement this function when they need 
  39. * to make sure that the cache is cleaned up after WordPress no longer needs it. 
  40. * 
  41. * @since 2.0.0 
  42. * 
  43. * @return true Always returns true. 
  44. */ 
  45. function wp_cache_close() { 
  46. return true; 
  47.  
  48. /** 
  49. * Decrements numeric cache item's value. 
  50. * 
  51. * @since 3.3.0 
  52. * 
  53. * @see WP_Object_Cache::decr() 
  54. * @global WP_Object_Cache $wp_object_cache Object cache global instance. 
  55. * 
  56. * @param int|string $key The cache key to decrement. 
  57. * @param int $offset Optional. The amount by which to decrement the item's value. Default 1. 
  58. * @param string $group Optional. The group the key is in. Default empty. 
  59. * @return false|int False on failure, the item's new value on success. 
  60. */ 
  61. function wp_cache_decr( $key, $offset = 1, $group = '' ) { 
  62. global $wp_object_cache; 
  63.  
  64. return $wp_object_cache->decr( $key, $offset, $group ); 
  65.  
  66. /** 
  67. * Removes the cache contents matching key and group. 
  68. * 
  69. * @since 2.0.0 
  70. * 
  71. * @see WP_Object_Cache::delete() 
  72. * @global WP_Object_Cache $wp_object_cache Object cache global instance. 
  73. * 
  74. * @param int|string $key What the contents in the cache are called. 
  75. * @param string $group Optional. Where the cache contents are grouped. Default empty. 
  76. * @return bool True on successful removal, false on failure. 
  77. */ 
  78. function wp_cache_delete( $key, $group = '' ) { 
  79. global $wp_object_cache; 
  80.  
  81. return $wp_object_cache->delete($key, $group); 
  82.  
  83. /** 
  84. * Removes all cache items. 
  85. * 
  86. * @since 2.0.0 
  87. * 
  88. * @see WP_Object_Cache::flush() 
  89. * @global WP_Object_Cache $wp_object_cache Object cache global instance. 
  90. * 
  91. * @return bool False on failure, true on success 
  92. */ 
  93. function wp_cache_flush() { 
  94. global $wp_object_cache; 
  95.  
  96. return $wp_object_cache->flush(); 
  97.  
  98. /** 
  99. * Retrieves the cache contents from the cache by key and group. 
  100. * 
  101. * @since 2.0.0 
  102. * 
  103. * @see WP_Object_Cache::get() 
  104. * @global WP_Object_Cache $wp_object_cache Object cache global instance. 
  105. * 
  106. * @param int|string $key The key under which the cache contents are stored. 
  107. * @param string $group Optional. Where the cache contents are grouped. Default empty. 
  108. * @param bool $force Optional. Whether to force an update of the local cache from the persistent 
  109. * cache. Default false. 
  110. * @param bool $found Optional. Whether the key was found in the cache. Disambiguates a return of false,  
  111. * a storable value. Passed by reference. Default null. 
  112. * @return bool|mixed False on failure to retrieve contents or the cache 
  113. * contents on success 
  114. */ 
  115. function wp_cache_get( $key, $group = '', $force = false, &$found = null ) { 
  116. global $wp_object_cache; 
  117.  
  118. return $wp_object_cache->get( $key, $group, $force, $found ); 
  119.  
  120. /** 
  121. * Increment numeric cache item's value 
  122. * 
  123. * @since 3.3.0 
  124. * 
  125. * @see WP_Object_Cache::incr() 
  126. * @global WP_Object_Cache $wp_object_cache Object cache global instance. 
  127. * 
  128. * @param int|string $key The key for the cache contents that should be incremented. 
  129. * @param int $offset Optional. The amount by which to increment the item's value. Default 1. 
  130. * @param string $group Optional. The group the key is in. Default empty. 
  131. * @return false|int False on failure, the item's new value on success. 
  132. */ 
  133. function wp_cache_incr( $key, $offset = 1, $group = '' ) { 
  134. global $wp_object_cache; 
  135.  
  136. return $wp_object_cache->incr( $key, $offset, $group ); 
  137.  
  138. /** 
  139. * Sets up Object Cache Global and assigns it. 
  140. * 
  141. * @since 2.0.0 
  142. * 
  143. * @global WP_Object_Cache $wp_object_cache 
  144. */ 
  145. function wp_cache_init() { 
  146. $GLOBALS['wp_object_cache'] = new WP_Object_Cache(); 
  147.  
  148. /** 
  149. * Replaces the contents of the cache with new data. 
  150. * 
  151. * @since 2.0.0 
  152. * 
  153. * @see WP_Object_Cache::replace() 
  154. * @global WP_Object_Cache $wp_object_cache Object cache global instance. 
  155. * 
  156. * @param int|string $key The key for the cache data that should be replaced. 
  157. * @param mixed $data The new data to store in the cache. 
  158. * @param string $group Optional. The group for the cache data that should be replaced. 
  159. * Default empty. 
  160. * @param int $expire Optional. When to expire the cache contents, in seconds. 
  161. * Default 0 (no expiration). 
  162. * @return bool False if original value does not exist, true if contents were replaced 
  163. */ 
  164. function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) { 
  165. global $wp_object_cache; 
  166.  
  167. return $wp_object_cache->replace( $key, $data, $group, (int) $expire ); 
  168.  
  169. /** 
  170. * Saves the data to the cache. 
  171. * 
  172. * Differs from wp_cache_add() and wp_cache_replace() in that it will always write data. 
  173. * 
  174. * @since 2.0.0 
  175. * 
  176. * @see WP_Object_Cache::set() 
  177. * @global WP_Object_Cache $wp_object_cache Object cache global instance. 
  178. * 
  179. * @param int|string $key The cache key to use for retrieval later. 
  180. * @param mixed $data The contents to store in the cache. 
  181. * @param string $group Optional. Where to group the cache contents. Enables the same key 
  182. * to be used across groups. Default empty. 
  183. * @param int $expire Optional. When to expire the cache contents, in seconds. 
  184. * Default 0 (no expiration). 
  185. * @return bool False on failure, true on success 
  186. */ 
  187. function wp_cache_set( $key, $data, $group = '', $expire = 0 ) { 
  188. global $wp_object_cache; 
  189.  
  190. return $wp_object_cache->set( $key, $data, $group, (int) $expire ); 
  191.  
  192. /** 
  193. * Switches the internal blog ID. 
  194. * 
  195. * This changes the blog id used to create keys in blog specific groups. 
  196. * 
  197. * @since 3.5.0 
  198. * 
  199. * @see WP_Object_Cache::switch_to_blog() 
  200. * @global WP_Object_Cache $wp_object_cache Object cache global instance. 
  201. * 
  202. * @param int $blog_id Site ID. 
  203. */ 
  204. function wp_cache_switch_to_blog( $blog_id ) { 
  205. global $wp_object_cache; 
  206.  
  207. $wp_object_cache->switch_to_blog( $blog_id ); 
  208.  
  209. /** 
  210. * Adds a group or set of groups to the list of global groups. 
  211. * 
  212. * @since 2.6.0 
  213. * 
  214. * @see WP_Object_Cache::add_global_groups() 
  215. * @global WP_Object_Cache $wp_object_cache Object cache global instance. 
  216. * 
  217. * @param string|array $groups A group or an array of groups to add. 
  218. */ 
  219. function wp_cache_add_global_groups( $groups ) { 
  220. global $wp_object_cache; 
  221.  
  222. $wp_object_cache->add_global_groups( $groups ); 
  223.  
  224. /** 
  225. * Adds a group or set of groups to the list of non-persistent groups. 
  226. * 
  227. * @since 2.6.0 
  228. * 
  229. * @param string|array $groups A group or an array of groups to add. 
  230. */ 
  231. function wp_cache_add_non_persistent_groups( $groups ) { 
  232. // Default cache doesn't persist so nothing to do here. 
  233.  
  234. /** 
  235. * Reset internal cache keys and structures. 
  236. * 
  237. * If the cache back end uses global blog or site IDs as part of its cache keys,  
  238. * this function instructs the back end to reset those keys and perform any cleanup 
  239. * since blog or site IDs have changed since cache init. 
  240. * 
  241. * This function is deprecated. Use wp_cache_switch_to_blog() instead of this 
  242. * function when preparing the cache for a blog switch. For clearing the cache 
  243. * during unit tests, consider using wp_cache_init(). wp_cache_init() is not 
  244. * recommended outside of unit tests as the performance penalty for using it is 
  245. * high. 
  246. * 
  247. * @since 2.6.0 
  248. * @deprecated 3.5.0 WP_Object_Cache::reset() 
  249. * @see WP_Object_Cache::reset() 
  250. * 
  251. * @global WP_Object_Cache $wp_object_cache Object cache global instance. 
  252. */ 
  253. function wp_cache_reset() { 
  254. _deprecated_function( __FUNCTION__, '3.5.0' ); 
  255.  
  256. global $wp_object_cache; 
  257.  
  258. $wp_object_cache->reset(); 
  259.  
  260. /** 
  261. * Core class that implements an object cache. 
  262. * 
  263. * The WordPress Object Cache is used to save on trips to the database. The 
  264. * Object Cache stores all of the cache data to memory and makes the cache 
  265. * contents available by using a key, which is used to name and later retrieve 
  266. * the cache contents. 
  267. * 
  268. * The Object Cache can be replaced by other caching mechanisms by placing files 
  269. * in the wp-content folder which is looked at in wp-settings. If that file 
  270. * exists, then this file will not be included. 
  271. * 
  272. * @package WordPress 
  273. * @subpackage Cache 
  274. * @since 2.0.0 
  275. */ 
  276. class WP_Object_Cache { 
  277.  
  278. /** 
  279. * Holds the cached objects. 
  280. * 
  281. * @since 2.0.0 
  282. * @access private 
  283. * @var array 
  284. */ 
  285. private $cache = array(); 
  286.  
  287. /** 
  288. * The amount of times the cache data was already stored in the cache. 
  289. * 
  290. * @since 2.5.0 
  291. * @access public 
  292. * @var int 
  293. */ 
  294. public $cache_hits = 0; 
  295.  
  296. /** 
  297. * Amount of times the cache did not have the request in cache. 
  298. * 
  299. * @since 2.0.0 
  300. * @access public 
  301. * @var int 
  302. */ 
  303. public $cache_misses = 0; 
  304.  
  305. /** 
  306. * List of global cache groups. 
  307. * 
  308. * @since 3.0.0 
  309. * @access protected 
  310. * @var array 
  311. */ 
  312. protected $global_groups = array(); 
  313.  
  314. /** 
  315. * The blog prefix to prepend to keys in non-global groups. 
  316. * 
  317. * @since 3.5.0 
  318. * @access private 
  319. * @var int 
  320. */ 
  321. private $blog_prefix; 
  322.  
  323. /** 
  324. * Holds the value of is_multisite(). 
  325. * 
  326. * @since 3.5.0 
  327. * @access private 
  328. * @var bool 
  329. */ 
  330. private $multisite; 
  331.  
  332. /** 
  333. * Makes private properties readable for backward compatibility. 
  334. * 
  335. * @since 4.0.0 
  336. * @access public 
  337. * 
  338. * @param string $name Property to get. 
  339. * @return mixed Property. 
  340. */ 
  341. public function __get( $name ) { 
  342. return $this->$name; 
  343.  
  344. /** 
  345. * Makes private properties settable for backward compatibility. 
  346. * 
  347. * @since 4.0.0 
  348. * @access public 
  349. * 
  350. * @param string $name Property to set. 
  351. * @param mixed $value Property value. 
  352. * @return mixed Newly-set property. 
  353. */ 
  354. public function __set( $name, $value ) { 
  355. return $this->$name = $value; 
  356.  
  357. /** 
  358. * Makes private properties checkable for backward compatibility. 
  359. * 
  360. * @since 4.0.0 
  361. * @access public 
  362. * 
  363. * @param string $name Property to check if set. 
  364. * @return bool Whether the property is set. 
  365. */ 
  366. public function __isset( $name ) { 
  367. return isset( $this->$name ); 
  368.  
  369. /** 
  370. * Makes private properties un-settable for backward compatibility. 
  371. * 
  372. * @since 4.0.0 
  373. * @access public 
  374. * 
  375. * @param string $name Property to unset. 
  376. */ 
  377. public function __unset( $name ) { 
  378. unset( $this->$name ); 
  379.  
  380. /** 
  381. * Adds data to the cache if it doesn't already exist. 
  382. * 
  383. * @since 2.0.0 
  384. * @access public 
  385. * 
  386. * @uses WP_Object_Cache::_exists() Checks to see if the cache already has data. 
  387. * @uses WP_Object_Cache::set() Sets the data after the checking the cache 
  388. * contents existence. 
  389. * 
  390. * @param int|string $key What to call the contents in the cache. 
  391. * @param mixed $data The contents to store in the cache. 
  392. * @param string $group Optional. Where to group the cache contents. Default 'default'. 
  393. * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration). 
  394. * @return bool False if cache key and group already exist, true on success 
  395. */ 
  396. public function add( $key, $data, $group = 'default', $expire = 0 ) { 
  397. if ( wp_suspend_cache_addition() ) 
  398. return false; 
  399.  
  400. if ( empty( $group ) ) 
  401. $group = 'default'; 
  402.  
  403. $id = $key; 
  404. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 
  405. $id = $this->blog_prefix . $key; 
  406.  
  407. if ( $this->_exists( $id, $group ) ) 
  408. return false; 
  409.  
  410. return $this->set( $key, $data, $group, (int) $expire ); 
  411.  
  412. /** 
  413. * Sets the list of global cache groups. 
  414. * 
  415. * @since 3.0.0 
  416. * @access public 
  417. * 
  418. * @param array $groups List of groups that are global. 
  419. */ 
  420. public function add_global_groups( $groups ) { 
  421. $groups = (array) $groups; 
  422.  
  423. $groups = array_fill_keys( $groups, true ); 
  424. $this->global_groups = array_merge( $this->global_groups, $groups ); 
  425.  
  426. /** 
  427. * Decrements numeric cache item's value. 
  428. * 
  429. * @since 3.3.0 
  430. * @access public 
  431. * 
  432. * @param int|string $key The cache key to decrement. 
  433. * @param int $offset Optional. The amount by which to decrement the item's value. Default 1. 
  434. * @param string $group Optional. The group the key is in. Default 'default'. 
  435. * @return false|int False on failure, the item's new value on success. 
  436. */ 
  437. public function decr( $key, $offset = 1, $group = 'default' ) { 
  438. if ( empty( $group ) ) 
  439. $group = 'default'; 
  440.  
  441. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 
  442. $key = $this->blog_prefix . $key; 
  443.  
  444. if ( ! $this->_exists( $key, $group ) ) 
  445. return false; 
  446.  
  447. if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) 
  448. $this->cache[ $group ][ $key ] = 0; 
  449.  
  450. $offset = (int) $offset; 
  451.  
  452. $this->cache[ $group ][ $key ] -= $offset; 
  453.  
  454. if ( $this->cache[ $group ][ $key ] < 0 ) 
  455. $this->cache[ $group ][ $key ] = 0; 
  456.  
  457. return $this->cache[ $group ][ $key ]; 
  458.  
  459. /** 
  460. * Removes the contents of the cache key in the group. 
  461. * 
  462. * If the cache key does not exist in the group, then nothing will happen. 
  463. * 
  464. * @since 2.0.0 
  465. * @access public 
  466. * 
  467. * @param int|string $key What the contents in the cache are called. 
  468. * @param string $group Optional. Where the cache contents are grouped. Default 'default'. 
  469. * @param bool $deprecated Optional. Unused. Default false. 
  470. * @return bool False if the contents weren't deleted and true on success. 
  471. */ 
  472. public function delete( $key, $group = 'default', $deprecated = false ) { 
  473. if ( empty( $group ) ) 
  474. $group = 'default'; 
  475.  
  476. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 
  477. $key = $this->blog_prefix . $key; 
  478.  
  479. if ( ! $this->_exists( $key, $group ) ) 
  480. return false; 
  481.  
  482. unset( $this->cache[$group][$key] ); 
  483. return true; 
  484.  
  485. /** 
  486. * Clears the object cache of all data. 
  487. * 
  488. * @since 2.0.0 
  489. * @access public 
  490. * 
  491. * @return true Always returns true. 
  492. */ 
  493. public function flush() { 
  494. $this->cache = array(); 
  495.  
  496. return true; 
  497.  
  498. /** 
  499. * Retrieves the cache contents, if it exists. 
  500. * 
  501. * The contents will be first attempted to be retrieved by searching by the 
  502. * key in the cache group. If the cache is hit (success) then the contents 
  503. * are returned. 
  504. * 
  505. * On failure, the number of cache misses will be incremented. 
  506. * 
  507. * @since 2.0.0 
  508. * @access public 
  509. * 
  510. * @param int|string $key What the contents in the cache are called. 
  511. * @param string $group Optional. Where the cache contents are grouped. Default 'default'. 
  512. * @param string $force Optional. Unused. Whether to force a refetch rather than relying on the local 
  513. * cache. Default false. 
  514. * @param bool $found Optional. Whether the key was found in the cache. Disambiguates a return of 
  515. * false, a storable value. Passed by reference. Default null. 
  516. * @return false|mixed False on failure to retrieve contents or the cache contents on success. 
  517. */ 
  518. public function get( $key, $group = 'default', $force = false, &$found = null ) { 
  519. if ( empty( $group ) ) 
  520. $group = 'default'; 
  521.  
  522. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 
  523. $key = $this->blog_prefix . $key; 
  524.  
  525. if ( $this->_exists( $key, $group ) ) { 
  526. $found = true; 
  527. $this->cache_hits += 1; 
  528. if ( is_object($this->cache[$group][$key]) ) 
  529. return clone $this->cache[$group][$key]; 
  530. else 
  531. return $this->cache[$group][$key]; 
  532.  
  533. $found = false; 
  534. $this->cache_misses += 1; 
  535. return false; 
  536.  
  537. /** 
  538. * Increments numeric cache item's value. 
  539. * 
  540. * @since 3.3.0 
  541. * @access public 
  542. * 
  543. * @param int|string $key The cache key to increment 
  544. * @param int $offset Optional. The amount by which to increment the item's value. Default 1. 
  545. * @param string $group Optional. The group the key is in. Default 'default'. 
  546. * @return false|int False on failure, the item's new value on success. 
  547. */ 
  548. public function incr( $key, $offset = 1, $group = 'default' ) { 
  549. if ( empty( $group ) ) 
  550. $group = 'default'; 
  551.  
  552. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 
  553. $key = $this->blog_prefix . $key; 
  554.  
  555. if ( ! $this->_exists( $key, $group ) ) 
  556. return false; 
  557.  
  558. if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) 
  559. $this->cache[ $group ][ $key ] = 0; 
  560.  
  561. $offset = (int) $offset; 
  562.  
  563. $this->cache[ $group ][ $key ] += $offset; 
  564.  
  565. if ( $this->cache[ $group ][ $key ] < 0 ) 
  566. $this->cache[ $group ][ $key ] = 0; 
  567.  
  568. return $this->cache[ $group ][ $key ]; 
  569.  
  570. /** 
  571. * Replaces the contents in the cache, if contents already exist. 
  572. * 
  573. * @since 2.0.0 
  574. * @access public 
  575. * 
  576. * @see WP_Object_Cache::set() 
  577. * 
  578. * @param int|string $key What to call the contents in the cache. 
  579. * @param mixed $data The contents to store in the cache. 
  580. * @param string $group Optional. Where to group the cache contents. Default 'default'. 
  581. * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration). 
  582. * @return bool False if not exists, true if contents were replaced. 
  583. */ 
  584. public function replace( $key, $data, $group = 'default', $expire = 0 ) { 
  585. if ( empty( $group ) ) 
  586. $group = 'default'; 
  587.  
  588. $id = $key; 
  589. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 
  590. $id = $this->blog_prefix . $key; 
  591.  
  592. if ( ! $this->_exists( $id, $group ) ) 
  593. return false; 
  594.  
  595. return $this->set( $key, $data, $group, (int) $expire ); 
  596.  
  597. /** 
  598. * Resets cache keys. 
  599. * 
  600. * @since 3.0.0 
  601. * @access public 
  602. * 
  603. * @deprecated 3.5.0 Use switch_to_blog() 
  604. * @see switch_to_blog() 
  605. */ 
  606. public function reset() { 
  607. _deprecated_function( __FUNCTION__, '3.5.0', 'switch_to_blog()' ); 
  608.  
  609. // Clear out non-global caches since the blog ID has changed. 
  610. foreach ( array_keys( $this->cache ) as $group ) { 
  611. if ( ! isset( $this->global_groups[ $group ] ) ) 
  612. unset( $this->cache[ $group ] ); 
  613.  
  614. /** 
  615. * Sets the data contents into the cache. 
  616. * 
  617. * The cache contents is grouped by the $group parameter followed by the 
  618. * $key. This allows for duplicate ids in unique groups. Therefore, naming of 
  619. * the group should be used with care and should follow normal function 
  620. * naming guidelines outside of core WordPress usage. 
  621. * 
  622. * The $expire parameter is not used, because the cache will automatically 
  623. * expire for each time a page is accessed and PHP finishes. The method is 
  624. * more for cache plugins which use files. 
  625. * 
  626. * @since 2.0.0 
  627. * @access public 
  628. * 
  629. * @param int|string $key What to call the contents in the cache. 
  630. * @param mixed $data The contents to store in the cache. 
  631. * @param string $group Optional. Where to group the cache contents. Default 'default'. 
  632. * @param int $expire Not Used. 
  633. * @return true Always returns true. 
  634. */ 
  635. public function set( $key, $data, $group = 'default', $expire = 0 ) { 
  636. if ( empty( $group ) ) 
  637. $group = 'default'; 
  638.  
  639. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) 
  640. $key = $this->blog_prefix . $key; 
  641.  
  642. if ( is_object( $data ) ) 
  643. $data = clone $data; 
  644.  
  645. $this->cache[$group][$key] = $data; 
  646. return true; 
  647.  
  648. /** 
  649. * Echoes the stats of the caching. 
  650. * 
  651. * Gives the cache hits, and cache misses. Also prints every cached group,  
  652. * key and the data. 
  653. * 
  654. * @since 2.0.0 
  655. * @access public 
  656. */ 
  657. public function stats() { 
  658. echo "<p>"; 
  659. echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />"; 
  660. echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />"; 
  661. echo "</p>"; 
  662. echo '<ul>'; 
  663. foreach ($this->cache as $group => $cache) { 
  664. echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>'; 
  665. echo '</ul>'; 
  666.  
  667. /** 
  668. * Switches the internal blog ID. 
  669. * 
  670. * This changes the blog ID used to create keys in blog specific groups. 
  671. * 
  672. * @since 3.5.0 
  673. * @access public 
  674. * 
  675. * @param int $blog_id Blog ID. 
  676. */ 
  677. public function switch_to_blog( $blog_id ) { 
  678. $blog_id = (int) $blog_id; 
  679. $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; 
  680.  
  681. /** 
  682. * Serves as a utility function to determine whether a key exists in the cache. 
  683. * 
  684. * @since 3.4.0 
  685. * @access protected 
  686. * 
  687. * @param int|string $key Cache key to check for existence. 
  688. * @param string $group Cache group for the key existence check. 
  689. * @return bool Whether the key exists in the cache for the given group. 
  690. */ 
  691. protected function _exists( $key, $group ) { 
  692. return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) ); 
  693.  
  694. /** 
  695. * Sets up object properties; PHP 5 style constructor. 
  696. * 
  697. * @since 2.0.8 
  698. */ 
  699. public function __construct() { 
  700. $this->multisite = is_multisite(); 
  701. $this->blog_prefix = $this->multisite ? get_current_blog_id() . ':' : ''; 
  702.  
  703.  
  704. /** 
  705. * @todo This should be moved to the PHP4 style constructor, PHP5 
  706. * already calls __destruct() 
  707. */ 
  708. register_shutdown_function( array( $this, '__destruct' ) ); 
  709.  
  710. /** 
  711. * Saves the object cache before object is completely destroyed. 
  712. * 
  713. * Called upon object destruction, which should be when PHP ends. 
  714. * 
  715. * @since 2.0.8 
  716. * 
  717. * @return true Always returns true. 
  718. */ 
  719. public function __destruct() { 
  720. return true; 
.