Skip to content

Commit

Permalink
Merge pull request #27 from inpsyde/fix/get-multiple
Browse files Browse the repository at this point in the history
Fix get multiple method
  • Loading branch information
Biont committed Dec 12, 2022
2 parents 17d18b3 + 67c3a91 commit 219e1ae
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
3 changes: 2 additions & 1 deletion dropin/object-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ function wp_cache_reset()
* @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 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.
*/
function wp_cache_get_multiple($keys, $group = '', $force = false)
{
Expand Down
10 changes: 9 additions & 1 deletion src/ObjectCacheProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,14 @@ public function switch_to_blog($blog_id)
*/
public function get_multiple($keys, $group = '', $force = false)
{
return $this->choose_pool($group)->getMultiple($keys);
$keys = array_unique($keys);
$cache_keys = [];
foreach ($keys as $key) {
$cache_keys[] = $this->key_gen->create((string) $key, (string) $group);
}

$items = $this->choose_pool($group)->getMultiple($cache_keys);

return array_combine($keys, $items);
}
}
37 changes: 37 additions & 0 deletions tests/PHPUnit/Unit/ObjectCacheProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

use Brain\Monkey\Functions;
use Inpsyde\WpStash\Generator\KeyGen;
use Inpsyde\WpStash\Generator\MultisiteCacheKeyGenerator;
use Inpsyde\WpStash\Generator\MultisiteKeyGen;
use Inpsyde\WpStash\ObjectCacheProxy;
use Inpsyde\WpStash\StashAdapter;
use Mockery\MockInterface;
use Stash\Driver\Ephemeral;
use Stash\Pool;

class ObjectCacheProxyTest extends AbstractUnitTestcase
{
Expand Down Expand Up @@ -143,6 +146,40 @@ public function test_add_non_persistent_groups(
$this->assertSame(array_fill_keys($nonPersistentGroups, true), $result);
}

public function test_get_multiple(): void
{
Functions\expect('wp_suspend_cache_addition')
->twice()
->andReturn(false);

$testee = new ObjectCacheProxy(
new StashAdapter(
new Pool(
new Ephemeral()
)
),
new StashAdapter(
new Pool(
new Ephemeral()
)
),
new MultisiteCacheKeyGenerator(1)
);

$testee->add('key_1', 'data_key_1', 'my_group');
$testee->add('key_2', 'data_key_2', 'my_group');
$this->assertSame(
[
'key_1' => 'data_key_1',
'key_2' => 'data_key_2',
],
$testee->get_multiple(
['key_1', 'key_2', 'key_2'],
'my_group'
)
);
}

public function default_test_data()
{
$args = [
Expand Down

0 comments on commit 219e1ae

Please sign in to comment.