From 8d25a65892452f8776116c51ca091e562d960406 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 8 Oct 2022 13:39:43 +0000 Subject: [PATCH] Tests: Add `wp_cache_*_multiple()` functions to Memcached implementation used in the test suite. Since this object cache implementation was added, WordPress has introduced a variety of caching API improvements: * `wp_cache_add_multiple()` * `wp_cache_set_multiple()` * `wp_cache_get_multiple()` * `wp_cache_delete_multiple()` Although WordPress core provides a compatibility layer if these functions are missing from third-party object caches, this commit updates the Memcached object cache used in the test suite to implement these new functions directly. Follow-up to [40561], [47938], [47944], [52700], [52703], [52706], [52708]. Props petitphp, spacedmonkey, tillkruss, SergeyBiryukov. Fixes #54864. git-svn-id: https://develop.svn.wordpress.org/trunk@54423 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/object-cache.php | 140 ++++++++++++++++++++++++ 1 file changed, 140 insertions(+) diff --git a/tests/phpunit/includes/object-cache.php b/tests/phpunit/includes/object-cache.php index f07460c24f..04b77c9ee5 100644 --- a/tests/phpunit/includes/object-cache.php +++ b/tests/phpunit/includes/object-cache.php @@ -40,6 +40,21 @@ function wp_cache_add_by_key( $server_key, $key, $value, $group = '', $expiratio return $wp_object_cache->addByKey( $server_key, $key, $value, $group, $expiration ); } +/** + * Adds multiple values to the cache in one call, if the cache keys don't already exist. + * + * @param array $items Array of keys and values to be added. + * @param string $group Optional. Where the cache contents are grouped. Default empty. + * @param int $expiration 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. + */ +function wp_cache_add_multiple( array $items, $group = '', $expiration = 0 ) { + global $wp_object_cache; + return $wp_object_cache->addMultiple( $items, $group, $expiration ); +} + /** * Adds a single server to the list of Memcached servers. * @@ -247,6 +262,19 @@ function wp_cache_delete_by_key( $server_key, $key, $group = '', $time = 0 ) { return $wp_object_cache->deleteByKey( $server_key, $key, $group, $time ); } +/** + * Deletes multiple values from the cache in one call. + * + * @param array $keys Array of keys under which the cache to 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. + */ +function wp_cache_delete_multiple( array $keys, $group = '' ) { + global $wp_object_cache; + return $wp_object_cache->deleteMultiple( $keys, $group ); +} + /** * Fetches the next result. * @@ -439,6 +467,21 @@ function wp_cache_get_multi_by_key( $server_key, $keys, $groups = '', &$cas_toke } } +/** + * Retrieves multiple values from the cache in one call. + * + * @param array $keys Array of keys under which the cache contents are stored. + * @param string $group Optional. Where the cache contents are grouped. Default empty. + * @param bool $force Optional. Whether to force an update of the local cache + * from the persistent cache. Default false. + * @return array Array of return values, grouped by key. Each value is either + * the cache contents on success, or false on failure. + */ +function wp_cache_get_multiple( $keys, $group = '', $force = false ) { + global $wp_object_cache; + return $wp_object_cache->getMultiple( $keys, $group, $force ); +} + /** * Retrieves a Memcached option value. * @@ -719,6 +762,23 @@ function wp_cache_set_multi_by_key( $server_key, $items, $groups = 'default', $e return $wp_object_cache->setMultiByKey( $server_key, $items, $groups, $expiration ); } +/** + * Sets multiple values to the cache in one call. + * + * Differs from wp_cache_add_multiple() in that it will always write data. + * + * @param array $items Array of keys and values to be set. + * @param string $group Optional. Where the cache contents are grouped. Default empty. + * @param int $expiration 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 on failure. + */ +function wp_cache_set_multiple( array $items, $group = '', $expiration = 0 ) { + global $wp_object_cache; + return $wp_object_cache->setMultiple( $items, $group, $expiration ); +} + /** * Sets a Memcached option. * @@ -956,6 +1016,26 @@ class WP_Object_Cache { return $this->add( $key, $value, $group, $expiration, $server_key, true ); } + /** + * Adds multiple values to cache. + * + * @param array $items Array of keys and values to be added. + * @param string $group Optional. Where the cache contents are grouped. Default empty. + * @param int $expiration 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 addMultiple( array $items, $group = '', $expiration = 0 ) { + $values = array(); + + foreach ( $items as $key => $value ) { + $values[ $key ] = $this->add( $key, $value, $group, $expiration ); + } + + return $values; + } + /** * Adds a single server to the list of Memcached servers. * @@ -1259,6 +1339,24 @@ class WP_Object_Cache { return $this->delete( $key, $group, $time, $server_key, true ); } + /** + * Removes multiple items from the cache. + * + * @param array $keys Array of keys under which the cache to 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 deleteMultiple( $keys, $group ) { + $values = array(); + + foreach ( $keys as $key ) { + $values[ $key ] = $this->delete( $key, $group ); + } + + return $values; + } + /** * Fetches the next result. * @@ -1536,6 +1634,28 @@ class WP_Object_Cache { } } + /** + * Get multiple items from the cache. + * + * @param array $keys Array of keys under which the cache contents are stored. + * @param string $group Optional. Where the cache contents are grouped. Default empty. + * @param bool $force Optional. Whether to force an update of the local cache + * from the persistent cache. Default false. + * @return array Array of return values, grouped by key. Each value is either + * the cache contents on success, or false on failure. + */ + public function getMultiple( $keys, $group = '', $force = false ) { + $values = array(); + + foreach ( $keys as $key ) { + $found = null; + $value = $this->get( $key, $group, $force, $found ); + $values[ $key ] = $found ? $value : false; + } + + return $values; + } + /** * Retrieves a Memcached option value. * @@ -1952,6 +2072,26 @@ class WP_Object_Cache { return $this->setMulti( $items, $groups, $expiration, $server_key, true ); } + /** + * Sets multiple values in cache. + * + * @param array $items Array of keys and values to be set. + * @param string $group Optional. Where the cache contents are grouped. Default empty. + * @param int $expiration 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 on failure. + */ + public function setMultiple( array $items, $group = '', $expiration = 0 ) { + $values = array(); + + foreach ( $items as $key => $value ) { + $values[ $key ] = $this->set( $key, $value, $group, $expiration ); + } + + return $values; + } + /** * Sets a Memcached option. *