Skip to content

Commit 219e1ae

Browse files
authored
Merge pull request #27 from inpsyde/fix/get-multiple
Fix get multiple method
2 parents 17d18b3 + 67c3a91 commit 219e1ae

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

dropin/object-cache.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ function wp_cache_reset()
304304
* @param string $group Optional. Where the cache contents are grouped. Default empty.
305305
* @param bool $force Optional. Whether to force an update of the local cache
306306
* from the persistent cache. Default false.
307-
* @return array Array of values organized into groups.
307+
* @return array Array of return values, grouped by key. Each value is either
308+
* the cache contents on success, or false on failure.
308309
*/
309310
function wp_cache_get_multiple($keys, $group = '', $force = false)
310311
{

src/ObjectCacheProxy.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,14 @@ public function switch_to_blog($blog_id)
462462
*/
463463
public function get_multiple($keys, $group = '', $force = false)
464464
{
465-
return $this->choose_pool($group)->getMultiple($keys);
465+
$keys = array_unique($keys);
466+
$cache_keys = [];
467+
foreach ($keys as $key) {
468+
$cache_keys[] = $this->key_gen->create((string) $key, (string) $group);
469+
}
470+
471+
$items = $this->choose_pool($group)->getMultiple($cache_keys);
472+
473+
return array_combine($keys, $items);
466474
}
467475
}

tests/PHPUnit/Unit/ObjectCacheProxyTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
use Brain\Monkey\Functions;
66
use Inpsyde\WpStash\Generator\KeyGen;
7+
use Inpsyde\WpStash\Generator\MultisiteCacheKeyGenerator;
78
use Inpsyde\WpStash\Generator\MultisiteKeyGen;
89
use Inpsyde\WpStash\ObjectCacheProxy;
910
use Inpsyde\WpStash\StashAdapter;
1011
use Mockery\MockInterface;
12+
use Stash\Driver\Ephemeral;
13+
use Stash\Pool;
1114

1215
class ObjectCacheProxyTest extends AbstractUnitTestcase
1316
{
@@ -143,6 +146,40 @@ public function test_add_non_persistent_groups(
143146
$this->assertSame(array_fill_keys($nonPersistentGroups, true), $result);
144147
}
145148

149+
public function test_get_multiple(): void
150+
{
151+
Functions\expect('wp_suspend_cache_addition')
152+
->twice()
153+
->andReturn(false);
154+
155+
$testee = new ObjectCacheProxy(
156+
new StashAdapter(
157+
new Pool(
158+
new Ephemeral()
159+
)
160+
),
161+
new StashAdapter(
162+
new Pool(
163+
new Ephemeral()
164+
)
165+
),
166+
new MultisiteCacheKeyGenerator(1)
167+
);
168+
169+
$testee->add('key_1', 'data_key_1', 'my_group');
170+
$testee->add('key_2', 'data_key_2', 'my_group');
171+
$this->assertSame(
172+
[
173+
'key_1' => 'data_key_1',
174+
'key_2' => 'data_key_2',
175+
],
176+
$testee->get_multiple(
177+
['key_1', 'key_2', 'key_2'],
178+
'my_group'
179+
)
180+
);
181+
}
182+
146183
public function default_test_data()
147184
{
148185
$args = [

0 commit comments

Comments
 (0)