bug #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 | #27734 #27731
| License       | MIT

Cf. issues.

Commits
-------

3178aed84e [Cache] ArrayAdapter and NullAdapter don't need stampede protection
This commit is contained in:
Nicolas Grekas 2018-06-28 08:42:31 +02:00
commit 17977c8d0c
2 changed files with 23 additions and 5 deletions

View File

@ -17,7 +17,6 @@ use Symfony\Component\Cache\CacheInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\ArrayTrait;
use Symfony\Component\Cache\Traits\GetTrait;
/**
* @author Nicolas Grekas <p@tchwork.com>
@ -25,7 +24,6 @@ use Symfony\Component\Cache\Traits\GetTrait;
class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface
{
use ArrayTrait;
use GetTrait;
private $createCacheItem;
@ -51,6 +49,21 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInter
);
}
/**
* {@inheritdoc}
*/
public function get(string $key, callable $callback, float $beta = null)
{
$item = $this->getItem($key);
// ArrayAdapter works in memory, we don't care about stampede protection
if (INF === $beta || !$item->isHit()) {
$this->save($item->set($callback($item)));
}
return $item->get();
}
/**
* {@inheritdoc}
*/

View File

@ -14,15 +14,12 @@ namespace Symfony\Component\Cache\Adapter;
use Psr\Cache\CacheItemInterface;
use Symfony\Component\Cache\CacheInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Traits\GetTrait;
/**
* @author Titouan Galopin <galopintitouan@gmail.com>
*/
class NullAdapter implements AdapterInterface, CacheInterface
{
use GetTrait;
private $createCacheItem;
public function __construct()
@ -40,6 +37,14 @@ class NullAdapter implements AdapterInterface, CacheInterface
);
}
/**
* {@inheritdoc}
*/
public function get(string $key, callable $callback, float $beta = null)
{
return $callback(($this->createCacheItem)());
}
/**
* {@inheritdoc}
*/