Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Cache] Pass arg to get callback everywhere #31879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Cache] Pass arg to get callback everywhere
  • Loading branch information
fancyweb committed Jun 6, 2019
commit d03eb033bb413f310cdbe89005ba1b4ee7a88943
3 changes: 2 additions & 1 deletion 3 src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public function get(string $key, callable $callback, float $beta = null, array &

// 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();
Expand Down
4 changes: 3 additions & 1 deletion 4 src/Symfony/Component/Cache/Adapter/NullAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ function ($key) {
*/
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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions 4 src/Symfony/Component/Cache/Adapter/ProxyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ public function get(string $key, callable $callback, float $beta = null, array &
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));
fancyweb marked this conversation as resolved.
Show resolved Hide resolved
($this->setInnerItem)($innerItem, (array) $item);

return $value;
Expand Down
4 changes: 2 additions & 2 deletions 4 src/Symfony/Component/Cache/Adapter/TraceableAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public function get(string $key, callable $callback, float $beta = null, array &
}

$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);
fancyweb marked this conversation as resolved.
Show resolved Hide resolved
};

$event = $this->start(__FUNCTION__);
Expand Down
19 changes: 19 additions & 0 deletions 19 src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -57,6 +60,22 @@ public function testGet()
$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()
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.