cache.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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 int
  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. if ( empty( $group ) )
  367. $group = 'default';
  368. $id = $key;
  369. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
  370. $id = $this->blog_prefix . $key;
  371. if ( $this->_exists( $id, $group ) )
  372. return false;
  373. return $this->set( $key, $data, $group, (int) $expire );
  374. }
  375. /**
  376. * Sets the list of global cache groups.
  377. *
  378. * @since 3.0.0
  379. *
  380. * @param array $groups List of groups that are global.
  381. */
  382. public function add_global_groups( $groups ) {
  383. $groups = (array) $groups;
  384. $groups = array_fill_keys( $groups, true );
  385. $this->global_groups = array_merge( $this->global_groups, $groups );
  386. }
  387. /**
  388. * Decrements numeric cache item's value.
  389. *
  390. * @since 3.3.0
  391. *
  392. * @param int|string $key The cache key to decrement.
  393. * @param int $offset Optional. The amount by which to decrement the item's value. Default 1.
  394. * @param string $group Optional. The group the key is in. Default 'default'.
  395. * @return false|int False on failure, the item's new value on success.
  396. */
  397. public function decr( $key, $offset = 1, $group = 'default' ) {
  398. if ( empty( $group ) )
  399. $group = 'default';
  400. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
  401. $key = $this->blog_prefix . $key;
  402. if ( ! $this->_exists( $key, $group ) )
  403. return false;
  404. if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
  405. $this->cache[ $group ][ $key ] = 0;
  406. $offset = (int) $offset;
  407. $this->cache[ $group ][ $key ] -= $offset;
  408. if ( $this->cache[ $group ][ $key ] < 0 )
  409. $this->cache[ $group ][ $key ] = 0;
  410. return $this->cache[ $group ][ $key ];
  411. }
  412. /**
  413. * Removes the contents of the cache key in the group.
  414. *
  415. * If the cache key does not exist in the group, then nothing will happen.
  416. *
  417. * @since 2.0.0
  418. *
  419. * @param int|string $key What the contents in the cache are called.
  420. * @param string $group Optional. Where the cache contents are grouped. Default 'default'.
  421. * @param bool $deprecated Optional. Unused. Default false.
  422. * @return bool False if the contents weren't deleted and true on success.
  423. */
  424. public function delete( $key, $group = 'default', $deprecated = false ) {
  425. if ( empty( $group ) )
  426. $group = 'default';
  427. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
  428. $key = $this->blog_prefix . $key;
  429. if ( ! $this->_exists( $key, $group ) )
  430. return false;
  431. unset( $this->cache[$group][$key] );
  432. return true;
  433. }
  434. /**
  435. * Clears the object cache of all data.
  436. *
  437. * @since 2.0.0
  438. *
  439. * @return true Always returns true.
  440. */
  441. public function flush() {
  442. $this->cache = array();
  443. return true;
  444. }
  445. /**
  446. * Retrieves the cache contents, if it exists.
  447. *
  448. * The contents will be first attempted to be retrieved by searching by the
  449. * key in the cache group. If the cache is hit (success) then the contents
  450. * are returned.
  451. *
  452. * On failure, the number of cache misses will be incremented.
  453. *
  454. * @since 2.0.0
  455. *
  456. * @param int|string $key What the contents in the cache are called.
  457. * @param string $group Optional. Where the cache contents are grouped. Default 'default'.
  458. * @param string $force Optional. Unused. Whether to force a refetch rather than relying on the local
  459. * cache. Default false.
  460. * @param bool $found Optional. Whether the key was found in the cache (passed by reference).
  461. * Disambiguates a return of false, a storable value. Default null.
  462. * @return false|mixed False on failure to retrieve contents or the cache contents on success.
  463. */
  464. public function get( $key, $group = 'default', $force = false, &$found = null ) {
  465. if ( empty( $group ) )
  466. $group = 'default';
  467. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
  468. $key = $this->blog_prefix . $key;
  469. if ( $this->_exists( $key, $group ) ) {
  470. $found = true;
  471. $this->cache_hits += 1;
  472. if ( is_object($this->cache[$group][$key]) )
  473. return clone $this->cache[$group][$key];
  474. else
  475. return $this->cache[$group][$key];
  476. }
  477. $found = false;
  478. $this->cache_misses += 1;
  479. return false;
  480. }
  481. /**
  482. * Increments numeric cache item's value.
  483. *
  484. * @since 3.3.0
  485. *
  486. * @param int|string $key The cache key to increment
  487. * @param int $offset Optional. The amount by which to increment the item's value. Default 1.
  488. * @param string $group Optional. The group the key is in. Default 'default'.
  489. * @return false|int False on failure, the item's new value on success.
  490. */
  491. public function incr( $key, $offset = 1, $group = 'default' ) {
  492. if ( empty( $group ) )
  493. $group = 'default';
  494. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
  495. $key = $this->blog_prefix . $key;
  496. if ( ! $this->_exists( $key, $group ) )
  497. return false;
  498. if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
  499. $this->cache[ $group ][ $key ] = 0;
  500. $offset = (int) $offset;
  501. $this->cache[ $group ][ $key ] += $offset;
  502. if ( $this->cache[ $group ][ $key ] < 0 )
  503. $this->cache[ $group ][ $key ] = 0;
  504. return $this->cache[ $group ][ $key ];
  505. }
  506. /**
  507. * Replaces the contents in the cache, if contents already exist.
  508. *
  509. * @since 2.0.0
  510. *
  511. * @see WP_Object_Cache::set()
  512. *
  513. * @param int|string $key What to call the contents in the cache.
  514. * @param mixed $data The contents to store in the cache.
  515. * @param string $group Optional. Where to group the cache contents. Default 'default'.
  516. * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration).
  517. * @return bool False if not exists, true if contents were replaced.
  518. */
  519. public function replace( $key, $data, $group = 'default', $expire = 0 ) {
  520. if ( empty( $group ) )
  521. $group = 'default';
  522. $id = $key;
  523. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
  524. $id = $this->blog_prefix . $key;
  525. if ( ! $this->_exists( $id, $group ) )
  526. return false;
  527. return $this->set( $key, $data, $group, (int) $expire );
  528. }
  529. /**
  530. * Resets cache keys.
  531. *
  532. * @since 3.0.0
  533. *
  534. * @deprecated 3.5.0 Use switch_to_blog()
  535. * @see switch_to_blog()
  536. */
  537. public function reset() {
  538. _deprecated_function( __FUNCTION__, '3.5.0', 'switch_to_blog()' );
  539. // Clear out non-global caches since the blog ID has changed.
  540. foreach ( array_keys( $this->cache ) as $group ) {
  541. if ( ! isset( $this->global_groups[ $group ] ) )
  542. unset( $this->cache[ $group ] );
  543. }
  544. }
  545. /**
  546. * Sets the data contents into the cache.
  547. *
  548. * The cache contents is grouped by the $group parameter followed by the
  549. * $key. This allows for duplicate ids in unique groups. Therefore, naming of
  550. * the group should be used with care and should follow normal function
  551. * naming guidelines outside of core WordPress usage.
  552. *
  553. * The $expire parameter is not used, because the cache will automatically
  554. * expire for each time a page is accessed and PHP finishes. The method is
  555. * more for cache plugins which use files.
  556. *
  557. * @since 2.0.0
  558. *
  559. * @param int|string $key What to call the contents in the cache.
  560. * @param mixed $data The contents to store in the cache.
  561. * @param string $group Optional. Where to group the cache contents. Default 'default'.
  562. * @param int $expire Not Used.
  563. * @return true Always returns true.
  564. */
  565. public function set( $key, $data, $group = 'default', $expire = 0 ) {
  566. if ( empty( $group ) )
  567. $group = 'default';
  568. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) )
  569. $key = $this->blog_prefix . $key;
  570. if ( is_object( $data ) )
  571. $data = clone $data;
  572. $this->cache[$group][$key] = $data;
  573. return true;
  574. }
  575. /**
  576. * Echoes the stats of the caching.
  577. *
  578. * Gives the cache hits, and cache misses. Also prints every cached group,
  579. * key and the data.
  580. *
  581. * @since 2.0.0
  582. */
  583. public function stats() {
  584. echo "<p>";
  585. echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
  586. echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
  587. echo "</p>";
  588. echo '<ul>';
  589. foreach ($this->cache as $group => $cache) {
  590. echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>';
  591. }
  592. echo '</ul>';
  593. }
  594. /**
  595. * Switches the internal blog ID.
  596. *
  597. * This changes the blog ID used to create keys in blog specific groups.
  598. *
  599. * @since 3.5.0
  600. *
  601. * @param int $blog_id Blog ID.
  602. */
  603. public function switch_to_blog( $blog_id ) {
  604. $blog_id = (int) $blog_id;
  605. $this->blog_prefix = $this->multisite ? $blog_id . ':' : '';
  606. }
  607. /**
  608. * Serves as a utility function to determine whether a key exists in the cache.
  609. *
  610. * @since 3.4.0
  611. *
  612. * @param int|string $key Cache key to check for existence.
  613. * @param string $group Cache group for the key existence check.
  614. * @return bool Whether the key exists in the cache for the given group.
  615. */
  616. protected function _exists( $key, $group ) {
  617. return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) );
  618. }
  619. /**
  620. * Sets up object properties; PHP 5 style constructor.
  621. *
  622. * @since 2.0.8
  623. */
  624. public function __construct() {
  625. $this->multisite = is_multisite();
  626. $this->blog_prefix = $this->multisite ? get_current_blog_id() . ':' : '';
  627. /**
  628. * @todo This should be moved to the PHP4 style constructor, PHP5
  629. * already calls __destruct()
  630. */
  631. register_shutdown_function( array( $this, '__destruct' ) );
  632. }
  633. /**
  634. * Saves the object cache before object is completely destroyed.
  635. *
  636. * Called upon object destruction, which should be when PHP ends.
  637. *
  638. * @since 2.0.8
  639. *
  640. * @return true Always returns true.
  641. */
  642. public function __destruct() {
  643. return true;
  644. }
  645. }