Skip to content

Commit 4a33574

Browse files
[Cache] allow to skip saving the computed value when using CacheInterface::get()
1 parent df30ce9 commit 4a33574

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

Cache/CacheInterface.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ interface CacheInterface
2929
* requested key, that could be used e.g. for expiration control. It could also
3030
* be an ItemInterface instance when its additional features are needed.
3131
*
32-
* @param string $key The key of the item to retrieve from the cache
33-
* @param callable(CacheItemInterface):mixed $callback Should return the computed value for the given key/item
34-
* @param float|null $beta A float that, as it grows, controls the likeliness of triggering
35-
* early expiration. 0 disables it, INF forces immediate expiration.
36-
* The default (or providing null) is implementation dependent but should
37-
* typically be 1.0, which should provide optimal stampede protection.
38-
* See https://en.wikipedia.org/wiki/Cache_stampede#Probabilistic_early_expiration
32+
* @param string $key The key of the item to retrieve from the cache
33+
* @param callable|CallbackInterface $callback Should return the computed value for the given key/item
34+
* @param float|null $beta A float that, as it grows, controls the likeliness of triggering
35+
* early expiration. 0 disables it, INF forces immediate expiration.
36+
* The default (or providing null) is implementation dependent but should
37+
* typically be 1.0, which should provide optimal stampede protection.
38+
* See https://en.wikipedia.org/wiki/Cache_stampede#Probabilistic_early_expiration
3939
*
4040
* @return mixed The value corresponding to the provided key
4141
*

Cache/CacheTrait.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ private function doGet(CacheItemPoolInterface $pool, string $key, callable $call
5959
}
6060

6161
if ($recompute) {
62-
$pool->save($item->set($callback($item)));
62+
$save = true;
63+
$item->set($callback($item, $save));
64+
if ($save) {
65+
$pool->save($item);
66+
}
6367
}
6468

6569
return $item->get();

Cache/CallbackInterface.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Contracts\Cache;
13+
14+
use Psr\Cache\CacheItemInterface;
15+
16+
/**
17+
* Computes and returns the cached value of an item.
18+
*
19+
* @author Nicolas Grekas <[email protected]>
20+
*/
21+
interface CallbackInterface
22+
{
23+
/**
24+
* @param CacheItemInterface|ItemInterface $item The item to compute the value for
25+
* @param bool &$save Should be set to false when the value should not be saved in the pool
26+
*
27+
* @return mixed The computed value for the passed item
28+
*/
29+
public function __invoke(CacheItemInterface $item, bool &$save);
30+
}

Cache/TagAwareCacheInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ interface TagAwareCacheInterface extends CacheInterface
2222
{
2323
/**
2424
* {@inheritdoc}
25-
*
26-
* @param callable(ItemInterface):mixed $callback Should return the computed value for the given key/item
2725
*/
2826
public function get(string $key, callable $callback, float $beta = null);
2927

0 commit comments

Comments
 (0)