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

/htdocs/wp-includes/class-wp-object-cache.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 475 lines | 175 code | 58 blank | 242 comment | 38 complexity | 761e6870f930716540f98c856360726e MD5 | raw file
  1. <?php
  2. /**
  3. * Object Cache API: WP_Object_Cache class
  4. *
  5. * @package WordPress
  6. * @subpackage Cache
  7. * @since 5.4.0
  8. */
  9. /**
  10. * Core class that implements an object cache.
  11. *
  12. * The WordPress Object Cache is used to save on trips to the database. The
  13. * Object Cache stores all of the cache data to memory and makes the cache
  14. * contents available by using a key, which is used to name and later retrieve
  15. * the cache contents.
  16. *
  17. * The Object Cache can be replaced by other caching mechanisms by placing files
  18. * in the wp-content folder which is looked at in wp-settings. If that file
  19. * exists, then this file will not be included.
  20. *
  21. * @since 2.0.0
  22. */
  23. class WP_Object_Cache {
  24. /**
  25. * Holds the cached objects.
  26. *
  27. * @since 2.0.0
  28. * @var array
  29. */
  30. private $cache = array();
  31. /**
  32. * The amount of times the cache data was already stored in the cache.
  33. *
  34. * @since 2.5.0
  35. * @var int
  36. */
  37. public $cache_hits = 0;
  38. /**
  39. * Amount of times the cache did not have the request in cache.
  40. *
  41. * @since 2.0.0
  42. * @var int
  43. */
  44. public $cache_misses = 0;
  45. /**
  46. * List of global cache groups.
  47. *
  48. * @since 3.0.0
  49. * @var array
  50. */
  51. protected $global_groups = array();
  52. /**
  53. * The blog prefix to prepend to keys in non-global groups.
  54. *
  55. * @since 3.5.0
  56. * @var string
  57. */
  58. private $blog_prefix;
  59. /**
  60. * Holds the value of is_multisite().
  61. *
  62. * @since 3.5.0
  63. * @var bool
  64. */
  65. private $multisite;
  66. /**
  67. * Sets up object properties; PHP 5 style constructor.
  68. *
  69. * @since 2.0.8
  70. */
  71. public function __construct() {
  72. $this->multisite = is_multisite();
  73. $this->blog_prefix = $this->multisite ? get_current_blog_id() . ':' : '';
  74. }
  75. /**
  76. * Makes private properties readable for backward compatibility.
  77. *
  78. * @since 4.0.0
  79. *
  80. * @param string $name Property to get.
  81. * @return mixed Property.
  82. */
  83. public function __get( $name ) {
  84. return $this->$name;
  85. }
  86. /**
  87. * Makes private properties settable for backward compatibility.
  88. *
  89. * @since 4.0.0
  90. *
  91. * @param string $name Property to set.
  92. * @param mixed $value Property value.
  93. * @return mixed Newly-set property.
  94. */
  95. public function __set( $name, $value ) {
  96. return $this->$name = $value;
  97. }
  98. /**
  99. * Makes private properties checkable for backward compatibility.
  100. *
  101. * @since 4.0.0
  102. *
  103. * @param string $name Property to check if set.
  104. * @return bool Whether the property is set.
  105. */
  106. public function __isset( $name ) {
  107. return isset( $this->$name );
  108. }
  109. /**
  110. * Makes private properties un-settable for backward compatibility.
  111. *
  112. * @since 4.0.0
  113. *
  114. * @param string $name Property to unset.
  115. */
  116. public function __unset( $name ) {
  117. unset( $this->$name );
  118. }
  119. /**
  120. * Adds data to the cache if it doesn't already exist.
  121. *
  122. * @since 2.0.0
  123. *
  124. * @uses WP_Object_Cache::_exists() Checks to see if the cache already has data.
  125. * @uses WP_Object_Cache::set() Sets the data after the checking the cache
  126. * contents existence.
  127. *
  128. * @param int|string $key What to call the contents in the cache.
  129. * @param mixed $data The contents to store in the cache.
  130. * @param string $group Optional. Where to group the cache contents. Default 'default'.
  131. * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration).
  132. * @return bool True on success, false if cache key and group already exist.
  133. */
  134. public function add( $key, $data, $group = 'default', $expire = 0 ) {
  135. if ( wp_suspend_cache_addition() ) {
  136. return false;
  137. }
  138. if ( empty( $group ) ) {
  139. $group = 'default';
  140. }
  141. $id = $key;
  142. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
  143. $id = $this->blog_prefix . $key;
  144. }
  145. if ( $this->_exists( $id, $group ) ) {
  146. return false;
  147. }
  148. return $this->set( $key, $data, $group, (int) $expire );
  149. }
  150. /**
  151. * Sets the list of global cache groups.
  152. *
  153. * @since 3.0.0
  154. *
  155. * @param array $groups List of groups that are global.
  156. */
  157. public function add_global_groups( $groups ) {
  158. $groups = (array) $groups;
  159. $groups = array_fill_keys( $groups, true );
  160. $this->global_groups = array_merge( $this->global_groups, $groups );
  161. }
  162. /**
  163. * Decrements numeric cache item's value.
  164. *
  165. * @since 3.3.0
  166. *
  167. * @param int|string $key The cache key to decrement.
  168. * @param int $offset Optional. The amount by which to decrement the item's value. Default 1.
  169. * @param string $group Optional. The group the key is in. Default 'default'.
  170. * @return int|false The item's new value on success, false on failure.
  171. */
  172. public function decr( $key, $offset = 1, $group = 'default' ) {
  173. if ( empty( $group ) ) {
  174. $group = 'default';
  175. }
  176. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
  177. $key = $this->blog_prefix . $key;
  178. }
  179. if ( ! $this->_exists( $key, $group ) ) {
  180. return false;
  181. }
  182. if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) {
  183. $this->cache[ $group ][ $key ] = 0;
  184. }
  185. $offset = (int) $offset;
  186. $this->cache[ $group ][ $key ] -= $offset;
  187. if ( $this->cache[ $group ][ $key ] < 0 ) {
  188. $this->cache[ $group ][ $key ] = 0;
  189. }
  190. return $this->cache[ $group ][ $key ];
  191. }
  192. /**
  193. * Removes the contents of the cache key in the group.
  194. *
  195. * If the cache key does not exist in the group, then nothing will happen.
  196. *
  197. * @since 2.0.0
  198. *
  199. * @param int|string $key What the contents in the cache are called.
  200. * @param string $group Optional. Where the cache contents are grouped. Default 'default'.
  201. * @param bool $deprecated Optional. Unused. Default false.
  202. * @return bool False if the contents weren't deleted and true on success.
  203. */
  204. public function delete( $key, $group = 'default', $deprecated = false ) {
  205. if ( empty( $group ) ) {
  206. $group = 'default';
  207. }
  208. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
  209. $key = $this->blog_prefix . $key;
  210. }
  211. if ( ! $this->_exists( $key, $group ) ) {
  212. return false;
  213. }
  214. unset( $this->cache[ $group ][ $key ] );
  215. return true;
  216. }
  217. /**
  218. * Clears the object cache of all data.
  219. *
  220. * @since 2.0.0
  221. *
  222. * @return true Always returns true.
  223. */
  224. public function flush() {
  225. $this->cache = array();
  226. return true;
  227. }
  228. /**
  229. * Retrieves the cache contents, if it exists.
  230. *
  231. * The contents will be first attempted to be retrieved by searching by the
  232. * key in the cache group. If the cache is hit (success) then the contents
  233. * are returned.
  234. *
  235. * On failure, the number of cache misses will be incremented.
  236. *
  237. * @since 2.0.0
  238. *
  239. * @param int|string $key What the contents in the cache are called.
  240. * @param string $group Optional. Where the cache contents are grouped. Default 'default'.
  241. * @param bool $force Optional. Unused. Whether to force a refetch rather than relying on the local
  242. * cache. Default false.
  243. * @param bool $found Optional. Whether the key was found in the cache (passed by reference).
  244. * Disambiguates a return of false, a storable value. Default null.
  245. * @return mixed|false The cache contents on success, false on failure to retrieve contents.
  246. */
  247. public function get( $key, $group = 'default', $force = false, &$found = null ) {
  248. if ( empty( $group ) ) {
  249. $group = 'default';
  250. }
  251. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
  252. $key = $this->blog_prefix . $key;
  253. }
  254. if ( $this->_exists( $key, $group ) ) {
  255. $found = true;
  256. $this->cache_hits += 1;
  257. if ( is_object( $this->cache[ $group ][ $key ] ) ) {
  258. return clone $this->cache[ $group ][ $key ];
  259. } else {
  260. return $this->cache[ $group ][ $key ];
  261. }
  262. }
  263. $found = false;
  264. $this->cache_misses += 1;
  265. return false;
  266. }
  267. /**
  268. * Increments numeric cache item's value.
  269. *
  270. * @since 3.3.0
  271. *
  272. * @param int|string $key The cache key to increment
  273. * @param int $offset Optional. The amount by which to increment the item's value. Default 1.
  274. * @param string $group Optional. The group the key is in. Default 'default'.
  275. * @return int|false The item's new value on success, false on failure.
  276. */
  277. public function incr( $key, $offset = 1, $group = 'default' ) {
  278. if ( empty( $group ) ) {
  279. $group = 'default';
  280. }
  281. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
  282. $key = $this->blog_prefix . $key;
  283. }
  284. if ( ! $this->_exists( $key, $group ) ) {
  285. return false;
  286. }
  287. if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) {
  288. $this->cache[ $group ][ $key ] = 0;
  289. }
  290. $offset = (int) $offset;
  291. $this->cache[ $group ][ $key ] += $offset;
  292. if ( $this->cache[ $group ][ $key ] < 0 ) {
  293. $this->cache[ $group ][ $key ] = 0;
  294. }
  295. return $this->cache[ $group ][ $key ];
  296. }
  297. /**
  298. * Replaces the contents in the cache, if contents already exist.
  299. *
  300. * @since 2.0.0
  301. *
  302. * @see WP_Object_Cache::set()
  303. *
  304. * @param int|string $key What to call the contents in the cache.
  305. * @param mixed $data The contents to store in the cache.
  306. * @param string $group Optional. Where to group the cache contents. Default 'default'.
  307. * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration).
  308. * @return bool False if not exists, true if contents were replaced.
  309. */
  310. public function replace( $key, $data, $group = 'default', $expire = 0 ) {
  311. if ( empty( $group ) ) {
  312. $group = 'default';
  313. }
  314. $id = $key;
  315. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
  316. $id = $this->blog_prefix . $key;
  317. }
  318. if ( ! $this->_exists( $id, $group ) ) {
  319. return false;
  320. }
  321. return $this->set( $key, $data, $group, (int) $expire );
  322. }
  323. /**
  324. * Resets cache keys.
  325. *
  326. * @since 3.0.0
  327. *
  328. * @deprecated 3.5.0 Use switch_to_blog()
  329. * @see switch_to_blog()
  330. */
  331. public function reset() {
  332. _deprecated_function( __FUNCTION__, '3.5.0', 'switch_to_blog()' );
  333. // Clear out non-global caches since the blog ID has changed.
  334. foreach ( array_keys( $this->cache ) as $group ) {
  335. if ( ! isset( $this->global_groups[ $group ] ) ) {
  336. unset( $this->cache[ $group ] );
  337. }
  338. }
  339. }
  340. /**
  341. * Sets the data contents into the cache.
  342. *
  343. * The cache contents are grouped by the $group parameter followed by the
  344. * $key. This allows for duplicate ids in unique groups. Therefore, naming of
  345. * the group should be used with care and should follow normal function
  346. * naming guidelines outside of core WordPress usage.
  347. *
  348. * The $expire parameter is not used, because the cache will automatically
  349. * expire for each time a page is accessed and PHP finishes. The method is
  350. * more for cache plugins which use files.
  351. *
  352. * @since 2.0.0
  353. *
  354. * @param int|string $key What to call the contents in the cache.
  355. * @param mixed $data The contents to store in the cache.
  356. * @param string $group Optional. Where to group the cache contents. Default 'default'.
  357. * @param int $expire Not Used.
  358. * @return true Always returns true.
  359. */
  360. public function set( $key, $data, $group = 'default', $expire = 0 ) {
  361. if ( empty( $group ) ) {
  362. $group = 'default';
  363. }
  364. if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
  365. $key = $this->blog_prefix . $key;
  366. }
  367. if ( is_object( $data ) ) {
  368. $data = clone $data;
  369. }
  370. $this->cache[ $group ][ $key ] = $data;
  371. return true;
  372. }
  373. /**
  374. * Echoes the stats of the caching.
  375. *
  376. * Gives the cache hits, and cache misses. Also prints every cached group,
  377. * key and the data.
  378. *
  379. * @since 2.0.0
  380. */
  381. public function stats() {
  382. echo '<p>';
  383. echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />";
  384. echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />";
  385. echo '</p>';
  386. echo '<ul>';
  387. foreach ( $this->cache as $group => $cache ) {
  388. echo '<li><strong>Group:</strong> ' . esc_html( $group ) . ' - ( ' . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>';
  389. }
  390. echo '</ul>';
  391. }
  392. /**
  393. * Switches the internal blog ID.
  394. *
  395. * This changes the blog ID used to create keys in blog specific groups.
  396. *
  397. * @since 3.5.0
  398. *
  399. * @param int $blog_id Blog ID.
  400. */
  401. public function switch_to_blog( $blog_id ) {
  402. $blog_id = (int) $blog_id;
  403. $this->blog_prefix = $this->multisite ? $blog_id . ':' : '';
  404. }
  405. /**
  406. * Serves as a utility function to determine whether a key exists in the cache.
  407. *
  408. * @since 3.4.0
  409. *
  410. * @param int|string $key Cache key to check for existence.
  411. * @param string $group Cache group for the key existence check.
  412. * @return bool Whether the key exists in the cache for the given group.
  413. */
  414. protected function _exists( $key, $group ) {
  415. return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) );
  416. }
  417. }