Skip to content

Commit 855b47f

Browse files
feature #41290 [Cache] Implement psr/cache 3 (derrabus)
This PR was merged into the 6.0 branch. Discussion ---------- [Cache] Implement psr/cache 3 | Q | A | ------------- | --- | Branch? | 6.0 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | N/A This PR adds the necessary return types to support version 3 of the `psr/cache` interfaces. I did not add parameter types in order to maintain v1 compatibility, but we could as well decide to drop v1 if we want. Commits ------- 71d76c4 [Cache] Implement psr/cache 3
2 parents ed78403 + 71d76c4 commit 855b47f

15 files changed

+96
-218
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"provide": {
1919
"php-http/async-client-implementation": "*",
2020
"php-http/client-implementation": "*",
21-
"psr/cache-implementation": "1.0|2.0",
21+
"psr/cache-implementation": "1.0|2.0|3.0",
2222
"psr/container-implementation": "1.0",
2323
"psr/event-dispatcher-implementation": "1.0",
2424
"psr/http-client-implementation": "1.0",
@@ -38,7 +38,7 @@
3838
"doctrine/event-manager": "~1.0",
3939
"doctrine/persistence": "^2",
4040
"twig/twig": "^2.13|^3.0.4",
41-
"psr/cache": "^1.0|^2.0",
41+
"psr/cache": "^1.0|^2.0|^3.0",
4242
"psr/container": "^1.0",
4343
"psr/event-dispatcher": "^1.0",
4444
"psr/link": "^1.1",

src/Symfony/Component/Cache/Adapter/AbstractAdapter.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,8 @@ public static function createConnection(string $dsn, array $options = [])
141141

142142
/**
143143
* {@inheritdoc}
144-
*
145-
* @return bool
146144
*/
147-
public function commit()
145+
public function commit(): bool
148146
{
149147
$ok = true;
150148
$byLifetime = (self::$mergeByLifetime)($this->deferred, $this->namespace, $expiredIds, \Closure::fromCallable([$this, 'getId']), $this->defaultLifetime);

src/Symfony/Component/Cache/Adapter/AdapterInterface.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,18 @@ interface AdapterInterface extends CacheItemPoolInterface
2626
{
2727
/**
2828
* {@inheritdoc}
29-
*
30-
* @return CacheItem
3129
*/
32-
public function getItem($key);
30+
public function getItem($key): CacheItem;
3331

3432
/**
3533
* {@inheritdoc}
3634
*
37-
* @return \Traversable|CacheItem[]
35+
* @return iterable<CacheItem>
3836
*/
39-
public function getItems(array $keys = []);
37+
public function getItems(array $keys = []): iterable;
4038

4139
/**
4240
* {@inheritdoc}
43-
*
44-
* @return bool
4541
*/
46-
public function clear(string $prefix = '');
42+
public function clear(string $prefix = ''): bool;
4743
}

src/Symfony/Component/Cache/Adapter/ArrayAdapter.php

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,8 @@ public function delete(string $key): bool
9797

9898
/**
9999
* {@inheritdoc}
100-
*
101-
* @return bool
102100
*/
103-
public function hasItem($key)
101+
public function hasItem($key): bool
104102
{
105103
if (\is_string($key) && isset($this->expiries[$key]) && $this->expiries[$key] > microtime(true)) {
106104
if ($this->maxItems) {
@@ -120,7 +118,7 @@ public function hasItem($key)
120118
/**
121119
* {@inheritdoc}
122120
*/
123-
public function getItem($key)
121+
public function getItem($key): CacheItem
124122
{
125123
if (!$isHit = $this->hasItem($key)) {
126124
$value = null;
@@ -139,7 +137,7 @@ public function getItem($key)
139137
/**
140138
* {@inheritdoc}
141139
*/
142-
public function getItems(array $keys = [])
140+
public function getItems(array $keys = []): iterable
143141
{
144142
\assert(self::validateKeys($keys));
145143

@@ -148,10 +146,8 @@ public function getItems(array $keys = [])
148146

149147
/**
150148
* {@inheritdoc}
151-
*
152-
* @return bool
153149
*/
154-
public function deleteItem($key)
150+
public function deleteItem($key): bool
155151
{
156152
\assert('' !== CacheItem::validateKey($key));
157153
unset($this->values[$key], $this->expiries[$key]);
@@ -161,10 +157,8 @@ public function deleteItem($key)
161157

162158
/**
163159
* {@inheritdoc}
164-
*
165-
* @return bool
166160
*/
167-
public function deleteItems(array $keys)
161+
public function deleteItems(array $keys): bool
168162
{
169163
foreach ($keys as $key) {
170164
$this->deleteItem($key);
@@ -175,10 +169,8 @@ public function deleteItems(array $keys)
175169

176170
/**
177171
* {@inheritdoc}
178-
*
179-
* @return bool
180172
*/
181-
public function save(CacheItemInterface $item)
173+
public function save(CacheItemInterface $item): bool
182174
{
183175
if (!$item instanceof CacheItem) {
184176
return false;
@@ -230,30 +222,24 @@ public function save(CacheItemInterface $item)
230222

231223
/**
232224
* {@inheritdoc}
233-
*
234-
* @return bool
235225
*/
236-
public function saveDeferred(CacheItemInterface $item)
226+
public function saveDeferred(CacheItemInterface $item): bool
237227
{
238228
return $this->save($item);
239229
}
240230

241231
/**
242232
* {@inheritdoc}
243-
*
244-
* @return bool
245233
*/
246-
public function commit()
234+
public function commit(): bool
247235
{
248236
return true;
249237
}
250238

251239
/**
252240
* {@inheritdoc}
253-
*
254-
* @return bool
255241
*/
256-
public function clear(string $prefix = '')
242+
public function clear(string $prefix = ''): bool
257243
{
258244
if ('' !== $prefix) {
259245
$now = microtime(true);
@@ -276,10 +262,8 @@ public function clear(string $prefix = '')
276262

277263
/**
278264
* Returns all cached values, with cache miss as null.
279-
*
280-
* @return array
281265
*/
282-
public function getValues()
266+
public function getValues(): array
283267
{
284268
if (!$this->storeSerialized) {
285269
return $this->values;
@@ -306,7 +290,7 @@ public function reset()
306290
$this->clear();
307291
}
308292

309-
private function generateItems(array $keys, $now, $f)
293+
private function generateItems(array $keys, $now, $f): \Generator
310294
{
311295
foreach ($keys as $i => $key) {
312296
if (!$isHit = isset($this->expiries[$key]) && ($this->expiries[$key] > $now || !$this->deleteItem($key))) {

src/Symfony/Component/Cache/Adapter/ChainAdapter.php

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function get(string $key, callable $callback, float $beta = null, array &
120120
/**
121121
* {@inheritdoc}
122122
*/
123-
public function getItem($key)
123+
public function getItem($key): CacheItem
124124
{
125125
$syncItem = self::$syncItem;
126126
$misses = [];
@@ -145,12 +145,12 @@ public function getItem($key)
145145
/**
146146
* {@inheritdoc}
147147
*/
148-
public function getItems(array $keys = [])
148+
public function getItems(array $keys = []): iterable
149149
{
150150
return $this->generateItems($this->adapters[0]->getItems($keys), 0);
151151
}
152152

153-
private function generateItems(iterable $items, int $adapterIndex)
153+
private function generateItems(iterable $items, int $adapterIndex): \Generator
154154
{
155155
$missing = [];
156156
$misses = [];
@@ -183,10 +183,8 @@ private function generateItems(iterable $items, int $adapterIndex)
183183

184184
/**
185185
* {@inheritdoc}
186-
*
187-
* @return bool
188186
*/
189-
public function hasItem($key)
187+
public function hasItem($key): bool
190188
{
191189
foreach ($this->adapters as $adapter) {
192190
if ($adapter->hasItem($key)) {
@@ -199,10 +197,8 @@ public function hasItem($key)
199197

200198
/**
201199
* {@inheritdoc}
202-
*
203-
* @return bool
204200
*/
205-
public function clear(string $prefix = '')
201+
public function clear(string $prefix = ''): bool
206202
{
207203
$cleared = true;
208204
$i = $this->adapterCount;
@@ -220,10 +216,8 @@ public function clear(string $prefix = '')
220216

221217
/**
222218
* {@inheritdoc}
223-
*
224-
* @return bool
225219
*/
226-
public function deleteItem($key)
220+
public function deleteItem($key): bool
227221
{
228222
$deleted = true;
229223
$i = $this->adapterCount;
@@ -237,10 +231,8 @@ public function deleteItem($key)
237231

238232
/**
239233
* {@inheritdoc}
240-
*
241-
* @return bool
242234
*/
243-
public function deleteItems(array $keys)
235+
public function deleteItems(array $keys): bool
244236
{
245237
$deleted = true;
246238
$i = $this->adapterCount;
@@ -254,10 +246,8 @@ public function deleteItems(array $keys)
254246

255247
/**
256248
* {@inheritdoc}
257-
*
258-
* @return bool
259249
*/
260-
public function save(CacheItemInterface $item)
250+
public function save(CacheItemInterface $item): bool
261251
{
262252
$saved = true;
263253
$i = $this->adapterCount;
@@ -271,10 +261,8 @@ public function save(CacheItemInterface $item)
271261

272262
/**
273263
* {@inheritdoc}
274-
*
275-
* @return bool
276264
*/
277-
public function saveDeferred(CacheItemInterface $item)
265+
public function saveDeferred(CacheItemInterface $item): bool
278266
{
279267
$saved = true;
280268
$i = $this->adapterCount;
@@ -288,10 +276,8 @@ public function saveDeferred(CacheItemInterface $item)
288276

289277
/**
290278
* {@inheritdoc}
291-
*
292-
* @return bool
293279
*/
294-
public function commit()
280+
public function commit(): bool
295281
{
296282
$committed = true;
297283
$i = $this->adapterCount;

src/Symfony/Component/Cache/Adapter/NullAdapter.php

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,85 +50,71 @@ public function get(string $key, callable $callback, float $beta = null, array &
5050
/**
5151
* {@inheritdoc}
5252
*/
53-
public function getItem($key)
53+
public function getItem($key): CacheItem
5454
{
5555
return (self::$createCacheItem)($key);
5656
}
5757

5858
/**
5959
* {@inheritdoc}
6060
*/
61-
public function getItems(array $keys = [])
61+
public function getItems(array $keys = []): iterable
6262
{
6363
return $this->generateItems($keys);
6464
}
6565

6666
/**
6767
* {@inheritdoc}
68-
*
69-
* @return bool
7068
*/
71-
public function hasItem($key)
69+
public function hasItem($key): bool
7270
{
7371
return false;
7472
}
7573

7674
/**
7775
* {@inheritdoc}
78-
*
79-
* @return bool
8076
*/
81-
public function clear(string $prefix = '')
77+
public function clear(string $prefix = ''): bool
8278
{
8379
return true;
8480
}
8581

8682
/**
8783
* {@inheritdoc}
88-
*
89-
* @return bool
9084
*/
91-
public function deleteItem($key)
85+
public function deleteItem($key): bool
9286
{
9387
return true;
9488
}
9589

9690
/**
9791
* {@inheritdoc}
98-
*
99-
* @return bool
10092
*/
101-
public function deleteItems(array $keys)
93+
public function deleteItems(array $keys): bool
10294
{
10395
return true;
10496
}
10597

10698
/**
10799
* {@inheritdoc}
108-
*
109-
* @return bool
110100
*/
111-
public function save(CacheItemInterface $item)
101+
public function save(CacheItemInterface $item): bool
112102
{
113103
return true;
114104
}
115105

116106
/**
117107
* {@inheritdoc}
118-
*
119-
* @return bool
120108
*/
121-
public function saveDeferred(CacheItemInterface $item)
109+
public function saveDeferred(CacheItemInterface $item): bool
122110
{
123111
return true;
124112
}
125113

126114
/**
127115
* {@inheritdoc}
128-
*
129-
* @return bool
130116
*/
131-
public function commit()
117+
public function commit(): bool
132118
{
133119
return true;
134120
}
@@ -141,7 +127,7 @@ public function delete(string $key): bool
141127
return $this->deleteItem($key);
142128
}
143129

144-
private function generateItems(array $keys)
130+
private function generateItems(array $keys): \Generator
145131
{
146132
$f = self::$createCacheItem;
147133

0 commit comments

Comments
 (0)