Skip to content

Commit 17977c8

Browse files
bug symfony#27734 [Cache] ArrayAdapter and NullAdapter don't need stampede protection (Pierre Rineau)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Cache] ArrayAdapter and NullAdapter don't need stampede protection | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | probably | Fixed tickets | symfony#27734 symfony#27731 | License | MIT Cf. issues. Commits ------- 3178aed [Cache] ArrayAdapter and NullAdapter don't need stampede protection
2 parents adb137d + 3178aed commit 17977c8

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717
use Symfony\Component\Cache\CacheItem;
1818
use Symfony\Component\Cache\ResettableInterface;
1919
use Symfony\Component\Cache\Traits\ArrayTrait;
20-
use Symfony\Component\Cache\Traits\GetTrait;
2120

2221
/**
2322
* @author Nicolas Grekas <[email protected]>
2423
*/
2524
class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface
2625
{
2726
use ArrayTrait;
28-
use GetTrait;
2927

3028
private $createCacheItem;
3129

@@ -51,6 +49,21 @@ function ($key, $value, $isHit) use ($defaultLifetime) {
5149
);
5250
}
5351

52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function get(string $key, callable $callback, float $beta = null)
56+
{
57+
$item = $this->getItem($key);
58+
59+
// ArrayAdapter works in memory, we don't care about stampede protection
60+
if (INF === $beta || !$item->isHit()) {
61+
$this->save($item->set($callback($item)));
62+
}
63+
64+
return $item->get();
65+
}
66+
5467
/**
5568
* {@inheritdoc}
5669
*/

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,12 @@
1414
use Psr\Cache\CacheItemInterface;
1515
use Symfony\Component\Cache\CacheInterface;
1616
use Symfony\Component\Cache\CacheItem;
17-
use Symfony\Component\Cache\Traits\GetTrait;
1817

1918
/**
2019
* @author Titouan Galopin <[email protected]>
2120
*/
2221
class NullAdapter implements AdapterInterface, CacheInterface
2322
{
24-
use GetTrait;
25-
2623
private $createCacheItem;
2724

2825
public function __construct()
@@ -40,6 +37,14 @@ function ($key) {
4037
);
4138
}
4239

40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function get(string $key, callable $callback, float $beta = null)
44+
{
45+
return $callback(($this->createCacheItem)());
46+
}
47+
4348
/**
4449
* {@inheritdoc}
4550
*/

0 commit comments

Comments
 (0)