[Cache] Pass arg to get callback everywhere

This commit is contained in:
Thomas Calvet 2019-06-05 18:16:43 +02:00
parent a80483cdb7
commit d03eb033bb
5 changed files with 28 additions and 6 deletions

View File

@ -59,7 +59,8 @@ class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInter
// ArrayAdapter works in memory, we don't care about stampede protection
if (INF === $beta || !$item->isHit()) {
$this->save($item->set($callback($item)));
$save = true;
$this->save($item->set($callback($item, $save)));
}
return $item->get();

View File

@ -42,7 +42,9 @@ class NullAdapter implements AdapterInterface, CacheInterface
*/
public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
{
return $callback(($this->createCacheItem)($key));
$save = true;
return $callback(($this->createCacheItem)($key), $save);
}
/**

View File

@ -103,9 +103,9 @@ class ProxyAdapter implements AdapterInterface, CacheInterface, PruneableInterfa
return $this->doGet($this, $key, $callback, $beta, $metadata);
}
return $this->pool->get($this->getId($key), function ($innerItem) use ($key, $callback) {
return $this->pool->get($this->getId($key), function ($innerItem, bool &$save) use ($key, $callback) {
$item = ($this->createCacheItem)($key, $innerItem);
$item->set($value = $callback($item));
$item->set($value = $callback($item, $save));
($this->setInnerItem)($innerItem, (array) $item);
return $value;

View File

@ -45,10 +45,10 @@ class TraceableAdapter implements AdapterInterface, CacheInterface, PruneableInt
}
$isHit = true;
$callback = function (CacheItem $item) use ($callback, &$isHit) {
$callback = function (CacheItem $item, bool &$save) use ($callback, &$isHit) {
$isHit = $item->isHit();
return $callback($item);
return $callback($item, $save);
};
$event = $this->start(__FUNCTION__);

View File

@ -12,9 +12,12 @@
namespace Symfony\Component\Cache\Tests\Adapter;
use Cache\IntegrationTests\CachePoolTest;
use PHPUnit\Framework\Assert;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Contracts\Cache\CallbackInterface;
abstract class AdapterTestCase extends CachePoolTest
{
@ -57,6 +60,22 @@ abstract class AdapterTestCase extends CachePoolTest
$this->assertSame($value, $item->get());
}, INF));
$this->assertFalse($isHit);
$this->assertSame($value, $cache->get('bar', new class($value) implements CallbackInterface {
private $value;
public function __construct(int $value)
{
$this->value = $value;
}
public function __invoke(CacheItemInterface $item, bool &$save)
{
Assert::assertSame('bar', $item->getKey());
return $this->value;
}
}));
}
public function testRecursiveGet()