From 3e2b649351e754d0dec7fcc7513d00dde84ed466 Mon Sep 17 00:00:00 2001 From: whyisjake Date: Tue, 9 Jun 2020 19:47:13 +0000 Subject: [PATCH] Cache API: Introduce `wp_cache_get_multi()`. Many caching backend have support for multiple gets in a single request. This brings that support to core, with a compatability fallback that will loop over requests if needed. Fixes: #20875. Props: nacin, tollmanz, wonderboymusic, ryan, jeremyfelt, spacedmonkey, boonebgorges, dd32, rmccue, ocean90, jipmoors, johnjamesjacoby, tillkruess, donmhico, davidbaumwald, SergeyBiryukov, whyisjake. Built from https://develop.svn.wordpress.org/trunk@47938 git-svn-id: http://core.svn.wordpress.org/trunk@47711 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/cache.php | 18 ++++++++++++++++++ wp-includes/class-wp-object-cache.php | 21 +++++++++++++++++++++ wp-includes/load.php | 2 ++ wp-includes/version.php | 2 +- 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/wp-includes/cache.php b/wp-includes/cache.php index 9957235873..23c5883ec0 100644 --- a/wp-includes/cache.php +++ b/wp-includes/cache.php @@ -126,6 +126,24 @@ function wp_cache_get( $key, $group = '', $force = false, &$found = null ) { return $wp_object_cache->get( $key, $group, $force, $found ); } +/** + * Gets multiple values from cache in one call. + * + * @since 5.5.0 + * @see WP_Object_Cache::get_multiple() + * + * @param array $keys Array of keys to get from group. + * @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|bool Array of values. + */ +function wp_cache_get_multiple( $keys, $group = '', $force = false ) { + global $wp_object_cache; + + return $wp_object_cache->get_multiple( $keys, $group, $force ); +} + /** * Increment numeric cache item's value * diff --git a/wp-includes/class-wp-object-cache.php b/wp-includes/class-wp-object-cache.php index 428e453631..7a64cc4784 100644 --- a/wp-includes/class-wp-object-cache.php +++ b/wp-includes/class-wp-object-cache.php @@ -302,6 +302,27 @@ class WP_Object_Cache { return false; } + /** + * Retrieves multiple values from the cache. + * + * @since 5.5.0 + * + * @param array $keys Array of keys to fetch. + * @param bool $force Optional. Unused. Whether to force a refetch rather than relying on the local + * cache. Default false. + * + * @return array Array of values organized into groups. + */ + public function get_multiple( $keys, $group = 'default', $force = false ) { + $values = array(); + + foreach ( $keys as $key ) { + $values[ $key ] = $this->get( $key, $group, $force ); + } + + return $values; + } + /** * Increments numeric cache item's value. * diff --git a/wp-includes/load.php b/wp-includes/load.php index 2ce982af2c..9023932e94 100644 --- a/wp-includes/load.php +++ b/wp-includes/load.php @@ -628,6 +628,8 @@ function wp_start_object_cache() { require_once ABSPATH . WPINC . '/cache.php'; } + require_once( ABSPATH . WPINC . '/cache-compat.php' ); + /* * If cache supports reset, reset instead of init if already * initialized. Reset signals to the cache that global IDs diff --git a/wp-includes/version.php b/wp-includes/version.php index ba52abdd13..76a4c0063b 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.5-alpha-47937'; +$wp_version = '5.5-alpha-47938'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.