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. git-svn-id: https://develop.svn.wordpress.org/trunk@47938 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
78d1ab2ed4
commit
d4d78e4ae7
@ -126,6 +126,24 @@ function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
|
|||||||
return $wp_object_cache->get( $key, $group, $force, $found );
|
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
|
* Increment numeric cache item's value
|
||||||
*
|
*
|
||||||
|
@ -302,6 +302,27 @@ class WP_Object_Cache {
|
|||||||
return false;
|
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.
|
* Increments numeric cache item's value.
|
||||||
*
|
*
|
||||||
|
@ -628,6 +628,8 @@ function wp_start_object_cache() {
|
|||||||
require_once ABSPATH . WPINC . '/cache.php';
|
require_once ABSPATH . WPINC . '/cache.php';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
require_once( ABSPATH . WPINC . '/cache-compat.php' );
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If cache supports reset, reset instead of init if already
|
* If cache supports reset, reset instead of init if already
|
||||||
* initialized. Reset signals to the cache that global IDs
|
* initialized. Reset signals to the cache that global IDs
|
||||||
|
@ -315,4 +315,23 @@ class Tests_Cache extends WP_UnitTestCase {
|
|||||||
// Make sure $fake_key is not stored.
|
// Make sure $fake_key is not stored.
|
||||||
$this->assertFalse( wp_cache_get( $fake_key ) );
|
$this->assertFalse( wp_cache_get( $fake_key ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 20875
|
||||||
|
*/
|
||||||
|
public function test_get_multiple() {
|
||||||
|
wp_cache_set( 'foo1', 'bar', 'group1' );
|
||||||
|
wp_cache_set( 'foo2', 'bar', 'group1' );
|
||||||
|
wp_cache_set( 'foo1', 'bar', 'group2' );
|
||||||
|
|
||||||
|
$found = wp_cache_get_multiple( array( 'foo1', 'foo2', 'foo3', ), 'group1' );
|
||||||
|
|
||||||
|
$expected = array(
|
||||||
|
'foo1' => 'bar',
|
||||||
|
'foo2' => 'bar',
|
||||||
|
'foo3' => false,
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertSame( $expected, $found );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -290,6 +290,11 @@ class Tests_Pluggable extends WP_UnitTestCase {
|
|||||||
'force' => false,
|
'force' => false,
|
||||||
'found' => null,
|
'found' => null,
|
||||||
),
|
),
|
||||||
|
'wp_cache_get_multiple' => array(
|
||||||
|
'keys',
|
||||||
|
'group' => '',
|
||||||
|
'force' => false,
|
||||||
|
),
|
||||||
'wp_cache_incr' => array(
|
'wp_cache_incr' => array(
|
||||||
'key',
|
'key',
|
||||||
'offset' => 1,
|
'offset' => 1,
|
||||||
|
Loading…
Reference in New Issue
Block a user