PageRenderTime 23ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/public_html/wp-includes/cache.php

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