8000 Support `iterable` type for all methods working with multiple keys by clue · Pull Request #62 · reactphp/cache · GitHub
[go: up one dir, main page]

Skip to content
8000

Support iterable type for all methods working with multiple keys #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ provide guarantees whether or not the item has been removed from cache.

#### getMultiple()

The `getMultiple(string[] $keys, mixed $default = null): PromiseInterface<array>` method can be used to
The `getMultiple(iterable<string> $keys, mixed $default = null): PromiseInterface<iterable<string,mixed>>` method can be used to
retrieve multiple cache items by their unique keys.

This method will resolve with an array of cached values on success or with the
Expand All @@ -142,9 +142,10 @@ Similarly, an expired cache item (once the time-to-live is expired) is
considered a cache miss.

```php
$cache->getMultiple(['name', 'age'])->then(function (array $values): void {
$name = $values['name'] ?? 'User';
$age = $values['age'] ?? 'n/a';
$cache->getMultiple(['name', 'age'])->then(function (iterable $values): void {
$array = is_array($values) ? $values : iterator_to_array($values);
$name = $array['name'] ?? 'User';
$age = $array['age'] ?? 'n/a';

echo $name . ' is ' . $age . PHP_EOL;
});
Expand All @@ -156,7 +157,7 @@ by [promises](https://github.com/reactphp/promise).

#### setMultiple()

The `setMultiple(array $values, ?float $ttl = null): PromiseInterface<bool>` method can be used to
The `setMultiple(iterable<string,mixed> $values, ?float $ttl = null): PromiseInterface<bool>` method can be used to
persist a set of key => value pairs in the cache, with an optional TTL.

This method will resolve with `true` on success or `false` when an error
Expand All @@ -178,7 +179,7 @@ and the key `bar` to `2`. If some of the keys already exist, they are overridden

#### deleteMultiple()

The `setMultiple(string[] $keys): PromiseInterface<bool>` method can be used to
The `setMultiple(iterable<string> $keys): PromiseInterface<bool>` method can be used to
delete multiple cache items in a single operation.

This method will resolve with `true` on success or `false` when an error
Expand Down
6 changes: 3 additions & 3 deletions src/ArrayCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function delete(string $key): PromiseInterface
return resolve(true);
}

public function getMultiple(array $keys, $default = null): PromiseInterface
public function getMultiple(iterable $keys, $default = null): PromiseInterface
{
$values = [];

Expand All @@ -135,7 +135,7 @@ public function getMultiple(array $keys, $default = null): PromiseInterface
return all($values);
}

public function setMultiple(array $values, ?float $ttl = null): PromiseInterface
public function setMultiple(iterable $values, ?float $ttl = null): PromiseInterface
{
foreach ($values as $key => $value) {
$this->set($key, $value, $ttl);
Expand All @@ -144,7 +144,7 @@ public function setMultiple(array $values, ?float $ttl = null): PromiseInterface
return resolve(true);
}

public function deleteMultiple(array $keys): PromiseInterface
public function deleteMultiple(iterable $keys): PromiseInterface
{
foreach ($keys as $key) {
unset($this->data[$key], $this->expires[$key]);
Expand Down
21 changes: 11 additions & 10 deletions src/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ public function delete(string $key): PromiseInterface;
* considered a cache miss.
*
* ```php
* $cache->getMultiple(['name', 'age'])->then(function (array $values): void {
* $name = $values['name'] ?? 'User';
* $age = $values['age'] ?? 'n/a';
* $cache->getMultiple(['name', 'age'])->then(function (iterable $values): void {
* $array = is_array($values) ? $values : iterator_to_array($values);
* $name = $array['name'] ?? 'User';
* $age = $array['age'] ?? 'n/a';
*
* echo $name . ' is ' . $age . PHP_EOL;
* });
Expand All @@ -118,11 +119,11 @@ public function delete(string $key): PromiseInterface;
* prints some example output. You can use any of the composition provided
* by [promises](https://github.com/reactphp/promise).
*
* @param string[] $keys A list of keys that can obtained in a single operation.
* @param iterable<string> $keys A list of keys that can obtained in a single operation.
* @param mixed $default Default value to return for keys that do not exist.
* @return PromiseInterface<array<string,mixed>> Returns a promise which resolves to an `array` of cached values
* @return PromiseInterface<iterable<string,mixed>> Returns a promise which resolves to an `array` of cached values
*/
public function getMultiple(array $keys, $default = null): PromiseInterface;
public function getMultiple(iterable $keys, $default = null): PromiseInterface;

/**
* Persists a set of key => value pairs in the cache, with an optional TTL.
Expand All @@ -144,19 +145,19 @@ public function getMultiple(array $keys, $default = null): PromiseInterface;
* This example eventually sets the list of values - the key `foo` to 1 value
* and the key `bar` to 2. If some of the keys already exist, they are overridden.
*
* @param array<string,mixed> $values A list of key => value pairs for a multiple-set operation.
* @param iterable<string,mixed> $values A list of key => value pairs for a multiple-set operation.
* @param ?float $ttl Optional. The TTL value of this item.
* @return PromiseInterface<bool> Returns a promise which resolves to `true` on success or `false` on error
*/
public function setMultiple(array $values, ?float $ttl = null): PromiseInterface;
public function setMultiple(iterable $values, ?float $ttl = null): PromiseInterface;

/**
* Deletes multiple cache items in a single operation.
*
* @param string[] $keys A list of string-based keys to be deleted.
* @param iterable<string> $keys A list of string-based keys to be deleted.
* @return PromiseInterface<bool> Returns a promise which resolves to `true` on success or `false` on error
*/
public function deleteMultiple(array $keys): PromiseInterface;
public function deleteMultiple(iterable $keys): PromiseInterface;

/**
* Wipes clean the entire cache.
Expand Down
48 changes: 48 additions & 0 deletions tests/ArrayCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ public function testGetMultiple(): void
->then($this->expectCallableOnceWith(['foo' => '1', 'bar' => 'baz']));
}

public function testGetMultipleWithIterableKeysFromGenerator(): void
{
$this->cache = new ArrayCache();
$this->cache->set('foo', '1');

$keys = (function (): \Generator { yield from ['foo', 'bar']; })();

$this->cache
->getMultiple($keys, 'baz')
->then($this->expectCallableOnceWith(['foo' => '1', 'bar' => 'baz']));
}

public function testSetMultiple(): void
{
$this->cache = new ArrayCache();
Expand All @@ -174,6 +186,18 @@ public function testSetMultiple(): void
->then($this->expectCallableOnceWith(['foo' => '1', 'bar' => '2']));
}

public function testSetMultipleWithIterableValuesFromGenerator(): void
{
$values = (function(): \Generator { yield from ['foo' => '1', 'bar' => '2']; })();

$this->cache = new ArrayCache();
$this->cache->setMultiple($values, 10);

$this->cache
->getMultiple(['foo', 'bar'])
->then($this->expectCallableOnceWith(['foo' => '1', 'bar' => '2']));
}

public function testDeleteMultiple(): void
{
$this->cache = new ArrayCache();
Expand All @@ -196,6 +220,30 @@ public function testDeleteMultiple(): void
->then($this->expectCallableOnceWith(false));
}

public function testDeleteMultipleWithIterableKeysFromGenerator(): void
{
$this->cache = new ArrayCache();
$this->cache->setMultiple(['foo' => 1, 'bar' => 2, 'baz' => 3]);

$keys = (function (): \Generator { yield from ['foo', 'baz']; })();

$this->cache
->deleteMultiple($keys)
->then($this->expectCallableOnceWith(true));

$this->cache
->has('foo')
->then($this->expectCallableOnceWith(false));

$this->cache
->has('bar')
->then($this->expectCallableOnceWith(true));

$this->cache
->has('baz')
->then($this->expectCallableOnceWith(false));
}

public func 469D tion testClearShouldClearCache(): void
{
$this->cache = new ArrayCache();
Expand Down
0