Bez popisu

cache.php 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * Object Cache API
  4. *
  5. * @link https://codex.wordpress.org/Class_Reference/WP_Object_Cache
  6. *
  7. * @package WordPress
  8. * @subpackage Cache
  9. */
  10. /** WP_Object_Cache class */
  11. require_once ABSPATH . WPINC . '/class-wp-object-cache.php';
  12. /**
  13. * Adds data to the cache, if the cache key doesn't already exist.
  14. *
  15. * @since 2.0.0
  16. *
  17. * @see WP_Object_Cache::add()
  18. * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  19. *
  20. * @param int|string $key The cache key to use for retrieval later.
  21. * @param mixed $data The data to add to the cache.
  22. * @param string $group Optional. The group to add the cache to. Enables the same key
  23. * to be used across groups. Default empty.
  24. * @param int $expire Optional. When the cache data should expire, in seconds.
  25. * Default 0 (no expiration).
  26. * @return bool True on success, false if cache key and group already exist.
  27. */
  28. function wp_cache_add( $key, $data, $group = '', $expire = 0 ) {
  29. global $wp_object_cache;
  30. return $wp_object_cache->add( $key, $data, $group, (int) $expire );
  31. }
  32. /**
  33. * Closes the cache.
  34. *
  35. * This function has ceased to do anything since WordPress 2.5. The
  36. * functionality was removed along with the rest of the persistent cache.
  37. *
  38. * This does not mean that plugins can't implement this function when they need
  39. * to make sure that the cache is cleaned up after WordPress no longer needs it.
  40. *
  41. * @since 2.0.0
  42. *
  43. * @return true Always returns true.
  44. */
  45. function wp_cache_close() {
  46. return true;
  47. }
  48. /**
  49. * Decrements numeric cache item's value.
  50. *
  51. * @since 3.3.0
  52. *
  53. * @see WP_Object_Cache::decr()
  54. * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  55. *
  56. * @param int|string $key The cache key to decrement.
  57. * @param int $offset Optional. The amount by which to decrement the item's value. Default 1.
  58. * @param string $group Optional. The group the key is in. Default empty.
  59. * @return int|false The item's new value on success, false on failure.
  60. */
  61. function wp_cache_decr( $key, $offset = 1, $group = '' ) {
  62. global $wp_object_cache;
  63. return $wp_object_cache->decr( $key, $offset, $group );
  64. }
  65. /**
  66. * Removes the cache contents matching key and group.
  67. *
  68. * @since 2.0.0
  69. *
  70. * @see WP_Object_Cache::delete()
  71. * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  72. *
  73. * @param int|string $key What the contents in the cache are called.
  74. * @param string $group Optional. Where the cache contents are grouped. Default empty.
  75. * @return bool True on successful removal, false on failure.
  76. */
  77. function wp_cache_delete( $key, $group = '' ) {
  78. global $wp_object_cache;
  79. return $wp_object_cache->delete( $key, $group );
  80. }
  81. /**
  82. * Removes all cache items.
  83. *
  84. * @since 2.0.0
  85. *
  86. * @see WP_Object_Cache::flush()
  87. * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  88. *
  89. * @return bool True on success, false on failure.
  90. */
  91. function wp_cache_flush() {
  92. global $wp_object_cache;
  93. return $wp_object_cache->flush();
  94. }
  95. /**
  96. * Retrieves the cache contents from the cache by key and group.
  97. *
  98. * @since 2.0.0
  99. *
  100. * @see WP_Object_Cache::get()
  101. * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  102. *
  103. * @param int|string $key The key under which the cache contents are stored.
  104. * @param string $group Optional. Where the cache contents are grouped. Default empty.
  105. * @param bool $force Optional. Whether to force an update of the local cache
  106. * from the persistent cache. Default false.
  107. * @param bool $found Optional. Whether the key was found in the cache (passed by reference).
  108. * Disambiguates a return of false, a storable value. Default null.
  109. * @return mixed|false The cache contents on success, false on failure to retrieve contents.
  110. */
  111. function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
  112. global $wp_object_cache;
  113. return $wp_object_cache->get( $key, $group, $force, $found );
  114. }
  115. /**
  116. * Retrieves multiple values from the cache in one call.
  117. *
  118. * @since 5.5.0
  119. *
  120. * @see WP_Object_Cache::get_multiple()
  121. * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  122. *
  123. * @param array $keys Array of keys under which the cache contents are stored.
  124. * @param string $group Optional. Where the cache contents are grouped. Default empty.
  125. * @param bool $force Optional. Whether to force an update of the local cache
  126. * from the persistent cache. Default false.
  127. * @return array Array of values organized into groups.
  128. */
  129. function wp_cache_get_multiple( $keys, $group = '', $force = false ) {
  130. global $wp_object_cache;
  131. return $wp_object_cache->get_multiple( $keys, $group, $force );
  132. }
  133. /**
  134. * Increment numeric cache item's value
  135. *
  136. * @since 3.3.0
  137. *
  138. * @see WP_Object_Cache::incr()
  139. * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  140. *
  141. * @param int|string $key The key for the cache contents that should be incremented.
  142. * @param int $offset Optional. The amount by which to increment the item's value. Default 1.
  143. * @param string $group Optional. The group the key is in. Default empty.
  144. * @return int|false The item's new value on success, false on failure.
  145. */
  146. function wp_cache_incr( $key, $offset = 1, $group = '' ) {
  147. global $wp_object_cache;
  148. return $wp_object_cache->incr( $key, $offset, $group );
  149. }
  150. /**
  151. * Sets up Object Cache Global and assigns it.
  152. *
  153. * @since 2.0.0
  154. *
  155. * @global WP_Object_Cache $wp_object_cache
  156. */
  157. function wp_cache_init() {
  158. $GLOBALS['wp_object_cache'] = new WP_Object_Cache();
  159. }
  160. /**
  161. * Replaces the contents of the cache with new data.
  162. *
  163. * @since 2.0.0
  164. *
  165. * @see WP_Object_Cache::replace()
  166. * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  167. *
  168. * @param int|string $key The key for the cache data that should be replaced.
  169. * @param mixed $data The new data to store in the cache.
  170. * @param string $group Optional. The group for the cache data that should be replaced.
  171. * Default empty.
  172. * @param int $expire Optional. When to expire the cache contents, in seconds.
  173. * Default 0 (no expiration).
  174. * @return bool False if original value does not exist, true if contents were replaced
  175. */
  176. function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) {
  177. global $wp_object_cache;
  178. return $wp_object_cache->replace( $key, $data, $group, (int) $expire );
  179. }
  180. /**
  181. * Saves the data to the cache.
  182. *
  183. * Differs from wp_cache_add() and wp_cache_replace() in that it will always write data.
  184. *
  185. * @since 2.0.0
  186. *
  187. * @see WP_Object_Cache::set()
  188. * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  189. *
  190. * @param int|string $key The cache key to use for retrieval later.
  191. * @param mixed $data The contents to store in the cache.
  192. * @param string $group Optional. Where to group the cache contents. Enables the same key
  193. * to be used across groups. Default empty.
  194. * @param int $expire Optional. When to expire the cache contents, in seconds.
  195. * Default 0 (no expiration).
  196. * @return bool True on success, false on failure.
  197. */
  198. function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
  199. global $wp_object_cache;
  200. return $wp_object_cache->set( $key, $data, $group, (int) $expire );
  201. }
  202. /**
  203. * Switches the internal blog ID.
  204. *
  205. * This changes the blog id used to create keys in blog specific groups.
  206. *
  207. * @since 3.5.0
  208. *
  209. * @see WP_Object_Cache::switch_to_blog()
  210. * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  211. *
  212. * @param int $blog_id Site ID.
  213. */
  214. function wp_cache_switch_to_blog( $blog_id ) {
  215. global $wp_object_cache;
  216. $wp_object_cache->switch_to_blog( $blog_id );
  217. }
  218. /**
  219. * Adds a group or set of groups to the list of global groups.
  220. *
  221. * @since 2.6.0
  222. *
  223. * @see WP_Object_Cache::add_global_groups()
  224. * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  225. *
  226. * @param string|string[] $groups A group or an array of groups to add.
  227. */
  228. function wp_cache_add_global_groups( $groups ) {
  229. global $wp_object_cache;
  230. $wp_object_cache->add_global_groups( $groups );
  231. }
  232. /**
  233. * Adds a group or set of groups to the list of non-persistent groups.
  234. *
  235. * @since 2.6.0
  236. *
  237. * @param string|string[] $groups A group or an array of groups to add.
  238. */
  239. function wp_cache_add_non_persistent_groups( $groups ) {
  240. // Default cache doesn't persist so nothing to do here.
  241. }
  242. /**
  243. * Reset internal cache keys and structures.
  244. *
  245. * If the cache back end uses global blog or site IDs as part of its cache keys,
  246. * this function instructs the back end to reset those keys and perform any cleanup
  247. * since blog or site IDs have changed since cache init.
  248. *
  249. * This function is deprecated. Use wp_cache_switch_to_blog() instead of this
  250. * function when preparing the cache for a blog switch. For clearing the cache
  251. * during unit tests, consider using wp_cache_init(). wp_cache_init() is not
  252. * recommended outside of unit tests as the performance penalty for using it is
  253. * high.
  254. *
  255. * @since 2.6.0
  256. * @deprecated 3.5.0 WP_Object_Cache::reset()
  257. * @see WP_Object_Cache::reset()
  258. *
  259. * @global WP_Object_Cache $wp_object_cache Object cache global instance.
  260. */
  261. function wp_cache_reset() {
  262. _deprecated_function( __FUNCTION__, '3.5.0', 'WP_Object_Cache::reset()' );
  263. global $wp_object_cache;
  264. $wp_object_cache->reset();
  265. }