8000 [Cache] Pass arg to get callback everywhere · symfony/symfony@d03eb03 · GitHub
[go: up one dir, main page]

Skip to content

Commit d03eb03

Browse files
committed
[Cache] Pass arg to get callback everywhere
1 parent a80483c commit d03eb03

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public function get(string $key, callable $callback, float $beta = null, array &
5959

6060
// ArrayAdapter works in memory, we don't care about stampede protection
6161
if (INF === $beta || !$item->isHit()) {
62-
$this->save($item->set($callback($item)));
62+
$save = true;
63+
$this->save($item->set($callback($item, $save)));
6364
}
6465

6566
return $item->get();

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ function ($key) {
4242
*/
4343
public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
4444
{
45-
return $callback(($this->createCacheItem)($key));
45+
$save = true;
46+
47+
return $callback(($this->createCacheItem)($key), $save);
4648
}
4749

4850
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ public function get(string $key, callable $callback, float $beta = null, array &
103103
return $this->doGet($this, $key, $callback, $beta, $metadata);
104104
}
105105

106-
return $this->pool->get($this->getId($key), function ($innerItem) use ($key, $callback) {
106+
return $this->pool->get($this->getId($key), function ($innerItem, bool &$save) use ($key, $callback) {
107107
$item = ($this->createCacheItem)($key, $innerItem);
108-
$item->set($value = $callback($item));
108+
$item->set($value = $callback($item, $save));
109109
($this->setInnerItem)($innerItem, (array) $item);
110110

111111
return $value;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ public function get(string $key, callable $callback, float $beta = null, array &
4545
}
4646

4747
$isHit = true;
48-
$callback = function (CacheItem $item) use ($callback, &$isHit) {
48+
$callback = function (CacheItem $item, bool &$save) use ($callback, &$isHit) {
4949
$isHit = $item->isHit();
5050

51-
return $callback($item);
51+
return $callback($item, $save);
5252
};
5353

5454
$event = $this->start(__FUNCTION__);

src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
namespace Symfony\Component\Cache\Tests\Adapter;
1313

1414
use Cache\IntegrationTests\CachePoolTest;
15+
use PHPUnit\Framework\Assert;
16+
use Psr\Cache\CacheItemInterface;
1517
use Psr\Cache\CacheItemPoolInterface;
1618
use Symfony\Component\Cache\CacheItem;
1719
use Symfony\Component\Cache\PruneableInterface;
20+
use Symfony\Contracts\Cache\CallbackInterface;
1821

1922
abstract class AdapterTestCase extends CachePoolTest
2023
{
@@ -57,6 +60,22 @@ public function testGet()
5760
$this->assertSame($value, $item->get());
5861
}, INF));
5962
$this->assertFalse($isHit);
63+
64+
$this->assertSame($value, $cache->get('bar', new class($value) implements CallbackInterface {
65+
private $value;
66+
67+
public function __construct(int $value)
68+
{
69+
$this->value = $value;
70+
}
71+
72+
public function __invoke(CacheItemInterface $item, bool &$save)
73+
{
74+
Assert::assertSame('bar', $item->getKey());
75+
76+
return $this->value;
77+
}
78+
}));
6079
}
6180

6281
public function testRecursiveGet()

0 commit comments

Comments
 (0)
0