Skip to content

Commit 3edf0ab

Browse files
[Contracts][Cache] allow retrieving metadata of cached items
1 parent 9c85a44 commit 3edf0ab

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

Cache/CacheInterface.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@ 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|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
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
39+
* @param array &$metadata The metadata of the cached item {@see ItemInterface::getMetadata()}
3940
*
4041
* @return mixed The value corresponding to the provided key
4142
*
4243
* @throws InvalidArgumentException When $key is not valid or when $beta is negative
4344
*/
44-
public function get(string $key, callable $callback, float $beta = null);
45+
public function get(string $key, callable $callback, float $beta = null, array &$metadata = null);
4546

4647
/**
4748
* Removes an item from the pool.

Cache/CacheTrait.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ trait CacheTrait
2424
/**
2525
* {@inheritdoc}
2626
*/
27-
public function get(string $key, callable $callback, float $beta = null)
27+
public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
2828
{
29-
return $this->doGet($this, $key, $callback, $beta);
29+
return $this->doGet($this, $key, $callback, $beta, $metadata);
3030
}
3131

3232
/**
@@ -37,7 +37,7 @@ public function delete(string $key): bool
3737
return $this->deleteItem($key);
3838
}
3939

40-
private function doGet(CacheItemPoolInterface $pool, string $key, callable $callback, ?float $beta)
40+
private function doGet(CacheItemPoolInterface $pool, string $key, callable $callback, ?float $beta, array &$metadata = null)
4141
{
4242
if (0 > $beta = $beta ?? 1.0) {
4343
throw new class(sprintf('Argument "$beta" provided to "%s::get()" must be a positive number, %f given.', \get_class($this), $beta)) extends \InvalidArgumentException implements InvalidArgumentException {
@@ -46,9 +46,9 @@ private function doGet(CacheItemPoolInterface $pool, string $key, callable $call
4646

4747
$item = $pool->getItem($key);
4848
$recompute = !$item->isHit() || INF === $beta;
49+
$metadata = $item instanceof ItemInterface ? $item->getMetadata() : array();
4950

50-
if (!$recompute && $item instanceof ItemInterface) {
51-
$metadata = $item->getMetadata();
51+
if (!$recompute && $metadata) {
5252
$expiry = $metadata[ItemInterface::METADATA_EXPIRY] ?? false;
5353
$ctime = $metadata[ItemInterface::METADATA_CTIME] ?? false;
5454

Cache/TagAwareCacheInterface.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020
*/
2121
interface TagAwareCacheInterface extends CacheInterface
2222
{
23-
/**
24-
* {@inheritdoc}
25-
*/
26-
public function get(string $key, callable $callback, float $beta = null);
27-
2823
/**
2924
* Invalidates cached items using tags.
3025
*

0 commit comments

Comments
 (0)