author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
child 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
16 | 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 |
/** |
|
11 |
* Core class that implements an object cache. |
|
12 |
* |
|
13 |
* The WordPress Object Cache is used to save on trips to the database. The |
|
14 |
* Object Cache stores all of the cache data to memory and makes the cache |
|
15 |
* contents available by using a key, which is used to name and later retrieve |
|
16 |
* the cache contents. |
|
17 |
* |
|
18 |
* The Object Cache can be replaced by other caching mechanisms by placing files |
|
19 |
* in the wp-content folder which is looked at in wp-settings. If that file |
|
20 |
* exists, then this file will not be included. |
|
21 |
* |
|
22 |
* @since 2.0.0 |
|
23 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
24 |
#[AllowDynamicProperties] |
16 | 25 |
class WP_Object_Cache { |
26 |
||
27 |
/** |
|
28 |
* Holds the cached objects. |
|
29 |
* |
|
30 |
* @since 2.0.0 |
|
31 |
* @var array |
|
32 |
*/ |
|
33 |
private $cache = array(); |
|
34 |
||
35 |
/** |
|
36 |
* The amount of times the cache data was already stored in the cache. |
|
37 |
* |
|
38 |
* @since 2.5.0 |
|
39 |
* @var int |
|
40 |
*/ |
|
41 |
public $cache_hits = 0; |
|
42 |
||
43 |
/** |
|
44 |
* Amount of times the cache did not have the request in cache. |
|
45 |
* |
|
46 |
* @since 2.0.0 |
|
47 |
* @var int |
|
48 |
*/ |
|
49 |
public $cache_misses = 0; |
|
50 |
||
51 |
/** |
|
52 |
* List of global cache groups. |
|
53 |
* |
|
54 |
* @since 3.0.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
55 |
* @var string[] |
16 | 56 |
*/ |
57 |
protected $global_groups = array(); |
|
58 |
||
59 |
/** |
|
60 |
* The blog prefix to prepend to keys in non-global groups. |
|
61 |
* |
|
62 |
* @since 3.5.0 |
|
63 |
* @var string |
|
64 |
*/ |
|
65 |
private $blog_prefix; |
|
66 |
||
67 |
/** |
|
68 |
* Holds the value of is_multisite(). |
|
69 |
* |
|
70 |
* @since 3.5.0 |
|
71 |
* @var bool |
|
72 |
*/ |
|
73 |
private $multisite; |
|
74 |
||
75 |
/** |
|
76 |
* Sets up object properties; PHP 5 style constructor. |
|
77 |
* |
|
78 |
* @since 2.0.8 |
|
79 |
*/ |
|
80 |
public function __construct() { |
|
81 |
$this->multisite = is_multisite(); |
|
82 |
$this->blog_prefix = $this->multisite ? get_current_blog_id() . ':' : ''; |
|
83 |
} |
|
84 |
||
85 |
/** |
|
86 |
* Makes private properties readable for backward compatibility. |
|
87 |
* |
|
88 |
* @since 4.0.0 |
|
89 |
* |
|
90 |
* @param string $name Property to get. |
|
91 |
* @return mixed Property. |
|
92 |
*/ |
|
93 |
public function __get( $name ) { |
|
94 |
return $this->$name; |
|
95 |
} |
|
96 |
||
97 |
/** |
|
98 |
* Makes private properties settable for backward compatibility. |
|
99 |
* |
|
100 |
* @since 4.0.0 |
|
101 |
* |
|
102 |
* @param string $name Property to set. |
|
103 |
* @param mixed $value Property value. |
|
104 |
* @return mixed Newly-set property. |
|
105 |
*/ |
|
106 |
public function __set( $name, $value ) { |
|
107 |
return $this->$name = $value; |
|
108 |
} |
|
109 |
||
110 |
/** |
|
111 |
* Makes private properties checkable for backward compatibility. |
|
112 |
* |
|
113 |
* @since 4.0.0 |
|
114 |
* |
|
115 |
* @param string $name Property to check if set. |
|
116 |
* @return bool Whether the property is set. |
|
117 |
*/ |
|
118 |
public function __isset( $name ) { |
|
119 |
return isset( $this->$name ); |
|
120 |
} |
|
121 |
||
122 |
/** |
|
123 |
* Makes private properties un-settable for backward compatibility. |
|
124 |
* |
|
125 |
* @since 4.0.0 |
|
126 |
* |
|
127 |
* @param string $name Property to unset. |
|
128 |
*/ |
|
129 |
public function __unset( $name ) { |
|
130 |
unset( $this->$name ); |
|
131 |
} |
|
132 |
||
133 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
134 |
* Serves as a utility function to determine whether a key is valid. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
135 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
136 |
* @since 6.1.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
137 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
138 |
* @param int|string $key Cache key to check for validity. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
139 |
* @return bool Whether the key is valid. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
140 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
141 |
protected function is_valid_key( $key ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
142 |
if ( is_int( $key ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
143 |
return true; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
144 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
145 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
146 |
if ( is_string( $key ) && trim( $key ) !== '' ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
147 |
return true; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
148 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
149 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
150 |
$type = gettype( $key ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
151 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
152 |
if ( ! function_exists( '__' ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
153 |
wp_load_translations_early(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
154 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
155 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
156 |
$message = is_string( $key ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
157 |
? __( 'Cache key must not be an empty string.' ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
158 |
/* translators: %s: The type of the given cache key. */ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
159 |
: sprintf( __( 'Cache key must be an integer or a non-empty string, %s given.' ), $type ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
160 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
161 |
_doing_it_wrong( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
162 |
sprintf( '%s::%s', __CLASS__, debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2 )[1]['function'] ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
163 |
$message, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
164 |
'6.1.0' |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
165 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
166 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
167 |
return false; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
168 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
169 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
170 |
/** |
19 | 171 |
* Serves as a utility function to determine whether a key exists in the cache. |
172 |
* |
|
173 |
* @since 3.4.0 |
|
174 |
* |
|
175 |
* @param int|string $key Cache key to check for existence. |
|
176 |
* @param string $group Cache group for the key existence check. |
|
177 |
* @return bool Whether the key exists in the cache for the given group. |
|
178 |
*/ |
|
179 |
protected function _exists( $key, $group ) { |
|
180 |
return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) ); |
|
181 |
} |
|
182 |
||
183 |
/** |
|
16 | 184 |
* Adds data to the cache if it doesn't already exist. |
185 |
* |
|
186 |
* @since 2.0.0 |
|
187 |
* |
|
188 |
* @uses WP_Object_Cache::_exists() Checks to see if the cache already has data. |
|
189 |
* @uses WP_Object_Cache::set() Sets the data after the checking the cache |
|
190 |
* contents existence. |
|
191 |
* |
|
192 |
* @param int|string $key What to call the contents in the cache. |
|
193 |
* @param mixed $data The contents to store in the cache. |
|
194 |
* @param string $group Optional. Where to group the cache contents. Default 'default'. |
|
19 | 195 |
* @param int $expire Optional. When to expire the cache contents, in seconds. |
196 |
* Default 0 (no expiration). |
|
16 | 197 |
* @return bool True on success, false if cache key and group already exist. |
198 |
*/ |
|
199 |
public function add( $key, $data, $group = 'default', $expire = 0 ) { |
|
200 |
if ( wp_suspend_cache_addition() ) { |
|
201 |
return false; |
|
202 |
} |
|
203 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
204 |
if ( ! $this->is_valid_key( $key ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
205 |
return false; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
206 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
207 |
|
16 | 208 |
if ( empty( $group ) ) { |
209 |
$group = 'default'; |
|
210 |
} |
|
211 |
||
212 |
$id = $key; |
|
213 |
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
|
214 |
$id = $this->blog_prefix . $key; |
|
215 |
} |
|
216 |
||
217 |
if ( $this->_exists( $id, $group ) ) { |
|
218 |
return false; |
|
219 |
} |
|
220 |
||
221 |
return $this->set( $key, $data, $group, (int) $expire ); |
|
222 |
} |
|
223 |
||
224 |
/** |
|
19 | 225 |
* Adds multiple values to the cache in one call. |
226 |
* |
|
227 |
* @since 6.0.0 |
|
16 | 228 |
* |
19 | 229 |
* @param array $data Array of keys and values to be added. |
230 |
* @param string $group Optional. Where the cache contents are grouped. Default empty. |
|
231 |
* @param int $expire Optional. When to expire the cache contents, in seconds. |
|
232 |
* Default 0 (no expiration). |
|
233 |
* @return bool[] Array of return values, grouped by key. Each value is either |
|
234 |
* true on success, or false if cache key and group already exist. |
|
16 | 235 |
*/ |
19 | 236 |
public function add_multiple( array $data, $group = '', $expire = 0 ) { |
237 |
$values = array(); |
|
16 | 238 |
|
19 | 239 |
foreach ( $data as $key => $value ) { |
240 |
$values[ $key ] = $this->add( $key, $value, $group, $expire ); |
|
241 |
} |
|
242 |
||
243 |
return $values; |
|
16 | 244 |
} |
245 |
||
246 |
/** |
|
19 | 247 |
* Replaces the contents in the cache, if contents already exist. |
16 | 248 |
* |
19 | 249 |
* @since 2.0.0 |
250 |
* |
|
251 |
* @see WP_Object_Cache::set() |
|
16 | 252 |
* |
19 | 253 |
* @param int|string $key What to call the contents in the cache. |
254 |
* @param mixed $data The contents to store in the cache. |
|
255 |
* @param string $group Optional. Where to group the cache contents. Default 'default'. |
|
256 |
* @param int $expire Optional. When to expire the cache contents, in seconds. |
|
257 |
* Default 0 (no expiration). |
|
258 |
* @return bool True if contents were replaced, false if original value does not exist. |
|
16 | 259 |
*/ |
19 | 260 |
public function replace( $key, $data, $group = 'default', $expire = 0 ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
261 |
if ( ! $this->is_valid_key( $key ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
262 |
return false; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
263 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
264 |
|
19 | 265 |
if ( empty( $group ) ) { |
266 |
$group = 'default'; |
|
267 |
} |
|
268 |
||
269 |
$id = $key; |
|
270 |
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
|
271 |
$id = $this->blog_prefix . $key; |
|
272 |
} |
|
273 |
||
274 |
if ( ! $this->_exists( $id, $group ) ) { |
|
275 |
return false; |
|
276 |
} |
|
277 |
||
278 |
return $this->set( $key, $data, $group, (int) $expire ); |
|
279 |
} |
|
280 |
||
281 |
/** |
|
282 |
* Sets the data contents into the cache. |
|
283 |
* |
|
284 |
* The cache contents are grouped by the $group parameter followed by the |
|
285 |
* $key. This allows for duplicate IDs in unique groups. Therefore, naming of |
|
286 |
* the group should be used with care and should follow normal function |
|
287 |
* naming guidelines outside of core WordPress usage. |
|
288 |
* |
|
289 |
* The $expire parameter is not used, because the cache will automatically |
|
290 |
* expire for each time a page is accessed and PHP finishes. The method is |
|
291 |
* more for cache plugins which use files. |
|
292 |
* |
|
293 |
* @since 2.0.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
294 |
* @since 6.1.0 Returns false if cache key is invalid. |
19 | 295 |
* |
296 |
* @param int|string $key What to call the contents in the cache. |
|
297 |
* @param mixed $data The contents to store in the cache. |
|
298 |
* @param string $group Optional. Where to group the cache contents. Default 'default'. |
|
299 |
* @param int $expire Optional. Not used. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
300 |
* @return bool True if contents were set, false if key is invalid. |
19 | 301 |
*/ |
302 |
public function set( $key, $data, $group = 'default', $expire = 0 ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
303 |
if ( ! $this->is_valid_key( $key ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
304 |
return false; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
305 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
306 |
|
16 | 307 |
if ( empty( $group ) ) { |
308 |
$group = 'default'; |
|
309 |
} |
|
310 |
||
311 |
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
|
312 |
$key = $this->blog_prefix . $key; |
|
313 |
} |
|
314 |
||
19 | 315 |
if ( is_object( $data ) ) { |
316 |
$data = clone $data; |
|
16 | 317 |
} |
318 |
||
19 | 319 |
$this->cache[ $group ][ $key ] = $data; |
16 | 320 |
return true; |
321 |
} |
|
322 |
||
323 |
/** |
|
19 | 324 |
* Sets multiple values to the cache in one call. |
16 | 325 |
* |
19 | 326 |
* @since 6.0.0 |
16 | 327 |
* |
19 | 328 |
* @param array $data Array of key and value to be set. |
329 |
* @param string $group Optional. Where the cache contents are grouped. Default empty. |
|
330 |
* @param int $expire Optional. When to expire the cache contents, in seconds. |
|
331 |
* Default 0 (no expiration). |
|
332 |
* @return bool[] Array of return values, grouped by key. Each value is always true. |
|
16 | 333 |
*/ |
19 | 334 |
public function set_multiple( array $data, $group = '', $expire = 0 ) { |
335 |
$values = array(); |
|
16 | 336 |
|
19 | 337 |
foreach ( $data as $key => $value ) { |
338 |
$values[ $key ] = $this->set( $key, $value, $group, $expire ); |
|
339 |
} |
|
340 |
||
341 |
return $values; |
|
16 | 342 |
} |
343 |
||
344 |
/** |
|
345 |
* Retrieves the cache contents, if it exists. |
|
346 |
* |
|
347 |
* The contents will be first attempted to be retrieved by searching by the |
|
348 |
* key in the cache group. If the cache is hit (success) then the contents |
|
349 |
* are returned. |
|
350 |
* |
|
351 |
* On failure, the number of cache misses will be incremented. |
|
352 |
* |
|
353 |
* @since 2.0.0 |
|
354 |
* |
|
355 |
* @param int|string $key The key under which the cache contents are stored. |
|
356 |
* @param string $group Optional. Where the cache contents are grouped. Default 'default'. |
|
357 |
* @param bool $force Optional. Unused. Whether to force an update of the local cache |
|
358 |
* from the persistent cache. Default false. |
|
359 |
* @param bool $found Optional. Whether the key was found in the cache (passed by reference). |
|
360 |
* Disambiguates a return of false, a storable value. Default null. |
|
361 |
* @return mixed|false The cache contents on success, false on failure to retrieve contents. |
|
362 |
*/ |
|
363 |
public function get( $key, $group = 'default', $force = false, &$found = null ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
364 |
if ( ! $this->is_valid_key( $key ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
365 |
return false; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
366 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
367 |
|
16 | 368 |
if ( empty( $group ) ) { |
369 |
$group = 'default'; |
|
370 |
} |
|
371 |
||
372 |
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
|
373 |
$key = $this->blog_prefix . $key; |
|
374 |
} |
|
375 |
||
376 |
if ( $this->_exists( $key, $group ) ) { |
|
377 |
$found = true; |
|
378 |
$this->cache_hits += 1; |
|
379 |
if ( is_object( $this->cache[ $group ][ $key ] ) ) { |
|
380 |
return clone $this->cache[ $group ][ $key ]; |
|
381 |
} else { |
|
382 |
return $this->cache[ $group ][ $key ]; |
|
383 |
} |
|
384 |
} |
|
385 |
||
386 |
$found = false; |
|
387 |
$this->cache_misses += 1; |
|
388 |
return false; |
|
389 |
} |
|
390 |
||
391 |
/** |
|
392 |
* Retrieves multiple values from the cache in one call. |
|
393 |
* |
|
394 |
* @since 5.5.0 |
|
395 |
* |
|
396 |
* @param array $keys Array of keys under which the cache contents are stored. |
|
397 |
* @param string $group Optional. Where the cache contents are grouped. Default 'default'. |
|
398 |
* @param bool $force Optional. Whether to force an update of the local cache |
|
399 |
* from the persistent cache. Default false. |
|
19 | 400 |
* @return array Array of return values, grouped by key. Each value is either |
401 |
* the cache contents on success, or false on failure. |
|
16 | 402 |
*/ |
403 |
public function get_multiple( $keys, $group = 'default', $force = false ) { |
|
404 |
$values = array(); |
|
405 |
||
406 |
foreach ( $keys as $key ) { |
|
407 |
$values[ $key ] = $this->get( $key, $group, $force ); |
|
408 |
} |
|
409 |
||
410 |
return $values; |
|
411 |
} |
|
412 |
||
413 |
/** |
|
19 | 414 |
* Removes the contents of the cache key in the group. |
415 |
* |
|
416 |
* If the cache key does not exist in the group, then nothing will happen. |
|
417 |
* |
|
418 |
* @since 2.0.0 |
|
419 |
* |
|
420 |
* @param int|string $key What the contents in the cache are called. |
|
421 |
* @param string $group Optional. Where the cache contents are grouped. Default 'default'. |
|
422 |
* @param bool $deprecated Optional. Unused. Default false. |
|
423 |
* @return bool True on success, false if the contents were not deleted. |
|
424 |
*/ |
|
425 |
public function delete( $key, $group = 'default', $deprecated = false ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
426 |
if ( ! $this->is_valid_key( $key ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
427 |
return false; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
428 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
429 |
|
19 | 430 |
if ( empty( $group ) ) { |
431 |
$group = 'default'; |
|
432 |
} |
|
433 |
||
434 |
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
|
435 |
$key = $this->blog_prefix . $key; |
|
436 |
} |
|
437 |
||
438 |
if ( ! $this->_exists( $key, $group ) ) { |
|
439 |
return false; |
|
440 |
} |
|
441 |
||
442 |
unset( $this->cache[ $group ][ $key ] ); |
|
443 |
return true; |
|
444 |
} |
|
445 |
||
446 |
/** |
|
447 |
* Deletes multiple values from the cache in one call. |
|
448 |
* |
|
449 |
* @since 6.0.0 |
|
450 |
* |
|
451 |
* @param array $keys Array of keys to be deleted. |
|
452 |
* @param string $group Optional. Where the cache contents are grouped. Default empty. |
|
453 |
* @return bool[] Array of return values, grouped by key. Each value is either |
|
454 |
* true on success, or false if the contents were not deleted. |
|
455 |
*/ |
|
456 |
public function delete_multiple( array $keys, $group = '' ) { |
|
457 |
$values = array(); |
|
458 |
||
459 |
foreach ( $keys as $key ) { |
|
460 |
$values[ $key ] = $this->delete( $key, $group ); |
|
461 |
} |
|
462 |
||
463 |
return $values; |
|
464 |
} |
|
465 |
||
466 |
/** |
|
16 | 467 |
* Increments numeric cache item's value. |
468 |
* |
|
469 |
* @since 3.3.0 |
|
470 |
* |
|
19 | 471 |
* @param int|string $key The cache key to increment. |
472 |
* @param int $offset Optional. The amount by which to increment the item's value. |
|
473 |
* Default 1. |
|
16 | 474 |
* @param string $group Optional. The group the key is in. Default 'default'. |
475 |
* @return int|false The item's new value on success, false on failure. |
|
476 |
*/ |
|
477 |
public function incr( $key, $offset = 1, $group = 'default' ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
478 |
if ( ! $this->is_valid_key( $key ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
479 |
return false; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
480 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
481 |
|
16 | 482 |
if ( empty( $group ) ) { |
483 |
$group = 'default'; |
|
484 |
} |
|
485 |
||
486 |
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
|
487 |
$key = $this->blog_prefix . $key; |
|
488 |
} |
|
489 |
||
490 |
if ( ! $this->_exists( $key, $group ) ) { |
|
491 |
return false; |
|
492 |
} |
|
493 |
||
494 |
if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) { |
|
495 |
$this->cache[ $group ][ $key ] = 0; |
|
496 |
} |
|
497 |
||
498 |
$offset = (int) $offset; |
|
499 |
||
500 |
$this->cache[ $group ][ $key ] += $offset; |
|
501 |
||
502 |
if ( $this->cache[ $group ][ $key ] < 0 ) { |
|
503 |
$this->cache[ $group ][ $key ] = 0; |
|
504 |
} |
|
505 |
||
506 |
return $this->cache[ $group ][ $key ]; |
|
507 |
} |
|
508 |
||
509 |
/** |
|
19 | 510 |
* Decrements numeric cache item's value. |
16 | 511 |
* |
19 | 512 |
* @since 3.3.0 |
16 | 513 |
* |
19 | 514 |
* @param int|string $key The cache key to decrement. |
515 |
* @param int $offset Optional. The amount by which to decrement the item's value. |
|
516 |
* Default 1. |
|
517 |
* @param string $group Optional. The group the key is in. Default 'default'. |
|
518 |
* @return int|false The item's new value on success, false on failure. |
|
16 | 519 |
*/ |
19 | 520 |
public function decr( $key, $offset = 1, $group = 'default' ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
521 |
if ( ! $this->is_valid_key( $key ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
522 |
return false; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
523 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
524 |
|
16 | 525 |
if ( empty( $group ) ) { |
526 |
$group = 'default'; |
|
527 |
} |
|
528 |
||
529 |
if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
|
530 |
$key = $this->blog_prefix . $key; |
|
531 |
} |
|
532 |
||
19 | 533 |
if ( ! $this->_exists( $key, $group ) ) { |
534 |
return false; |
|
535 |
} |
|
536 |
||
537 |
if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) { |
|
538 |
$this->cache[ $group ][ $key ] = 0; |
|
539 |
} |
|
540 |
||
541 |
$offset = (int) $offset; |
|
542 |
||
543 |
$this->cache[ $group ][ $key ] -= $offset; |
|
544 |
||
545 |
if ( $this->cache[ $group ][ $key ] < 0 ) { |
|
546 |
$this->cache[ $group ][ $key ] = 0; |
|
16 | 547 |
} |
548 |
||
19 | 549 |
return $this->cache[ $group ][ $key ]; |
550 |
} |
|
551 |
||
552 |
/** |
|
553 |
* Clears the object cache of all data. |
|
554 |
* |
|
555 |
* @since 2.0.0 |
|
556 |
* |
|
557 |
* @return true Always returns true. |
|
558 |
*/ |
|
559 |
public function flush() { |
|
560 |
$this->cache = array(); |
|
561 |
||
16 | 562 |
return true; |
563 |
} |
|
564 |
||
565 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
566 |
* Removes all cache items in a group. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
567 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
568 |
* @since 6.1.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
569 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
570 |
* @param string $group Name of group to remove from cache. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
571 |
* @return true Always returns true. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
572 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
573 |
public function flush_group( $group ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
574 |
unset( $this->cache[ $group ] ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
575 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
576 |
return true; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
577 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
578 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
579 |
/** |
19 | 580 |
* Sets the list of global cache groups. |
581 |
* |
|
582 |
* @since 3.0.0 |
|
583 |
* |
|
584 |
* @param string|string[] $groups List of groups that are global. |
|
585 |
*/ |
|
586 |
public function add_global_groups( $groups ) { |
|
587 |
$groups = (array) $groups; |
|
588 |
||
589 |
$groups = array_fill_keys( $groups, true ); |
|
590 |
$this->global_groups = array_merge( $this->global_groups, $groups ); |
|
591 |
} |
|
592 |
||
593 |
/** |
|
594 |
* Switches the internal blog ID. |
|
595 |
* |
|
596 |
* This changes the blog ID used to create keys in blog specific groups. |
|
597 |
* |
|
598 |
* @since 3.5.0 |
|
599 |
* |
|
600 |
* @param int $blog_id Blog ID. |
|
601 |
*/ |
|
602 |
public function switch_to_blog( $blog_id ) { |
|
603 |
$blog_id = (int) $blog_id; |
|
604 |
$this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; |
|
605 |
} |
|
606 |
||
607 |
/** |
|
608 |
* Resets cache keys. |
|
609 |
* |
|
610 |
* @since 3.0.0 |
|
611 |
* |
|
612 |
* @deprecated 3.5.0 Use WP_Object_Cache::switch_to_blog() |
|
613 |
* @see switch_to_blog() |
|
614 |
*/ |
|
615 |
public function reset() { |
|
616 |
_deprecated_function( __FUNCTION__, '3.5.0', 'WP_Object_Cache::switch_to_blog()' ); |
|
617 |
||
618 |
// Clear out non-global caches since the blog ID has changed. |
|
619 |
foreach ( array_keys( $this->cache ) as $group ) { |
|
620 |
if ( ! isset( $this->global_groups[ $group ] ) ) { |
|
621 |
unset( $this->cache[ $group ] ); |
|
622 |
} |
|
623 |
} |
|
624 |
} |
|
625 |
||
626 |
/** |
|
16 | 627 |
* Echoes the stats of the caching. |
628 |
* |
|
629 |
* Gives the cache hits, and cache misses. Also prints every cached group, |
|
630 |
* key and the data. |
|
631 |
* |
|
632 |
* @since 2.0.0 |
|
633 |
*/ |
|
634 |
public function stats() { |
|
635 |
echo '<p>'; |
|
636 |
echo "<strong>Cache Hits:</strong> {$this->cache_hits}<br />"; |
|
637 |
echo "<strong>Cache Misses:</strong> {$this->cache_misses}<br />"; |
|
638 |
echo '</p>'; |
|
639 |
echo '<ul>'; |
|
640 |
foreach ( $this->cache as $group => $cache ) { |
|
641 |
echo '<li><strong>Group:</strong> ' . esc_html( $group ) . ' - ( ' . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>'; |
|
642 |
} |
|
643 |
echo '</ul>'; |
|
644 |
} |
|
645 |
} |