164 |
178 |
165 return $this->set( $key, $data, $group, (int) $expire ); |
179 return $this->set( $key, $data, $group, (int) $expire ); |
166 } |
180 } |
167 |
181 |
168 /** |
182 /** |
169 * Sets the list of global cache groups. |
183 * Adds multiple values to the cache in one call. |
170 * |
184 * |
171 * @since 3.0.0 |
185 * @since 6.0.0 |
172 * |
186 * |
173 * @param string|string[] $groups List of groups that are global. |
187 * @param array $data Array of keys and values to be added. |
174 */ |
188 * @param string $group Optional. Where the cache contents are grouped. Default empty. |
175 public function add_global_groups( $groups ) { |
189 * @param int $expire Optional. When to expire the cache contents, in seconds. |
176 $groups = (array) $groups; |
190 * Default 0 (no expiration). |
177 |
191 * @return bool[] Array of return values, grouped by key. Each value is either |
178 $groups = array_fill_keys( $groups, true ); |
192 * true on success, or false if cache key and group already exist. |
179 $this->global_groups = array_merge( $this->global_groups, $groups ); |
193 */ |
180 } |
194 public function add_multiple( array $data, $group = '', $expire = 0 ) { |
181 |
195 $values = array(); |
182 /** |
196 |
183 * Decrements numeric cache item's value. |
197 foreach ( $data as $key => $value ) { |
184 * |
198 $values[ $key ] = $this->add( $key, $value, $group, $expire ); |
185 * @since 3.3.0 |
199 } |
186 * |
200 |
187 * @param int|string $key The cache key to decrement. |
201 return $values; |
188 * @param int $offset Optional. The amount by which to decrement the item's value. Default 1. |
202 } |
189 * @param string $group Optional. The group the key is in. Default 'default'. |
203 |
190 * @return int|false The item's new value on success, false on failure. |
204 /** |
191 */ |
205 * Replaces the contents in the cache, if contents already exist. |
192 public function decr( $key, $offset = 1, $group = 'default' ) { |
206 * |
|
207 * @since 2.0.0 |
|
208 * |
|
209 * @see WP_Object_Cache::set() |
|
210 * |
|
211 * @param int|string $key What to call the contents in the cache. |
|
212 * @param mixed $data The contents to store in the cache. |
|
213 * @param string $group Optional. Where to group the cache contents. Default 'default'. |
|
214 * @param int $expire Optional. When to expire the cache contents, in seconds. |
|
215 * Default 0 (no expiration). |
|
216 * @return bool True if contents were replaced, false if original value does not exist. |
|
217 */ |
|
218 public function replace( $key, $data, $group = 'default', $expire = 0 ) { |
|
219 if ( empty( $group ) ) { |
|
220 $group = 'default'; |
|
221 } |
|
222 |
|
223 $id = $key; |
|
224 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
|
225 $id = $this->blog_prefix . $key; |
|
226 } |
|
227 |
|
228 if ( ! $this->_exists( $id, $group ) ) { |
|
229 return false; |
|
230 } |
|
231 |
|
232 return $this->set( $key, $data, $group, (int) $expire ); |
|
233 } |
|
234 |
|
235 /** |
|
236 * Sets the data contents into the cache. |
|
237 * |
|
238 * The cache contents are grouped by the $group parameter followed by the |
|
239 * $key. This allows for duplicate IDs in unique groups. Therefore, naming of |
|
240 * the group should be used with care and should follow normal function |
|
241 * naming guidelines outside of core WordPress usage. |
|
242 * |
|
243 * The $expire parameter is not used, because the cache will automatically |
|
244 * expire for each time a page is accessed and PHP finishes. The method is |
|
245 * more for cache plugins which use files. |
|
246 * |
|
247 * @since 2.0.0 |
|
248 * |
|
249 * @param int|string $key What to call the contents in the cache. |
|
250 * @param mixed $data The contents to store in the cache. |
|
251 * @param string $group Optional. Where to group the cache contents. Default 'default'. |
|
252 * @param int $expire Optional. Not used. |
|
253 * @return true Always returns true. |
|
254 */ |
|
255 public function set( $key, $data, $group = 'default', $expire = 0 ) { |
193 if ( empty( $group ) ) { |
256 if ( empty( $group ) ) { |
194 $group = 'default'; |
257 $group = 'default'; |
195 } |
258 } |
196 |
259 |
197 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
260 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
198 $key = $this->blog_prefix . $key; |
261 $key = $this->blog_prefix . $key; |
199 } |
262 } |
200 |
263 |
201 if ( ! $this->_exists( $key, $group ) ) { |
264 if ( is_object( $data ) ) { |
202 return false; |
265 $data = clone $data; |
203 } |
266 } |
204 |
267 |
205 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) { |
268 $this->cache[ $group ][ $key ] = $data; |
206 $this->cache[ $group ][ $key ] = 0; |
|
207 } |
|
208 |
|
209 $offset = (int) $offset; |
|
210 |
|
211 $this->cache[ $group ][ $key ] -= $offset; |
|
212 |
|
213 if ( $this->cache[ $group ][ $key ] < 0 ) { |
|
214 $this->cache[ $group ][ $key ] = 0; |
|
215 } |
|
216 |
|
217 return $this->cache[ $group ][ $key ]; |
|
218 } |
|
219 |
|
220 /** |
|
221 * Removes the contents of the cache key in the group. |
|
222 * |
|
223 * If the cache key does not exist in the group, then nothing will happen. |
|
224 * |
|
225 * @since 2.0.0 |
|
226 * |
|
227 * @param int|string $key What the contents in the cache are called. |
|
228 * @param string $group Optional. Where the cache contents are grouped. Default 'default'. |
|
229 * @param bool $deprecated Optional. Unused. Default false. |
|
230 * @return bool False if the contents weren't deleted and true on success. |
|
231 */ |
|
232 public function delete( $key, $group = 'default', $deprecated = false ) { |
|
233 if ( empty( $group ) ) { |
|
234 $group = 'default'; |
|
235 } |
|
236 |
|
237 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
|
238 $key = $this->blog_prefix . $key; |
|
239 } |
|
240 |
|
241 if ( ! $this->_exists( $key, $group ) ) { |
|
242 return false; |
|
243 } |
|
244 |
|
245 unset( $this->cache[ $group ][ $key ] ); |
|
246 return true; |
269 return true; |
247 } |
270 } |
248 |
271 |
249 /** |
272 /** |
250 * Clears the object cache of all data. |
273 * Sets multiple values to the cache in one call. |
251 * |
274 * |
252 * @since 2.0.0 |
275 * @since 6.0.0 |
253 * |
276 * |
254 * @return true Always returns true. |
277 * @param array $data Array of key and value to be set. |
255 */ |
278 * @param string $group Optional. Where the cache contents are grouped. Default empty. |
256 public function flush() { |
279 * @param int $expire Optional. When to expire the cache contents, in seconds. |
257 $this->cache = array(); |
280 * Default 0 (no expiration). |
258 |
281 * @return bool[] Array of return values, grouped by key. Each value is always true. |
259 return true; |
282 */ |
|
283 public function set_multiple( array $data, $group = '', $expire = 0 ) { |
|
284 $values = array(); |
|
285 |
|
286 foreach ( $data as $key => $value ) { |
|
287 $values[ $key ] = $this->set( $key, $value, $group, $expire ); |
|
288 } |
|
289 |
|
290 return $values; |
260 } |
291 } |
261 |
292 |
262 /** |
293 /** |
263 * Retrieves the cache contents, if it exists. |
294 * Retrieves the cache contents, if it exists. |
264 * |
295 * |
322 |
354 |
323 return $values; |
355 return $values; |
324 } |
356 } |
325 |
357 |
326 /** |
358 /** |
|
359 * Removes the contents of the cache key in the group. |
|
360 * |
|
361 * If the cache key does not exist in the group, then nothing will happen. |
|
362 * |
|
363 * @since 2.0.0 |
|
364 * |
|
365 * @param int|string $key What the contents in the cache are called. |
|
366 * @param string $group Optional. Where the cache contents are grouped. Default 'default'. |
|
367 * @param bool $deprecated Optional. Unused. Default false. |
|
368 * @return bool True on success, false if the contents were not deleted. |
|
369 */ |
|
370 public function delete( $key, $group = 'default', $deprecated = false ) { |
|
371 if ( empty( $group ) ) { |
|
372 $group = 'default'; |
|
373 } |
|
374 |
|
375 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
|
376 $key = $this->blog_prefix . $key; |
|
377 } |
|
378 |
|
379 if ( ! $this->_exists( $key, $group ) ) { |
|
380 return false; |
|
381 } |
|
382 |
|
383 unset( $this->cache[ $group ][ $key ] ); |
|
384 return true; |
|
385 } |
|
386 |
|
387 /** |
|
388 * Deletes multiple values from the cache in one call. |
|
389 * |
|
390 * @since 6.0.0 |
|
391 * |
|
392 * @param array $keys Array of keys to be deleted. |
|
393 * @param string $group Optional. Where the cache contents are grouped. Default empty. |
|
394 * @return bool[] Array of return values, grouped by key. Each value is either |
|
395 * true on success, or false if the contents were not deleted. |
|
396 */ |
|
397 public function delete_multiple( array $keys, $group = '' ) { |
|
398 $values = array(); |
|
399 |
|
400 foreach ( $keys as $key ) { |
|
401 $values[ $key ] = $this->delete( $key, $group ); |
|
402 } |
|
403 |
|
404 return $values; |
|
405 } |
|
406 |
|
407 /** |
327 * Increments numeric cache item's value. |
408 * Increments numeric cache item's value. |
328 * |
409 * |
329 * @since 3.3.0 |
410 * @since 3.3.0 |
330 * |
411 * |
331 * @param int|string $key The cache key to increment |
412 * @param int|string $key The cache key to increment. |
332 * @param int $offset Optional. The amount by which to increment the item's value. Default 1. |
413 * @param int $offset Optional. The amount by which to increment the item's value. |
|
414 * Default 1. |
333 * @param string $group Optional. The group the key is in. Default 'default'. |
415 * @param string $group Optional. The group the key is in. Default 'default'. |
334 * @return int|false The item's new value on success, false on failure. |
416 * @return int|false The item's new value on success, false on failure. |
335 */ |
417 */ |
336 public function incr( $key, $offset = 1, $group = 'default' ) { |
418 public function incr( $key, $offset = 1, $group = 'default' ) { |
337 if ( empty( $group ) ) { |
419 if ( empty( $group ) ) { |
360 |
442 |
361 return $this->cache[ $group ][ $key ]; |
443 return $this->cache[ $group ][ $key ]; |
362 } |
444 } |
363 |
445 |
364 /** |
446 /** |
365 * Replaces the contents in the cache, if contents already exist. |
447 * Decrements numeric cache item's value. |
366 * |
448 * |
367 * @since 2.0.0 |
449 * @since 3.3.0 |
368 * |
450 * |
369 * @see WP_Object_Cache::set() |
451 * @param int|string $key The cache key to decrement. |
370 * |
452 * @param int $offset Optional. The amount by which to decrement the item's value. |
371 * @param int|string $key What to call the contents in the cache. |
453 * Default 1. |
372 * @param mixed $data The contents to store in the cache. |
454 * @param string $group Optional. The group the key is in. Default 'default'. |
373 * @param string $group Optional. Where to group the cache contents. Default 'default'. |
455 * @return int|false The item's new value on success, false on failure. |
374 * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration). |
456 */ |
375 * @return bool False if not exists, true if contents were replaced. |
457 public function decr( $key, $offset = 1, $group = 'default' ) { |
376 */ |
458 if ( empty( $group ) ) { |
377 public function replace( $key, $data, $group = 'default', $expire = 0 ) { |
459 $group = 'default'; |
378 if ( empty( $group ) ) { |
460 } |
379 $group = 'default'; |
461 |
380 } |
462 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
381 |
463 $key = $this->blog_prefix . $key; |
382 $id = $key; |
464 } |
383 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
465 |
384 $id = $this->blog_prefix . $key; |
466 if ( ! $this->_exists( $key, $group ) ) { |
385 } |
|
386 |
|
387 if ( ! $this->_exists( $id, $group ) ) { |
|
388 return false; |
467 return false; |
389 } |
468 } |
390 |
469 |
391 return $this->set( $key, $data, $group, (int) $expire ); |
470 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) { |
|
471 $this->cache[ $group ][ $key ] = 0; |
|
472 } |
|
473 |
|
474 $offset = (int) $offset; |
|
475 |
|
476 $this->cache[ $group ][ $key ] -= $offset; |
|
477 |
|
478 if ( $this->cache[ $group ][ $key ] < 0 ) { |
|
479 $this->cache[ $group ][ $key ] = 0; |
|
480 } |
|
481 |
|
482 return $this->cache[ $group ][ $key ]; |
|
483 } |
|
484 |
|
485 /** |
|
486 * Clears the object cache of all data. |
|
487 * |
|
488 * @since 2.0.0 |
|
489 * |
|
490 * @return true Always returns true. |
|
491 */ |
|
492 public function flush() { |
|
493 $this->cache = array(); |
|
494 |
|
495 return true; |
|
496 } |
|
497 |
|
498 /** |
|
499 * Sets the list of global cache groups. |
|
500 * |
|
501 * @since 3.0.0 |
|
502 * |
|
503 * @param string|string[] $groups List of groups that are global. |
|
504 */ |
|
505 public function add_global_groups( $groups ) { |
|
506 $groups = (array) $groups; |
|
507 |
|
508 $groups = array_fill_keys( $groups, true ); |
|
509 $this->global_groups = array_merge( $this->global_groups, $groups ); |
|
510 } |
|
511 |
|
512 /** |
|
513 * Switches the internal blog ID. |
|
514 * |
|
515 * This changes the blog ID used to create keys in blog specific groups. |
|
516 * |
|
517 * @since 3.5.0 |
|
518 * |
|
519 * @param int $blog_id Blog ID. |
|
520 */ |
|
521 public function switch_to_blog( $blog_id ) { |
|
522 $blog_id = (int) $blog_id; |
|
523 $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; |
392 } |
524 } |
393 |
525 |
394 /** |
526 /** |
395 * Resets cache keys. |
527 * Resets cache keys. |
396 * |
528 * |
397 * @since 3.0.0 |
529 * @since 3.0.0 |
398 * |
530 * |
399 * @deprecated 3.5.0 Use switch_to_blog() |
531 * @deprecated 3.5.0 Use WP_Object_Cache::switch_to_blog() |
400 * @see switch_to_blog() |
532 * @see switch_to_blog() |
401 */ |
533 */ |
402 public function reset() { |
534 public function reset() { |
403 _deprecated_function( __FUNCTION__, '3.5.0', 'switch_to_blog()' ); |
535 _deprecated_function( __FUNCTION__, '3.5.0', 'WP_Object_Cache::switch_to_blog()' ); |
404 |
536 |
405 // Clear out non-global caches since the blog ID has changed. |
537 // Clear out non-global caches since the blog ID has changed. |
406 foreach ( array_keys( $this->cache ) as $group ) { |
538 foreach ( array_keys( $this->cache ) as $group ) { |
407 if ( ! isset( $this->global_groups[ $group ] ) ) { |
539 if ( ! isset( $this->global_groups[ $group ] ) ) { |
408 unset( $this->cache[ $group ] ); |
540 unset( $this->cache[ $group ] ); |
409 } |
541 } |
410 } |
542 } |
411 } |
|
412 |
|
413 /** |
|
414 * Sets the data contents into the cache. |
|
415 * |
|
416 * The cache contents are grouped by the $group parameter followed by the |
|
417 * $key. This allows for duplicate IDs in unique groups. Therefore, naming of |
|
418 * the group should be used with care and should follow normal function |
|
419 * naming guidelines outside of core WordPress usage. |
|
420 * |
|
421 * The $expire parameter is not used, because the cache will automatically |
|
422 * expire for each time a page is accessed and PHP finishes. The method is |
|
423 * more for cache plugins which use files. |
|
424 * |
|
425 * @since 2.0.0 |
|
426 * |
|
427 * @param int|string $key What to call the contents in the cache. |
|
428 * @param mixed $data The contents to store in the cache. |
|
429 * @param string $group Optional. Where to group the cache contents. Default 'default'. |
|
430 * @param int $expire Not Used. |
|
431 * @return true Always returns true. |
|
432 */ |
|
433 public function set( $key, $data, $group = 'default', $expire = 0 ) { |
|
434 if ( empty( $group ) ) { |
|
435 $group = 'default'; |
|
436 } |
|
437 |
|
438 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { |
|
439 $key = $this->blog_prefix . $key; |
|
440 } |
|
441 |
|
442 if ( is_object( $data ) ) { |
|
443 $data = clone $data; |
|
444 } |
|
445 |
|
446 $this->cache[ $group ][ $key ] = $data; |
|
447 return true; |
|
448 } |
543 } |
449 |
544 |
450 /** |
545 /** |
451 * Echoes the stats of the caching. |
546 * Echoes the stats of the caching. |
452 * |
547 * |