PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/cache.php

https://bitbucket.org/space87/wordpressstart
PHP | 566 lines | 162 code | 66 blank | 338 comment | 23 complexity | d9d19f52df2dccd1f0773ac4950b3683 MD5 | raw file
  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. * Adds a group or set of groups to the list of global groups.
  165. *
  166. * @since 2.6.0
  167. *
  168. * @param string|array $groups A group or an array of groups to add
  169. */
  170. function wp_cache_add_global_groups( $groups ) {
  171. global $wp_object_cache;
  172. return $wp_object_cache->add_global_groups($groups);
  173. }
  174. /**
  175. * Adds a group or set of groups to the list of non-persistent groups.
  176. *
  177. * @since 2.6.0
  178. *
  179. * @param string|array $groups A group or an array of groups to add
  180. */
  181. function wp_cache_add_non_persistent_groups( $groups ) {
  182. // Default cache doesn't persist so nothing to do here.
  183. return;
  184. }
  185. /**
  186. * Reset internal cache keys and structures. If the cache backend uses global blog or site IDs as part of its cache keys,
  187. * this function instructs the backend to reset those keys and perform any cleanup since blog or site IDs have changed since cache init.
  188. *
  189. * @since 2.6.0
  190. */
  191. function wp_cache_reset() {
  192. global $wp_object_cache;
  193. return $wp_object_cache->reset();
  194. }
  195. /**
  196. * WordPress Object Cache
  197. *
  198. * The WordPress Object Cache is used to save on trips to the database. The
  199. * Object Cache stores all of the cache data to memory and makes the cache
  200. * contents available by using a key, which is used to name and later retrieve
  201. * the cache contents.
  202. *
  203. * The Object Cache can be replaced by other caching mechanisms by placing files
  204. * in the wp-content folder which is looked at in wp-settings. If that file
  205. * exists, then this file will not be included.
  206. *
  207. * @package WordPress
  208. * @subpackage Cache
  209. * @since 2.0
  210. */
  211. class WP_Object_Cache {
  212. /**
  213. * Holds the cached objects
  214. *
  215. * @var array
  216. * @access private
  217. * @since 2.0.0
  218. */
  219. var $cache = array ();
  220. /**
  221. * The amount of times the cache data was already stored in the cache.
  222. *
  223. * @since 2.5.0
  224. * @access private
  225. * @var int
  226. */
  227. var $cache_hits = 0;
  228. /**
  229. * Amount of times the cache did not have the request in cache
  230. *
  231. * @var int
  232. * @access public
  233. * @since 2.0.0
  234. */
  235. var $cache_misses = 0;
  236. /**
  237. * List of global groups
  238. *
  239. * @var array
  240. * @access protected
  241. * @since 3.0.0
  242. */
  243. var $global_groups = array();
  244. /**
  245. * Adds data to the cache if it doesn't already exist.
  246. *
  247. * @uses WP_Object_Cache::_exists Checks to see if the cache already has data.
  248. * @uses WP_Object_Cache::set Sets the data after the checking the cache
  249. * contents existence.
  250. *
  251. * @since 2.0.0
  252. *
  253. * @param int|string $key What to call the contents in the cache
  254. * @param mixed $data The contents to store in the cache
  255. * @param string $group Where to group the cache contents
  256. * @param int $expire When to expire the cache contents
  257. * @return bool False if cache key and group already exist, true on success
  258. */
  259. function add( $key, $data, $group = 'default', $expire = '' ) {
  260. if ( wp_suspend_cache_addition() )
  261. return false;
  262. if ( empty( $group ) )
  263. $group = 'default';
  264. if ( $this->_exists($key, $group) )
  265. return false;
  266. return $this->set($key, $data, $group, $expire);
  267. }
  268. /**
  269. * Sets the list of global groups.
  270. *
  271. * @since 3.0.0
  272. *
  273. * @param array $groups List of groups that are global.
  274. */
  275. function add_global_groups( $groups ) {
  276. $groups = (array) $groups;
  277. $this->global_groups = array_merge($this->global_groups, $groups);
  278. $this->global_groups = array_unique($this->global_groups);
  279. }
  280. /**
  281. * Decrement numeric cache item's value
  282. *
  283. * @since 3.3.0
  284. *
  285. * @param int|string $key The cache key to increment
  286. * @param int $offset The amount by which to decrement the item's value. Default is 1.
  287. * @param string $group The group the key is in.
  288. * @return false|int False on failure, the item's new value on success.
  289. */
  290. function decr( $key, $offset = 1, $group = 'default' ) {
  291. if ( ! $this->_exists( $key, $group ) )
  292. return false;
  293. if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
  294. $this->cache[ $group ][ $key ] = 0;
  295. $offset = (int) $offset;
  296. $this->cache[ $group ][ $key ] -= $offset;
  297. if ( $this->cache[ $group ][ $key ] < 0 )
  298. $this->cache[ $group ][ $key ] = 0;
  299. return $this->cache[ $group ][ $key ];
  300. }
  301. /**
  302. * Remove the contents of the cache key in the group
  303. *
  304. * If the cache key does not exist in the group and $force parameter is set
  305. * to false, then nothing will happen. The $force parameter is set to false
  306. * by default.
  307. *
  308. * @since 2.0.0
  309. *
  310. * @param int|string $key What the contents in the cache are called
  311. * @param string $group Where the cache contents are grouped
  312. * @param bool $force Optional. Whether to force the unsetting of the cache
  313. * key in the group
  314. * @return bool False if the contents weren't deleted and true on success
  315. */
  316. function delete($key, $group = 'default', $force = false) {
  317. if ( empty( $group ) )
  318. $group = 'default';
  319. if ( ! $force && ! $this->_exists( $key, $group ) )
  320. return false;
  321. unset( $this->cache[$group][$key] );
  322. return true;
  323. }
  324. /**
  325. * Clears the object cache of all data
  326. *
  327. * @since 2.0.0
  328. *
  329. * @return bool Always returns true
  330. */
  331. function flush() {
  332. $this->cache = array ();
  333. return true;
  334. }
  335. /**
  336. * Retrieves the cache contents, if it exists
  337. *
  338. * The contents will be first attempted to be retrieved by searching by the
  339. * key in the cache group. If the cache is hit (success) then the contents
  340. * are returned.
  341. *
  342. * On failure, the number of cache misses will be incremented.
  343. *
  344. * @since 2.0.0
  345. *
  346. * @param int|string $key What the contents in the cache are called
  347. * @param string $group Where the cache contents are grouped
  348. * @param string $force Whether to force a refetch rather than relying on the local cache (default is false)
  349. * @return bool|mixed False on failure to retrieve contents or the cache
  350. * contents on success
  351. */
  352. function get( $key, $group = 'default', $force = false, &$found = null ) {
  353. if ( empty( $group ) )
  354. $group = 'default';
  355. if ( $this->_exists( $key, $group ) ) {
  356. $found = true;
  357. $this->cache_hits += 1;
  358. if ( is_object($this->cache[$group][$key]) )
  359. return clone $this->cache[$group][$key];
  360. else
  361. return $this->cache[$group][$key];
  362. }
  363. $found = false;
  364. $this->cache_misses += 1;
  365. return false;
  366. }
  367. /**
  368. * Increment numeric cache item's value
  369. *
  370. * @since 3.3.0
  371. *
  372. * @param int|string $key The cache key to increment
  373. * @param int $offset The amount by which to increment the item's value. Default is 1.
  374. * @param string $group The group the key is in.
  375. * @return false|int False on failure, the item's new value on success.
  376. */
  377. function incr( $key, $offset = 1, $group = 'default' ) {
  378. if ( empty( $group ) )
  379. $group = 'default';
  380. if ( ! $this->_exists( $key, $group ) )
  381. return false;
  382. if ( ! is_numeric( $this->cache[ $group ][ $key ] ) )
  383. $this->cache[ $group ][ $key ] = 0;
  384. $offset = (int) $offset;
  385. $this->cache[ $group ][ $key ] += $offset;
  386. if ( $this->cache[ $group ][ $key ] < 0 )
  387. $this->cache[ $group ][ $key ] = 0;
  388. return $this->cache[ $group ][ $key ];
  389. }
  390. /**
  391. * Replace the contents in the cache, if contents already exist
  392. *
  393. * @since 2.0.0
  394. * @see WP_Object_Cache::set()
  395. *
  396. * @param int|string $key What to call the contents in the cache
  397. * @param mixed $data The contents to store in the cache
  398. * @param string $group Where to group the cache contents
  399. * @param int $expire When to expire the cache contents
  400. * @return bool False if not exists, true if contents were replaced
  401. */
  402. function replace($key, $data, $group = 'default', $expire = '') {
  403. if ( empty( $group ) )
  404. $group = 'default';
  405. if ( ! $this->_exists( $key, $group ) )
  406. return false;
  407. return $this->set($key, $data, $group, $expire);
  408. }
  409. /**
  410. * Reset keys
  411. *
  412. * @since 3.0.0
  413. */
  414. function reset() {
  415. // Clear out non-global caches since the blog ID has changed.
  416. foreach ( array_keys($this->cache) as $group ) {
  417. if ( !in_array($group, $this->global_groups) )
  418. unset($this->cache[$group]);
  419. }
  420. }
  421. /**
  422. * Sets the data contents into the cache
  423. *
  424. * The cache contents is grouped by the $group parameter followed by the
  425. * $key. This allows for duplicate ids in unique groups. Therefore, naming of
  426. * the group should be used with care and should follow normal function
  427. * naming guidelines outside of core WordPress usage.
  428. *
  429. * The $expire parameter is not used, because the cache will automatically
  430. * expire for each time a page is accessed and PHP finishes. The method is
  431. * more for cache plugins which use files.
  432. *
  433. * @since 2.0.0
  434. *
  435. * @param int|string $key What to call the contents in the cache
  436. * @param mixed $data The contents to store in the cache
  437. * @param string $group Where to group the cache contents
  438. * @param int $expire Not Used
  439. * @return bool Always returns true
  440. */
  441. function set($key, $data, $group = 'default', $expire = '') {
  442. if ( empty( $group ) )
  443. $group = 'default';
  444. if ( is_object($data) )
  445. $data = clone $data;
  446. $this->cache[$group][$key] = $data;
  447. return true;
  448. }
  449. /**
  450. * Echoes the stats of the caching.
  451. *
  452. * Gives the cache hits, and cache misses. Also prints every cached group,
  453. * key and the data.
  454. *
  455. * @since 2.0.0
  456. */
  457. function stats() {
  458. echo "<p>";
  459. echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
  460. echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
  461. echo "</p>";
  462. echo '<ul>';
  463. foreach ($this->cache as $group => $cache) {
  464. echo "<li><strong>Group:</strong> $group - ( " . number_format( strlen( serialize( $cache ) ) / 1024, 2 ) . 'k )</li>';
  465. }
  466. echo '</ul>';
  467. }
  468. /**
  469. * Utility function to determine whether a key exists in the cache.
  470. * @access private
  471. */
  472. protected function _exists($key, $group) {
  473. return isset( $this->cache[$group] ) && is_array( $this->cache[$group] ) && array_key_exists( $key, $this->cache[$group] );
  474. }
  475. /**
  476. * Sets up object properties; PHP 5 style constructor
  477. *
  478. * @since 2.0.8
  479. * @return null|WP_Object_Cache If cache is disabled, returns null.
  480. */
  481. function __construct() {
  482. /**
  483. * @todo This should be moved to the PHP4 style constructor, PHP5
  484. * already calls __destruct()
  485. */
  486. register_shutdown_function(array(&$this, "__destruct"));
  487. }
  488. /**
  489. * Will save the object cache before object is completely destroyed.
  490. *
  491. * Called upon object destruction, which should be when PHP ends.
  492. *
  493. * @since 2.0.8
  494. *
  495. * @return bool True value. Won't be used by PHP
  496. */
  497. function __destruct() {
  498. return true;
  499. }
  500. }