PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/cache.php

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