PageRenderTime 57ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/cache.php

https://github.com/alx/barceloneta
PHP | 446 lines | 117 code | 46 blank | 283 comment | 13 complexity | f9f07f024b4998343d7d93adc0ffa078 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
  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
  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
  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
  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
  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
  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
  99. * @uses $wp_object_cache Object Cache Class
  100. * @see WP_Object_Cache::replace()
  101. *
  102. * @param int|string $id 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 $id 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
  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. // Default cache doesn't persist so nothing to do here.
  138. return;
  139. }
  140. /**
  141. * Adds a group or set of groups to the list of non-persistent groups.
  142. *
  143. * @since 2.6
  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. * WordPress Object Cache
  153. *
  154. * The WordPress Object Cache is used to save on trips to the database. The
  155. * Object Cache stores all of the cache data to memory and makes the cache
  156. * contents available by using a key, which is used to name and later retrieve
  157. * the cache contents.
  158. *
  159. * The Object Cache can be replaced by other caching mechanisms by placing files
  160. * in the wp-content folder which is looked at in wp-settings. If that file
  161. * exists, then this file will not be included.
  162. *
  163. * @package WordPress
  164. * @subpackage Cache
  165. * @since 2.0
  166. */
  167. class WP_Object_Cache {
  168. /**
  169. * Holds the cached objects
  170. *
  171. * @var array
  172. * @access private
  173. * @since 2.0
  174. */
  175. var $cache = array ();
  176. /**
  177. * Cache objects that do not exist in the cache
  178. *
  179. * @var array
  180. * @access private
  181. * @since 2.0
  182. */
  183. var $non_existant_objects = array ();
  184. /**
  185. * The amount of times the cache data was already stored in the cache.
  186. *
  187. * @since 2.5
  188. * @access private
  189. * @var int
  190. */
  191. var $cache_hits = 0;
  192. /**
  193. * Amount of times the cache did not have the request in cache
  194. *
  195. * @var int
  196. * @access public
  197. * @since 2.0
  198. */
  199. var $cache_misses = 0;
  200. /**
  201. * Adds data to the cache if it doesn't already exist.
  202. *
  203. * @uses WP_Object_Cache::get Checks to see if the cache already has data.
  204. * @uses WP_Object_Cache::set Sets the data after the checking the cache
  205. * contents existance.
  206. *
  207. * @since 2.0
  208. *
  209. * @param int|string $id What to call the contents in the cache
  210. * @param mixed $data The contents to store in the cache
  211. * @param string $group Where to group the cache contents
  212. * @param int $expire When to expire the cache contents
  213. * @return bool False if cache ID and group already exists, true on success
  214. */
  215. function add($id, $data, $group = 'default', $expire = '') {
  216. if (empty ($group))
  217. $group = 'default';
  218. if (false !== $this->get($id, $group, false))
  219. return false;
  220. return $this->set($id, $data, $group, $expire);
  221. }
  222. /**
  223. * Remove the contents of the cache ID in the group
  224. *
  225. * If the cache ID does not exist in the group and $force parameter is set
  226. * to false, then nothing will happen. The $force parameter is set to false
  227. * by default.
  228. *
  229. * On success the group and the id will be added to the
  230. * $non_existant_objects property in the class.
  231. *
  232. * @since 2.0
  233. *
  234. * @param int|string $id What the contents in the cache are called
  235. * @param string $group Where the cache contents are grouped
  236. * @param bool $force Optional. Whether to force the unsetting of the cache
  237. * ID in the group
  238. * @return bool False if the contents weren't deleted and true on success
  239. */
  240. function delete($id, $group = 'default', $force = false) {
  241. if (empty ($group))
  242. $group = 'default';
  243. if (!$force && false === $this->get($id, $group, false))
  244. return false;
  245. unset ($this->cache[$group][$id]);
  246. $this->non_existant_objects[$group][$id] = true;
  247. return true;
  248. }
  249. /**
  250. * Clears the object cache of all data
  251. *
  252. * @since 2.0
  253. *
  254. * @return bool Always returns true
  255. */
  256. function flush() {
  257. $this->cache = array ();
  258. return true;
  259. }
  260. /**
  261. * Retrieves the cache contents, if it exists
  262. *
  263. * The contents will be first attempted to be retrieved by searching by the
  264. * ID in the cache group. If the cache is hit (success) then the contents
  265. * are returned.
  266. *
  267. * On failure, the $non_existant_objects property is checked and if the
  268. * cache group and ID exist in there the cache misses will not be
  269. * incremented. If not in the nonexistant objects property, then the cache
  270. * misses will be incremented and the cache group and ID will be added to
  271. * the nonexistant objects.
  272. *
  273. * @since 2.0
  274. *
  275. * @param int|string $id What the contents in the cache are called
  276. * @param string $group Where the cache contents are grouped
  277. * @return bool|mixed False on failure to retrieve contents or the cache
  278. * contents on success
  279. */
  280. function get($id, $group = 'default') {
  281. if (empty ($group))
  282. $group = 'default';
  283. if (isset ($this->cache[$group][$id])) {
  284. $this->cache_hits += 1;
  285. return $this->cache[$group][$id];
  286. }
  287. if ( isset ($this->non_existant_objects[$group][$id]) )
  288. return false;
  289. $this->non_existant_objects[$group][$id] = true;
  290. $this->cache_misses += 1;
  291. return false;
  292. }
  293. /**
  294. * Replace the contents in the cache, if contents already exist
  295. *
  296. * @since 2.0
  297. * @see WP_Object_Cache::set()
  298. *
  299. * @param int|string $id What to call the contents in the cache
  300. * @param mixed $data The contents to store in the cache
  301. * @param string $group Where to group the cache contents
  302. * @param int $expire When to expire the cache contents
  303. * @return bool False if not exists, true if contents were replaced
  304. */
  305. function replace($id, $data, $group = 'default', $expire = '') {
  306. if (empty ($group))
  307. $group = 'default';
  308. if (false === $this->get($id, $group, false))
  309. return false;
  310. return $this->set($id, $data, $group, $expire);
  311. }
  312. /**
  313. * Sets the data contents into the cache
  314. *
  315. * The cache contents is grouped by the $group parameter followed by the
  316. * $id. This allows for duplicate ids in unique groups. Therefore, naming of
  317. * the group should be used with care and should follow normal function
  318. * naming guidelines outside of core WordPress usage.
  319. *
  320. * The $expire parameter is not used, because the cache will automatically
  321. * expire for each time a page is accessed and PHP finishes. The method is
  322. * more for cache plugins which use files.
  323. *
  324. * @since 2.0
  325. *
  326. * @param int|string $id What to call the contents in the cache
  327. * @param mixed $data The contents to store in the cache
  328. * @param string $group Where to group the cache contents
  329. * @param int $expire Not Used
  330. * @return bool Always returns true
  331. */
  332. function set($id, $data, $group = 'default', $expire = '') {
  333. if (empty ($group))
  334. $group = 'default';
  335. if (NULL === $data)
  336. $data = '';
  337. $this->cache[$group][$id] = $data;
  338. if(isset($this->non_existant_objects[$group][$id]))
  339. unset ($this->non_existant_objects[$group][$id]);
  340. return true;
  341. }
  342. /**
  343. * Echos the stats of the caching.
  344. *
  345. * Gives the cache hits, and cache misses. Also prints every cached group,
  346. * key and the data.
  347. *
  348. * @since 2.0
  349. */
  350. function stats() {
  351. echo "<p>";
  352. echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
  353. echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
  354. echo "</p>";
  355. foreach ($this->cache as $group => $cache) {
  356. echo "<p>";
  357. echo "<strong>Group:</strong> $group<br />";
  358. echo "<strong>Cache:</strong>";
  359. echo "<pre>";
  360. print_r($cache);
  361. echo "</pre>";
  362. }
  363. }
  364. /**
  365. * PHP4 constructor; Calls PHP 5 style constructor
  366. *
  367. * @since 2.0
  368. *
  369. * @return WP_Object_Cache
  370. */
  371. function WP_Object_Cache() {
  372. return $this->__construct();
  373. }
  374. /**
  375. * Sets up object properties; PHP 5 style constructor
  376. *
  377. * @since 2.0.8
  378. * @return null|WP_Object_Cache If cache is disabled, returns null.
  379. */
  380. function __construct() {
  381. /**
  382. * @todo This should be moved to the PHP4 style constructor, PHP5
  383. * already calls __destruct()
  384. */
  385. register_shutdown_function(array(&$this, "__destruct"));
  386. }
  387. /**
  388. * Will save the object cache before object is completely destroyed.
  389. *
  390. * Called upon object destruction, which should be when PHP ends.
  391. *
  392. * @since 2.0.8
  393. *
  394. * @return bool True value. Won't be used by PHP
  395. */
  396. function __destruct() {
  397. return true;
  398. }
  399. }
  400. ?>