8000 Merge pull request #62 from clue-labs/iterable · reactphp/cache@d4869f4 · GitHub
[go: up one dir, main page]

10000 Skip to content

Commit d4869f4

Browse files
authored
Merge pull request #62 from clue-labs/iterable
Support `iterable` type for all methods working with multiple keys
2 parents b8795e3 + 07bf96b commit d4869f4

File tree

4 files changed

+69
-19
lines changed

4 files changed

+69
-19
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ provide guarantees whether or not the item has been removed from cache.
133133

134134
#### getMultiple()
135135

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

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

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

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

157158
#### setMultiple()
158159

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

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

179180
#### deleteMultiple()
180181

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

184185
This method will resolve with `true` on success or `false` when an error

src/ArrayCache.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function delete(string $key): PromiseInterface
123123
return resolve(true);
124124
}
125125

126-
public function getMultiple(array $keys, $default = null): PromiseInterface
126+
public function getMultiple(iterable $keys, $default = null): PromiseInterface
127127
{
128128
$values = [];
129129

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

138-
public function setMultiple(array $values, ?float $ttl = null): PromiseInterface
138+
public function setMultiple(iterable $values, ?float $ttl = null): PromiseInterface
139139
{
140140
foreach ($values as $key => $value) {
141141
$this->set($key, $value, $ttl);
@@ -144,7 +144,7 @@ public function setMultiple(array $values, ?float $ttl = null): PromiseInterface
144144
return resolve(true);
145145
}
146146

147-
public function deleteMultiple(array $keys): PromiseInterface
147+
public function deleteMultiple(iterable $keys): PromiseInterface
148148
{
149149
foreach ($keys as $key) {
150150
unset($this->data[$key], $this->expires[$key]);

src/CacheInterface.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ public function delete(string $key): PromiseInterface;
106106
* considered a cache miss.
107107
*
108108
* ```php
109-
* $cache->getMultiple(['name', 'age'])->then(function (array $values): void {
110-
* $name = $values['name'] ?? 'User';
111-
* $age = $values['age'] ?? 'n/a';
109+
* $cache->getMultiple(['name', 'age'])->then(function (iterable $values): void {
110+
* $array = is_array($values) ? $values : iterator_to_array($values);
111+
* $name = $array['name'] ?? 'User';
112+
* $age = $array['age'] ?? 'n/a';
112113
*
113114
* echo $name . ' is ' . $age . PHP_EOL;
114115
* });
@@ -118,11 +119,11 @@ public function delete(string $key): PromiseInterface;
118119
* prints some example output. You can use any of the composition provided
119120
* by [promises](https://github.com/reactphp/promise).
120121
*
121-
* @param string[] $keys A list of keys that can obtained in a single operation.
122+
* @param iterable<string> $keys A list of keys that can obtained in a single operation.
122123
* @param mixed $default Default value to return for keys that do not exist.
123-
* @return PromiseInterface<array<string,mixed>> Returns a promise which resolves to an `array` of cached values
124+
* @return PromiseInterface<iterable<string,mixed>> Returns a promise which resolves to an `array` of cached values
124125
*/
125-
public function getMultiple(array $keys, $default = null): PromiseInterface;
126+
public function getMultiple(iterable $keys, $default = null): PromiseInterface;
126127

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

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

161162
/**
162163
* Wipes clean the entire cache.

tests/ArrayCacheTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ public function testGetMultiple(): void
164164
->then($this->expectCallableOnceWith(['foo' => '1', 'bar' => 'baz']));
165165
}
166166

167+
public function testGetMultipleWithIterableKeysFromGenerator(): void
168+
{
169+
$this->cache = new ArrayCache();
170+
$this->cache->set('foo', '1');
171+
172+
$keys = (function (): \Generator { yield from ['foo', 'bar']; })();
173+
174+
$this->cache
175+
->getMultiple($keys, 'baz')
176+
->then($this->expectCallableOnceWith(['foo' => '1', 'bar' => 'baz']));
177+
}
178+
167179
public function testSetMultiple(): void
168180
{
169181
$this->cache = new ArrayCache();
@@ -174,6 +186,18 @@ public function testSetMultiple(): void
174186
->then($this->expectCallableOnceWith(['foo' => '1', 'bar' => '2']));
175187
}
176188

189+
public function testSetMultipleWithIterableValuesFromGenerator(): void
190+
{
191+
$values = (function(): \Generator { yield from ['foo' => '1', 'bar' => '2']; })();
192+
193+
$this->cache = new ArrayCache();
194+
$this->cache->setMultiple($values, 10);
195+
196+
$this->cache
197+
->getMultiple(['foo', 'bar'])
198+
->then($this->expectCallableOnceWith(['foo' => '1', 'bar' => '2']));
199+
}
200+
177201
public function testDeleteMultiple(): void
178202
{
179203
$this->cache = new ArrayCache();
@@ -196,6 +220,30 @@ public function testDeleteMultiple(): void
196220
->then($this->expectCallableOnceWith(false));
197221
}
198222

223+
public function testDeleteMultipleWithIterableKeysFromGenerator(): void
224+
{
225+
$this->cache = new ArrayCache();
226+
$this->cache->setMultiple(['foo' => 1, 'bar' => 2, 'baz' => 3]);
227+
228+
$keys = (function (): \Generator { yield from ['foo', 'baz']; })();
229+
230+
$this->cache
231+
->deleteMultiple($keys)
232+
->then($this->expectCallableOnceWith(true));
233+
234+
$this->cache
235+
->has('foo')
236+
->then($this->expectCallableOnceWith(false));
237+
238+
$this->cache
239+
->has('bar')
240+
->then($this->expectCallableOnceWith(true));
241+
242+
$this->cache
243+
->has('baz')
244+
->then($this->expectCallableOnceWith(false));
245+
}
246+
199247
public function testClearShouldClearCache(): void
200248
{
201249
$this->cache = new ArrayCache();

0 commit comments

Comments
 (0)
0