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