cache.php 21 KB

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