PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/cache.php

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