author | ymh <ymh.work@gmail.com> |
Tue, 27 Sep 2022 16:37:53 +0200 | |
changeset 19 | 3d72ae0968f4 |
parent 18 | be944660c56a |
child 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* Object Cache API |
|
4 |
* |
|
19 | 5 |
* @link https://developer.wordpress.org/reference/classes/wp_object_cache/ |
0 | 6 |
* |
7 |
* @package WordPress |
|
8 |
* @subpackage Cache |
|
9 |
*/ |
|
10 |
||
16 | 11 |
/** WP_Object_Cache class */ |
12 |
require_once ABSPATH . WPINC . '/class-wp-object-cache.php'; |
|
13 |
||
0 | 14 |
/** |
19 | 15 |
* Sets up Object Cache Global and assigns it. |
16 |
* |
|
17 |
* @since 2.0.0 |
|
18 |
* |
|
19 |
* @global WP_Object_Cache $wp_object_cache |
|
20 |
*/ |
|
21 |
function wp_cache_init() { |
|
22 |
$GLOBALS['wp_object_cache'] = new WP_Object_Cache(); |
|
23 |
} |
|
24 |
||
25 |
/** |
|
0 | 26 |
* Adds data to the cache, if the cache key doesn't already exist. |
27 |
* |
|
28 |
* @since 2.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
29 |
* |
0 | 30 |
* @see WP_Object_Cache::add() |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
31 |
* @global WP_Object_Cache $wp_object_cache Object cache global instance. |
0 | 32 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
33 |
* @param int|string $key The cache key to use for retrieval later. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
34 |
* @param mixed $data The data to add to the cache. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
35 |
* @param string $group Optional. The group to add the cache to. Enables the same key |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
36 |
* to be used across groups. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
37 |
* @param int $expire Optional. When the cache data should expire, in seconds. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
38 |
* Default 0 (no expiration). |
16 | 39 |
* @return bool True on success, false if cache key and group already exist. |
0 | 40 |
*/ |
41 |
function wp_cache_add( $key, $data, $group = '', $expire = 0 ) { |
|
42 |
global $wp_object_cache; |
|
43 |
||
44 |
return $wp_object_cache->add( $key, $data, $group, (int) $expire ); |
|
45 |
} |
|
46 |
||
47 |
/** |
|
19 | 48 |
* Adds multiple values to the cache in one call. |
49 |
* |
|
50 |
* @since 6.0.0 |
|
51 |
* |
|
52 |
* @see WP_Object_Cache::add_multiple() |
|
53 |
* @global WP_Object_Cache $wp_object_cache Object cache global instance. |
|
0 | 54 |
* |
19 | 55 |
* @param array $data Array of keys and values to be set. |
56 |
* @param string $group Optional. Where the cache contents are grouped. Default empty. |
|
57 |
* @param int $expire Optional. When to expire the cache contents, in seconds. |
|
58 |
* Default 0 (no expiration). |
|
59 |
* @return bool[] Array of return values, grouped by key. Each value is either |
|
60 |
* true on success, or false if cache key and group already exist. |
|
61 |
*/ |
|
62 |
function wp_cache_add_multiple( array $data, $group = '', $expire = 0 ) { |
|
63 |
global $wp_object_cache; |
|
64 |
||
65 |
return $wp_object_cache->add_multiple( $data, $group, $expire ); |
|
66 |
} |
|
67 |
||
68 |
/** |
|
69 |
* Replaces the contents of the cache with new data. |
|
0 | 70 |
* |
71 |
* @since 2.0.0 |
|
72 |
* |
|
19 | 73 |
* @see WP_Object_Cache::replace() |
74 |
* @global WP_Object_Cache $wp_object_cache Object cache global instance. |
|
75 |
* |
|
76 |
* @param int|string $key The key for the cache data that should be replaced. |
|
77 |
* @param mixed $data The new data to store in the cache. |
|
78 |
* @param string $group Optional. The group for the cache data that should be replaced. |
|
79 |
* Default empty. |
|
80 |
* @param int $expire Optional. When to expire the cache contents, in seconds. |
|
81 |
* Default 0 (no expiration). |
|
82 |
* @return bool True if contents were replaced, false if original value does not exist. |
|
0 | 83 |
*/ |
19 | 84 |
function wp_cache_replace( $key, $data, $group = '', $expire = 0 ) { |
85 |
global $wp_object_cache; |
|
86 |
||
87 |
return $wp_object_cache->replace( $key, $data, $group, (int) $expire ); |
|
0 | 88 |
} |
89 |
||
90 |
/** |
|
19 | 91 |
* Saves the data to the cache. |
0 | 92 |
* |
19 | 93 |
* Differs from wp_cache_add() and wp_cache_replace() in that it will always write data. |
0 | 94 |
* |
95 |
* @since 2.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
96 |
* |
19 | 97 |
* @see WP_Object_Cache::set() |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
98 |
* @global WP_Object_Cache $wp_object_cache Object cache global instance. |
0 | 99 |
* |
19 | 100 |
* @param int|string $key The cache key to use for retrieval later. |
101 |
* @param mixed $data The contents to store in the cache. |
|
102 |
* @param string $group Optional. Where to group the cache contents. Enables the same key |
|
103 |
* to be used across groups. Default empty. |
|
104 |
* @param int $expire Optional. When to expire the cache contents, in seconds. |
|
105 |
* Default 0 (no expiration). |
|
106 |
* @return bool True on success, false on failure. |
|
0 | 107 |
*/ |
19 | 108 |
function wp_cache_set( $key, $data, $group = '', $expire = 0 ) { |
0 | 109 |
global $wp_object_cache; |
110 |
||
19 | 111 |
return $wp_object_cache->set( $key, $data, $group, (int) $expire ); |
0 | 112 |
} |
113 |
||
114 |
/** |
|
19 | 115 |
* Sets multiple values to the cache in one call. |
0 | 116 |
* |
19 | 117 |
* @since 6.0.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
118 |
* |
19 | 119 |
* @see WP_Object_Cache::set_multiple() |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
120 |
* @global WP_Object_Cache $wp_object_cache Object cache global instance. |
0 | 121 |
* |
19 | 122 |
* @param array $data Array of keys and values to be set. |
123 |
* @param string $group Optional. Where the cache contents are grouped. Default empty. |
|
124 |
* @param int $expire Optional. When to expire the cache contents, in seconds. |
|
125 |
* Default 0 (no expiration). |
|
126 |
* @return bool[] Array of return values, grouped by key. Each value is either |
|
127 |
* true on success, or false on failure. |
|
0 | 128 |
*/ |
19 | 129 |
function wp_cache_set_multiple( array $data, $group = '', $expire = 0 ) { |
0 | 130 |
global $wp_object_cache; |
131 |
||
19 | 132 |
return $wp_object_cache->set_multiple( $data, $group, $expire ); |
0 | 133 |
} |
134 |
||
135 |
/** |
|
136 |
* Retrieves the cache contents from the cache by key and group. |
|
137 |
* |
|
138 |
* @since 2.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
139 |
* |
0 | 140 |
* @see WP_Object_Cache::get() |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
141 |
* @global WP_Object_Cache $wp_object_cache Object cache global instance. |
0 | 142 |
* |
16 | 143 |
* @param int|string $key The key under which the cache contents are stored. |
144 |
* @param string $group Optional. Where the cache contents are grouped. Default empty. |
|
145 |
* @param bool $force Optional. Whether to force an update of the local cache |
|
146 |
* from the persistent cache. Default false. |
|
147 |
* @param bool $found Optional. Whether the key was found in the cache (passed by reference). |
|
148 |
* Disambiguates a return of false, a storable value. Default null. |
|
149 |
* @return mixed|false The cache contents on success, false on failure to retrieve contents. |
|
0 | 150 |
*/ |
151 |
function wp_cache_get( $key, $group = '', $force = false, &$found = null ) { |
|
152 |
global $wp_object_cache; |
|
153 |
||
154 |
return $wp_object_cache->get( $key, $group, $force, $found ); |
|
155 |
} |
|
156 |
||
157 |
/** |
|
16 | 158 |
* Retrieves multiple values from the cache in one call. |
159 |
* |
|
160 |
* @since 5.5.0 |
|
161 |
* |
|
162 |
* @see WP_Object_Cache::get_multiple() |
|
163 |
* @global WP_Object_Cache $wp_object_cache Object cache global instance. |
|
164 |
* |
|
165 |
* @param array $keys Array of keys under which the cache contents are stored. |
|
166 |
* @param string $group Optional. Where the cache contents are grouped. Default empty. |
|
167 |
* @param bool $force Optional. Whether to force an update of the local cache |
|
168 |
* from the persistent cache. Default false. |
|
19 | 169 |
* @return array Array of return values, grouped by key. Each value is either |
170 |
* the cache contents on success, or false on failure. |
|
16 | 171 |
*/ |
172 |
function wp_cache_get_multiple( $keys, $group = '', $force = false ) { |
|
173 |
global $wp_object_cache; |
|
174 |
||
175 |
return $wp_object_cache->get_multiple( $keys, $group, $force ); |
|
176 |
} |
|
177 |
||
178 |
/** |
|
19 | 179 |
* Removes the cache contents matching key and group. |
180 |
* |
|
181 |
* @since 2.0.0 |
|
182 |
* |
|
183 |
* @see WP_Object_Cache::delete() |
|
184 |
* @global WP_Object_Cache $wp_object_cache Object cache global instance. |
|
185 |
* |
|
186 |
* @param int|string $key What the contents in the cache are called. |
|
187 |
* @param string $group Optional. Where the cache contents are grouped. Default empty. |
|
188 |
* @return bool True on successful removal, false on failure. |
|
189 |
*/ |
|
190 |
function wp_cache_delete( $key, $group = '' ) { |
|
191 |
global $wp_object_cache; |
|
192 |
||
193 |
return $wp_object_cache->delete( $key, $group ); |
|
194 |
} |
|
195 |
||
196 |
/** |
|
197 |
* Deletes multiple values from the cache in one call. |
|
198 |
* |
|
199 |
* @since 6.0.0 |
|
200 |
* |
|
201 |
* @see WP_Object_Cache::delete_multiple() |
|
202 |
* @global WP_Object_Cache $wp_object_cache Object cache global instance. |
|
203 |
* |
|
204 |
* @param array $keys Array of keys under which the cache to deleted. |
|
205 |
* @param string $group Optional. Where the cache contents are grouped. Default empty. |
|
206 |
* @return bool[] Array of return values, grouped by key. Each value is either |
|
207 |
* true on success, or false if the contents were not deleted. |
|
208 |
*/ |
|
209 |
function wp_cache_delete_multiple( array $keys, $group = '' ) { |
|
210 |
global $wp_object_cache; |
|
211 |
||
212 |
return $wp_object_cache->delete_multiple( $keys, $group ); |
|
213 |
} |
|
214 |
||
215 |
/** |
|
216 |
* Increments numeric cache item's value. |
|
0 | 217 |
* |
218 |
* @since 3.3.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
219 |
* |
0 | 220 |
* @see WP_Object_Cache::incr() |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
221 |
* @global WP_Object_Cache $wp_object_cache Object cache global instance. |
0 | 222 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
223 |
* @param int|string $key The key for the cache contents that should be incremented. |
19 | 224 |
* @param int $offset Optional. The amount by which to increment the item's value. |
225 |
* Default 1. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
226 |
* @param string $group Optional. The group the key is in. Default empty. |
16 | 227 |
* @return int|false The item's new value on success, false on failure. |
0 | 228 |
*/ |
229 |
function wp_cache_incr( $key, $offset = 1, $group = '' ) { |
|
230 |
global $wp_object_cache; |
|
231 |
||
232 |
return $wp_object_cache->incr( $key, $offset, $group ); |
|
233 |
} |
|
234 |
||
235 |
/** |
|
19 | 236 |
* Decrements numeric cache item's value. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
237 |
* |
19 | 238 |
* @since 3.3.0 |
0 | 239 |
* |
19 | 240 |
* @see WP_Object_Cache::decr() |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
241 |
* @global WP_Object_Cache $wp_object_cache Object cache global instance. |
0 | 242 |
* |
19 | 243 |
* @param int|string $key The cache key to decrement. |
244 |
* @param int $offset Optional. The amount by which to decrement the item's value. |
|
245 |
* Default 1. |
|
246 |
* @param string $group Optional. The group the key is in. Default empty. |
|
247 |
* @return int|false The item's new value on success, false on failure. |
|
0 | 248 |
*/ |
19 | 249 |
function wp_cache_decr( $key, $offset = 1, $group = '' ) { |
0 | 250 |
global $wp_object_cache; |
251 |
||
19 | 252 |
return $wp_object_cache->decr( $key, $offset, $group ); |
0 | 253 |
} |
254 |
||
255 |
/** |
|
19 | 256 |
* Removes all cache items. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
257 |
* |
5 | 258 |
* @since 2.0.0 |
259 |
* |
|
19 | 260 |
* @see WP_Object_Cache::flush() |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
261 |
* @global WP_Object_Cache $wp_object_cache Object cache global instance. |
0 | 262 |
* |
16 | 263 |
* @return bool True on success, false on failure. |
0 | 264 |
*/ |
19 | 265 |
function wp_cache_flush() { |
0 | 266 |
global $wp_object_cache; |
267 |
||
19 | 268 |
return $wp_object_cache->flush(); |
0 | 269 |
} |
270 |
||
271 |
/** |
|
19 | 272 |
* Removes all cache items from the in-memory runtime cache. |
0 | 273 |
* |
19 | 274 |
* @since 6.0.0 |
0 | 275 |
* |
19 | 276 |
* @see WP_Object_Cache::flush() |
0 | 277 |
* |
19 | 278 |
* @return bool True on success, false on failure. |
0 | 279 |
*/ |
19 | 280 |
function wp_cache_flush_runtime() { |
281 |
return wp_cache_flush(); |
|
282 |
} |
|
0 | 283 |
|
19 | 284 |
/** |
285 |
* Closes the cache. |
|
286 |
* |
|
287 |
* This function has ceased to do anything since WordPress 2.5. The |
|
288 |
* functionality was removed along with the rest of the persistent cache. |
|
289 |
* |
|
290 |
* This does not mean that plugins can't implement this function when they need |
|
291 |
* to make sure that the cache is cleaned up after WordPress no longer needs it. |
|
292 |
* |
|
293 |
* @since 2.0.0 |
|
294 |
* |
|
295 |
* @return true Always returns true. |
|
296 |
*/ |
|
297 |
function wp_cache_close() { |
|
298 |
return true; |
|
0 | 299 |
} |
300 |
||
301 |
/** |
|
302 |
* Adds a group or set of groups to the list of global groups. |
|
303 |
* |
|
304 |
* @since 2.6.0 |
|
305 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
306 |
* @see WP_Object_Cache::add_global_groups() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
307 |
* @global WP_Object_Cache $wp_object_cache Object cache global instance. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
308 |
* |
18 | 309 |
* @param string|string[] $groups A group or an array of groups to add. |
0 | 310 |
*/ |
311 |
function wp_cache_add_global_groups( $groups ) { |
|
312 |
global $wp_object_cache; |
|
313 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
314 |
$wp_object_cache->add_global_groups( $groups ); |
0 | 315 |
} |
316 |
||
317 |
/** |
|
318 |
* Adds a group or set of groups to the list of non-persistent groups. |
|
319 |
* |
|
320 |
* @since 2.6.0 |
|
321 |
* |
|
18 | 322 |
* @param string|string[] $groups A group or an array of groups to add. |
0 | 323 |
*/ |
324 |
function wp_cache_add_non_persistent_groups( $groups ) { |
|
325 |
// Default cache doesn't persist so nothing to do here. |
|
326 |
} |
|
327 |
||
328 |
/** |
|
19 | 329 |
* Switches the internal blog ID. |
330 |
* |
|
331 |
* This changes the blog id used to create keys in blog specific groups. |
|
332 |
* |
|
333 |
* @since 3.5.0 |
|
334 |
* |
|
335 |
* @see WP_Object_Cache::switch_to_blog() |
|
336 |
* @global WP_Object_Cache $wp_object_cache Object cache global instance. |
|
337 |
* |
|
338 |
* @param int $blog_id Site ID. |
|
339 |
*/ |
|
340 |
function wp_cache_switch_to_blog( $blog_id ) { |
|
341 |
global $wp_object_cache; |
|
342 |
||
343 |
$wp_object_cache->switch_to_blog( $blog_id ); |
|
344 |
} |
|
345 |
||
346 |
/** |
|
347 |
* Resets internal cache keys and structures. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
348 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
349 |
* If the cache back end uses global blog or site IDs as part of its cache keys, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
350 |
* this function instructs the back end to reset those keys and perform any cleanup |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
351 |
* since blog or site IDs have changed since cache init. |
0 | 352 |
* |
353 |
* This function is deprecated. Use wp_cache_switch_to_blog() instead of this |
|
354 |
* function when preparing the cache for a blog switch. For clearing the cache |
|
355 |
* during unit tests, consider using wp_cache_init(). wp_cache_init() is not |
|
19 | 356 |
* recommended outside of unit tests as the performance penalty for using it is high. |
0 | 357 |
* |
19 | 358 |
* @since 3.0.0 |
359 |
* @deprecated 3.5.0 Use wp_cache_switch_to_blog() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
360 |
* @see WP_Object_Cache::reset() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
361 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
362 |
* @global WP_Object_Cache $wp_object_cache Object cache global instance. |
0 | 363 |
*/ |
364 |
function wp_cache_reset() { |
|
19 | 365 |
_deprecated_function( __FUNCTION__, '3.5.0', 'wp_cache_switch_to_blog()' ); |
0 | 366 |
|
367 |
global $wp_object_cache; |
|
368 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
369 |
$wp_object_cache->reset(); |
0 | 370 |
} |