diff -r be944660c56a -r 3d72ae0968f4 wp/wp-includes/class-wp-object-cache.php --- a/wp/wp-includes/class-wp-object-cache.php Wed Sep 21 18:19:35 2022 +0200 +++ b/wp/wp-includes/class-wp-object-cache.php Tue Sep 27 16:37:53 2022 +0200 @@ -130,6 +130,19 @@ } /** + * Serves as a utility function to determine whether a key exists in the cache. + * + * @since 3.4.0 + * + * @param int|string $key Cache key to check for existence. + * @param string $group Cache group for the key existence check. + * @return bool Whether the key exists in the cache for the given group. + */ + protected function _exists( $key, $group ) { + return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) ); + } + + /** * Adds data to the cache if it doesn't already exist. * * @since 2.0.0 @@ -141,7 +154,8 @@ * @param int|string $key What to call the contents in the cache. * @param mixed $data The contents to store in the cache. * @param string $group Optional. Where to group the cache contents. Default 'default'. - * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration). + * @param int $expire Optional. When to expire the cache contents, in seconds. + * Default 0 (no expiration). * @return bool True on success, false if cache key and group already exist. */ public function add( $key, $data, $group = 'default', $expire = 0 ) { @@ -166,30 +180,79 @@ } /** - * Sets the list of global cache groups. + * Adds multiple values to the cache in one call. + * + * @since 6.0.0 * - * @since 3.0.0 - * - * @param string|string[] $groups List of groups that are global. + * @param array $data Array of keys and values to be added. + * @param string $group Optional. Where the cache contents are grouped. Default empty. + * @param int $expire Optional. When to expire the cache contents, in seconds. + * Default 0 (no expiration). + * @return bool[] Array of return values, grouped by key. Each value is either + * true on success, or false if cache key and group already exist. */ - public function add_global_groups( $groups ) { - $groups = (array) $groups; + public function add_multiple( array $data, $group = '', $expire = 0 ) { + $values = array(); - $groups = array_fill_keys( $groups, true ); - $this->global_groups = array_merge( $this->global_groups, $groups ); + foreach ( $data as $key => $value ) { + $values[ $key ] = $this->add( $key, $value, $group, $expire ); + } + + return $values; } /** - * Decrements numeric cache item's value. + * Replaces the contents in the cache, if contents already exist. * - * @since 3.3.0 + * @since 2.0.0 + * + * @see WP_Object_Cache::set() * - * @param int|string $key The cache key to decrement. - * @param int $offset Optional. The amount by which to decrement the item's value. Default 1. - * @param string $group Optional. The group the key is in. Default 'default'. - * @return int|false The item's new value on success, false on failure. + * @param int|string $key What to call the contents in the cache. + * @param mixed $data The contents to store in the cache. + * @param string $group Optional. Where to group the cache contents. Default 'default'. + * @param int $expire Optional. When to expire the cache contents, in seconds. + * Default 0 (no expiration). + * @return bool True if contents were replaced, false if original value does not exist. */ - public function decr( $key, $offset = 1, $group = 'default' ) { + public function replace( $key, $data, $group = 'default', $expire = 0 ) { + if ( empty( $group ) ) { + $group = 'default'; + } + + $id = $key; + if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { + $id = $this->blog_prefix . $key; + } + + if ( ! $this->_exists( $id, $group ) ) { + return false; + } + + return $this->set( $key, $data, $group, (int) $expire ); + } + + /** + * Sets the data contents into the cache. + * + * The cache contents are grouped by the $group parameter followed by the + * $key. This allows for duplicate IDs in unique groups. Therefore, naming of + * the group should be used with care and should follow normal function + * naming guidelines outside of core WordPress usage. + * + * The $expire parameter is not used, because the cache will automatically + * expire for each time a page is accessed and PHP finishes. The method is + * more for cache plugins which use files. + * + * @since 2.0.0 + * + * @param int|string $key What to call the contents in the cache. + * @param mixed $data The contents to store in the cache. + * @param string $group Optional. Where to group the cache contents. Default 'default'. + * @param int $expire Optional. Not used. + * @return true Always returns true. + */ + public function set( $key, $data, $group = 'default', $expire = 0 ) { if ( empty( $group ) ) { $group = 'default'; } @@ -198,65 +261,33 @@ $key = $this->blog_prefix . $key; } - if ( ! $this->_exists( $key, $group ) ) { - return false; - } - - if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) { - $this->cache[ $group ][ $key ] = 0; - } - - $offset = (int) $offset; - - $this->cache[ $group ][ $key ] -= $offset; - - if ( $this->cache[ $group ][ $key ] < 0 ) { - $this->cache[ $group ][ $key ] = 0; + if ( is_object( $data ) ) { + $data = clone $data; } - return $this->cache[ $group ][ $key ]; - } - - /** - * Removes the contents of the cache key in the group. - * - * If the cache key does not exist in the group, then nothing will happen. - * - * @since 2.0.0 - * - * @param int|string $key What the contents in the cache are called. - * @param string $group Optional. Where the cache contents are grouped. Default 'default'. - * @param bool $deprecated Optional. Unused. Default false. - * @return bool False if the contents weren't deleted and true on success. - */ - public function delete( $key, $group = 'default', $deprecated = false ) { - if ( empty( $group ) ) { - $group = 'default'; - } - - if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { - $key = $this->blog_prefix . $key; - } - - if ( ! $this->_exists( $key, $group ) ) { - return false; - } - - unset( $this->cache[ $group ][ $key ] ); + $this->cache[ $group ][ $key ] = $data; return true; } /** - * Clears the object cache of all data. + * Sets multiple values to the cache in one call. * - * @since 2.0.0 + * @since 6.0.0 * - * @return true Always returns true. + * @param array $data Array of key and value to be set. + * @param string $group Optional. Where the cache contents are grouped. Default empty. + * @param int $expire Optional. When to expire the cache contents, in seconds. + * Default 0 (no expiration). + * @return bool[] Array of return values, grouped by key. Each value is always true. */ - public function flush() { - $this->cache = array(); + public function set_multiple( array $data, $group = '', $expire = 0 ) { + $values = array(); - return true; + foreach ( $data as $key => $value ) { + $values[ $key ] = $this->set( $key, $value, $group, $expire ); + } + + return $values; } /** @@ -311,7 +342,8 @@ * @param string $group Optional. Where the cache contents are grouped. Default 'default'. * @param bool $force Optional. Whether to force an update of the local cache * from the persistent cache. Default false. - * @return array Array of values organized into groups. + * @return array Array of return values, grouped by key. Each value is either + * the cache contents on success, or false on failure. */ public function get_multiple( $keys, $group = 'default', $force = false ) { $values = array(); @@ -324,12 +356,62 @@ } /** + * Removes the contents of the cache key in the group. + * + * If the cache key does not exist in the group, then nothing will happen. + * + * @since 2.0.0 + * + * @param int|string $key What the contents in the cache are called. + * @param string $group Optional. Where the cache contents are grouped. Default 'default'. + * @param bool $deprecated Optional. Unused. Default false. + * @return bool True on success, false if the contents were not deleted. + */ + public function delete( $key, $group = 'default', $deprecated = false ) { + if ( empty( $group ) ) { + $group = 'default'; + } + + if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { + $key = $this->blog_prefix . $key; + } + + if ( ! $this->_exists( $key, $group ) ) { + return false; + } + + unset( $this->cache[ $group ][ $key ] ); + return true; + } + + /** + * Deletes multiple values from the cache in one call. + * + * @since 6.0.0 + * + * @param array $keys Array of keys to be deleted. + * @param string $group Optional. Where the cache contents are grouped. Default empty. + * @return bool[] Array of return values, grouped by key. Each value is either + * true on success, or false if the contents were not deleted. + */ + public function delete_multiple( array $keys, $group = '' ) { + $values = array(); + + foreach ( $keys as $key ) { + $values[ $key ] = $this->delete( $key, $group ); + } + + return $values; + } + + /** * Increments numeric cache item's value. * * @since 3.3.0 * - * @param int|string $key The cache key to increment - * @param int $offset Optional. The amount by which to increment the item's value. Default 1. + * @param int|string $key The cache key to increment. + * @param int $offset Optional. The amount by which to increment the item's value. + * Default 1. * @param string $group Optional. The group the key is in. Default 'default'. * @return int|false The item's new value on success, false on failure. */ @@ -362,75 +444,17 @@ } /** - * Replaces the contents in the cache, if contents already exist. - * - * @since 2.0.0 - * - * @see WP_Object_Cache::set() + * Decrements numeric cache item's value. * - * @param int|string $key What to call the contents in the cache. - * @param mixed $data The contents to store in the cache. - * @param string $group Optional. Where to group the cache contents. Default 'default'. - * @param int $expire Optional. When to expire the cache contents. Default 0 (no expiration). - * @return bool False if not exists, true if contents were replaced. - */ - public function replace( $key, $data, $group = 'default', $expire = 0 ) { - if ( empty( $group ) ) { - $group = 'default'; - } - - $id = $key; - if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) { - $id = $this->blog_prefix . $key; - } - - if ( ! $this->_exists( $id, $group ) ) { - return false; - } - - return $this->set( $key, $data, $group, (int) $expire ); - } - - /** - * Resets cache keys. - * - * @since 3.0.0 + * @since 3.3.0 * - * @deprecated 3.5.0 Use switch_to_blog() - * @see switch_to_blog() + * @param int|string $key The cache key to decrement. + * @param int $offset Optional. The amount by which to decrement the item's value. + * Default 1. + * @param string $group Optional. The group the key is in. Default 'default'. + * @return int|false The item's new value on success, false on failure. */ - public function reset() { - _deprecated_function( __FUNCTION__, '3.5.0', 'switch_to_blog()' ); - - // Clear out non-global caches since the blog ID has changed. - foreach ( array_keys( $this->cache ) as $group ) { - if ( ! isset( $this->global_groups[ $group ] ) ) { - unset( $this->cache[ $group ] ); - } - } - } - - /** - * Sets the data contents into the cache. - * - * The cache contents are grouped by the $group parameter followed by the - * $key. This allows for duplicate IDs in unique groups. Therefore, naming of - * the group should be used with care and should follow normal function - * naming guidelines outside of core WordPress usage. - * - * The $expire parameter is not used, because the cache will automatically - * expire for each time a page is accessed and PHP finishes. The method is - * more for cache plugins which use files. - * - * @since 2.0.0 - * - * @param int|string $key What to call the contents in the cache. - * @param mixed $data The contents to store in the cache. - * @param string $group Optional. Where to group the cache contents. Default 'default'. - * @param int $expire Not Used. - * @return true Always returns true. - */ - public function set( $key, $data, $group = 'default', $expire = 0 ) { + public function decr( $key, $offset = 1, $group = 'default' ) { if ( empty( $group ) ) { $group = 'default'; } @@ -439,15 +463,86 @@ $key = $this->blog_prefix . $key; } - if ( is_object( $data ) ) { - $data = clone $data; + if ( ! $this->_exists( $key, $group ) ) { + return false; + } + + if ( ! is_numeric( $this->cache[ $group ][ $key ] ) ) { + $this->cache[ $group ][ $key ] = 0; + } + + $offset = (int) $offset; + + $this->cache[ $group ][ $key ] -= $offset; + + if ( $this->cache[ $group ][ $key ] < 0 ) { + $this->cache[ $group ][ $key ] = 0; } - $this->cache[ $group ][ $key ] = $data; + return $this->cache[ $group ][ $key ]; + } + + /** + * Clears the object cache of all data. + * + * @since 2.0.0 + * + * @return true Always returns true. + */ + public function flush() { + $this->cache = array(); + return true; } /** + * Sets the list of global cache groups. + * + * @since 3.0.0 + * + * @param string|string[] $groups List of groups that are global. + */ + public function add_global_groups( $groups ) { + $groups = (array) $groups; + + $groups = array_fill_keys( $groups, true ); + $this->global_groups = array_merge( $this->global_groups, $groups ); + } + + /** + * Switches the internal blog ID. + * + * This changes the blog ID used to create keys in blog specific groups. + * + * @since 3.5.0 + * + * @param int $blog_id Blog ID. + */ + public function switch_to_blog( $blog_id ) { + $blog_id = (int) $blog_id; + $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; + } + + /** + * Resets cache keys. + * + * @since 3.0.0 + * + * @deprecated 3.5.0 Use WP_Object_Cache::switch_to_blog() + * @see switch_to_blog() + */ + public function reset() { + _deprecated_function( __FUNCTION__, '3.5.0', 'WP_Object_Cache::switch_to_blog()' ); + + // Clear out non-global caches since the blog ID has changed. + foreach ( array_keys( $this->cache ) as $group ) { + if ( ! isset( $this->global_groups[ $group ] ) ) { + unset( $this->cache[ $group ] ); + } + } + } + + /** * Echoes the stats of the caching. * * Gives the cache hits, and cache misses. Also prints every cached group, @@ -466,31 +561,4 @@ } echo ''; } - - /** - * Switches the internal blog ID. - * - * This changes the blog ID used to create keys in blog specific groups. - * - * @since 3.5.0 - * - * @param int $blog_id Blog ID. - */ - public function switch_to_blog( $blog_id ) { - $blog_id = (int) $blog_id; - $this->blog_prefix = $this->multisite ? $blog_id . ':' : ''; - } - - /** - * Serves as a utility function to determine whether a key exists in the cache. - * - * @since 3.4.0 - * - * @param int|string $key Cache key to check for existence. - * @param string $group Cache group for the key existence check. - * @return bool Whether the key exists in the cache for the given group. - */ - protected function _exists( $key, $group ) { - return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) ); - } }