PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-includes/cache.php

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