Changeset 52706 for trunk/src/wp-includes/class-wp-object-cache.php
- Timestamp:
- 02/11/2022 06:47:38 PM (5 years ago)
- File:
-
- 1 edited
-
trunk/src/wp-includes/class-wp-object-cache.php (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-object-cache.php
r52705 r52706 131 131 132 132 /** 133 * Serves as a utility function to determine whether a key exists in the cache. 134 * 135 * @since 3.4.0 136 * 137 * @param int|string $key Cache key to check for existence. 138 * @param string $group Cache group for the key existence check. 139 * @return bool Whether the key exists in the cache for the given group. 140 */ 141 protected function _exists( $key, $group ) { 142 return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) ); 143 } 144 145 /** 133 146 * Adds data to the cache if it doesn't already exist. 134 147 * … … 168 181 169 182 /** 170 * Sets the list of global cache groups. 171 * 172 * @since 3.0.0 173 * 174 * @param string|string[] $groups List of groups that are global. 175 */ 176 public function add_global_groups( $groups ) { 177 $groups = (array) $groups; 178 179 $groups = array_fill_keys( $groups, true ); 180 $this->global_groups = array_merge( $this->global_groups, $groups ); 181 } 182 183 /** 184 * Decrements numeric cache item's value. 185 * 186 * @since 3.3.0 187 * 188 * @param int|string $key The cache key to decrement. 189 * @param int $offset Optional. The amount by which to decrement the item's value. 190 * Default 1. 191 * @param string $group Optional. The group the key is in. Default 'default'. 192 * @return int|false The item's new value on success, false on failure. 193 */ 194 public function decr( $key, $offset = 1, $group = 'default' ) { 183 * Adds multiple values to the cache in one call. 184 * 185 * @since 6.0.0 186 * 187 * @param array $data Array of keys and values to be added. 188 * @param string $group Optional. Where the cache contents are grouped. Default empty. 189 * @param int $expire Optional. When to expire the cache contents, in seconds. 190 * Default 0 (no expiration). 191 * @return array Array of return values. 192 */ 193 public function add_multiple( array $data, $group = '', $expire = 0 ) { 194 $values = array(); 195 196 foreach ( $data as $key => $value ) { 197 $values[ $key ] = $this->add( $key, $value, $group, $expire ); 198 } 199 200 return $values; 201 } 202 203 /** 204 * Replaces the contents in the cache, if contents already exist. 205 * 206 * @since 2.0.0 207 * 208 * @see WP_Object_Cache::set() 209 * 210 * @param int|string $key What to call the contents in the cache. 211 * @param mixed $data The contents to store in the cache. 212 * @param string $group Optional. Where to group the cache contents. Default 'default'. 213 * @param int $expire Optional. When to expire the cache contents, in seconds. 214 * Default 0 (no expiration). 215 * @return bool True if contents were replaced, false if original value does not exist. 216 */ 217 public function replace( $key, $data, $group = 'default', $expire = 0 ) { 218 if ( empty( $group ) ) { 219 $group = 'default'; 220 } 221 222 $id = $key; 223 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { 224 $id = $this->blog_prefix . $key; 225 } 226 227 if ( ! $this->_exists( $id, $group ) ) { 228 return false; 229 } 230 231 return $this->set( $key, $data, $group, (int) $expire ); 232 } 233 234 /** 235 * Sets the data contents into the cache. 236 * 237 * The cache contents are grouped by the $group parameter followed by the 238 * $key. This allows for duplicate IDs in unique groups. Therefore, naming of 239 * the group should be used with care and should follow normal function 240 * naming guidelines outside of core WordPress usage. 241 * 242 * The $expire parameter is not used, because the cache will automatically 243 * expire for each time a page is accessed and PHP finishes. The method is 244 * more for cache plugins which use files. 245 * 246 * @since 2.0.0 247 * 248 * @param int|string $key What to call the contents in the cache. 249 * @param mixed $data The contents to store in the cache. 250 * @param string $group Optional. Where to group the cache contents. Default 'default'. 251 * @param int $expire Optional. Not used. 252 * @return true Always returns true. 253 */ 254 public function set( $key, $data, $group = 'default', $expire = 0 ) { 195 255 if ( empty( $group ) ) { 196 256 $group = 'default'; … … 201 261 } 202 262 203 if ( ! $this->_exists( $key, $group ) ) { 204 return false; 205 } 206 207 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) { 208 $this->cache[ $group ][ $key ] = 0; 209 } 210 211 $offset = (int) $offset; 212 213 $this->cache[ $group ][ $key ] -= $offset; 214 215 if ( $this->cache[ $group ][ $key ] < 0 ) { 216 $this->cache[ $group ][ $key ] = 0; 217 } 218 219 return $this->cache[ $group ][ $key ]; 220 } 221 222 /** 223 * Removes the contents of the cache key in the group. 224 * 225 * If the cache key does not exist in the group, then nothing will happen. 226 * 227 * @since 2.0.0 228 * 229 * @param int|string $key What the contents in the cache are called. 230 * @param string $group Optional. Where the cache contents are grouped. Default 'default'. 231 * @param bool $deprecated Optional. Unused. Default false. 232 * @return bool True on success, false if the contents were not deleted. 233 */ 234 public function delete( $key, $group = 'default', $deprecated = false ) { 235 if ( empty( $group ) ) { 236 $group = 'default'; 237 } 238 239 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { 240 $key = $this->blog_prefix . $key; 241 } 242 243 if ( ! $this->_exists( $key, $group ) ) { 244 return false; 245 } 246 247 unset( $this->cache[ $group ][ $key ] ); 263 if ( is_object( $data ) ) { 264 $data = clone $data; 265 } 266 267 $this->cache[ $group ][ $key ] = $data; 248 268 return true; 249 269 } 250 270 251 271 /** 252 * Clears the object cache of all data. 253 * 254 * @since 2.0.0 255 * 256 * @return true Always returns true. 257 */ 258 public function flush() { 259 $this->cache = array(); 260 261 return true; 272 * Sets multiple values to the cache in one call. 273 * 274 * @since 6.0.0 275 * 276 * @param array $data Array of key and value to be set. 277 * @param string $group Optional. Where the cache contents are grouped. Default empty. 278 * @param int $expire Optional. When to expire the cache contents, in seconds. 279 * Default 0 (no expiration). 280 * @return array Array of return values. 281 */ 282 public function set_multiple( array $data, $group = '', $expire = 0 ) { 283 $values = array(); 284 285 foreach ( $data as $key => $value ) { 286 $values[ $key ] = $this->set( $key, $value, $group, $expire ); 287 } 288 289 return $values; 262 290 } 263 291 … … 327 355 328 356 /** 357 * Removes the contents of the cache key in the group. 358 * 359 * If the cache key does not exist in the group, then nothing will happen. 360 * 361 * @since 2.0.0 362 * 363 * @param int|string $key What the contents in the cache are called. 364 * @param string $group Optional. Where the cache contents are grouped. Default 'default'. 365 * @param bool $deprecated Optional. Unused. Default false. 366 * @return bool True on success, false if the contents were not deleted. 367 */ 368 public function delete( $key, $group = 'default', $deprecated = false ) { 369 if ( empty( $group ) ) { 370 $group = 'default'; 371 } 372 373 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { 374 $key = $this->blog_prefix . $key; 375 } 376 377 if ( ! $this->_exists( $key, $group ) ) { 378 return false; 379 } 380 381 unset( $this->cache[ $group ][ $key ] ); 382 return true; 383 } 384 385 /** 329 386 * Deletes multiple values from the cache in one call. 330 387 * … … 340 397 foreach ( $keys as $key ) { 341 398 $values[ $key ] = $this->delete( $key, $group ); 342 }343 344 return $values;345 }346 347 /**348 * Adds multiple values to the cache in one call.349 *350 * @since 6.0.0351 *352 * @param array $data Array of keys and values to be added.353 * @param string $group Optional. Where the cache contents are grouped. Default empty.354 * @param int $expire Optional. When to expire the cache contents, in seconds.355 * Default 0 (no expiration).356 * @return array Array of return values.357 */358 public function add_multiple( array $data, $group = '', $expire = 0 ) {359 $values = array();360 361 foreach ( $data as $key => $value ) {362 $values[ $key ] = $this->add( $key, $value, $group, $expire );363 }364 365 return $values;366 }367 368 /**369 * Sets multiple values to the cache in one call.370 *371 * @since 6.0.0372 *373 * @param array $data Array of key and value to be set.374 * @param string $group Optional. Where the cache contents are grouped. Default empty.375 * @param int $expire Optional. When to expire the cache contents, in seconds.376 * Default 0 (no expiration).377 * @return array Array of return values.378 */379 public function set_multiple( array $data, $group = '', $expire = 0 ) {380 $values = array();381 382 foreach ( $data as $key => $value ) {383 $values[ $key ] = $this->set( $key, $value, $group, $expire );384 399 } 385 400 … … 427 442 428 443 /** 429 * Replaces the contents in the cache, if contents already exist. 430 * 431 * @since 2.0.0 432 * 433 * @see WP_Object_Cache::set() 434 * 435 * @param int|string $key What to call the contents in the cache. 436 * @param mixed $data The contents to store in the cache. 437 * @param string $group Optional. Where to group the cache contents. Default 'default'. 438 * @param int $expire Optional. When to expire the cache contents, in seconds. 439 * Default 0 (no expiration). 440 * @return bool True if contents were replaced, false if original value does not exist. 441 */ 442 public function replace( $key, $data, $group = 'default', $expire = 0 ) { 443 if ( empty( $group ) ) { 444 $group = 'default'; 445 } 446 447 $id = $key; 448 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { 449 $id = $this->blog_prefix . $key; 450 } 451 452 if ( ! $this->_exists( $id, $group ) ) { 444 * Decrements numeric cache item's value. 445 * 446 * @since 3.3.0 447 * 448 * @param int|string $key The cache key to decrement. 449 * @param int $offset Optional. The amount by which to decrement the item's value. 450 * Default 1. 451 * @param string $group Optional. The group the key is in. Default 'default'. 452 * @return int|false The item's new value on success, false on failure. 453 */ 454 public function decr( $key, $offset = 1, $group = 'default' ) { 455 if ( empty( $group ) ) { 456 $group = 'default'; 457 } 458 459 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { 460 $key = $this->blog_prefix . $key; 461 } 462 463 if ( ! $this->_exists( $key, $group ) ) { 453 464 return false; 454 465 } 455 466 456 return $this->set( $key, $data, $group, (int) $expire ); 467 if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) { 468 $this->cache[ $group ][ $key ] = 0; 469 } 470 471 $offset = (int) $offset; 472 473 $this->cache[ $group ][ $key ] -= $offset; 474 475 if ( $this->cache[ $group ][ $key ] < 0 ) { 476 $this->cache[ $group ][ $key ] = 0; 477 } 478 479 return $this->cache[ $group ][ $key ]; 480 } 481 482 /** 483 * Clears the object cache of all data. 484 * 485 * @since 2.0.0 486 * 487 * @return true Always returns true. 488 */ 489 public function flush() { 490 $this->cache = array(); 491 492 return true; 493 } 494 495 /** 496 * Sets the list of global cache groups. 497 * 498 * @since 3.0.0 499 * 500 * @param string|string[] $groups List of groups that are global. 501 */ 502 public function add_global_groups( $groups ) { 503 $groups = (array) $groups; 504 505 $groups = array_fill_keys( $groups, true ); 506 $this->global_groups = array_merge( $this->global_groups, $groups ); 507 } 508 509 /** 510 * Switches the internal blog ID. 511 * 512 * This changes the blog ID used to create keys in blog specific groups. 513 * 514 * @since 3.5.0 515 * 516 * @param int $blog_id Blog ID. 517 */ 518 public function switch_to_blog( $blog_id ) { 519 $blog_id = (int) $blog_id; 520 $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; 457 521 } 458 522 … … 474 538 } 475 539 } 476 }477 478 /**479 * Sets the data contents into the cache.480 *481 * The cache contents are grouped by the $group parameter followed by the482 * $key. This allows for duplicate IDs in unique groups. Therefore, naming of483 * the group should be used with care and should follow normal function484 * naming guidelines outside of core WordPress usage.485 *486 * The $expire parameter is not used, because the cache will automatically487 * expire for each time a page is accessed and PHP finishes. The method is488 * more for cache plugins which use files.489 *490 * @since 2.0.0491 *492 * @param int|string $key What to call the contents in the cache.493 * @param mixed $data The contents to store in the cache.494 * @param string $group Optional. Where to group the cache contents. Default 'default'.495 * @param int $expire Optional. Not used.496 * @return true Always returns true.497 */498 public function set( $key, $data, $group = 'default', $expire = 0 ) {499 if ( empty( $group ) ) {500 $group = 'default';501 }502 503 if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {504 $key = $this->blog_prefix . $key;505 }506 507 if ( is_object( $data ) ) {508 $data = clone $data;509 }510 511 $this->cache[ $group ][ $key ] = $data;512 return true;513 540 } 514 541 … … 532 559 echo '</ul>'; 533 560 } 534 535 /**536 * Switches the internal blog ID.537 *538 * This changes the blog ID used to create keys in blog specific groups.539 *540 * @since 3.5.0541 *542 * @param int $blog_id Blog ID.543 */544 public function switch_to_blog( $blog_id ) {545 $blog_id = (int) $blog_id;546 $this->blog_prefix = $this->multisite ? $blog_id . ':' : '';547 }548 549 /**550 * Serves as a utility function to determine whether a key exists in the cache.551 *552 * @since 3.4.0553 *554 * @param int|string $key Cache key to check for existence.555 * @param string $group Cache group for the key existence check.556 * @return bool Whether the key exists in the cache for the given group.557 */558 protected function _exists( $key, $group ) {559 return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) );560 }561 561 }
Note:
See TracChangeset
for help on using the changeset viewer.