Skip to content

Commit 2df5490

Browse files
[Cache] add logs on early-recomputation and locking
1 parent efda127 commit 2df5490

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

CacheTrait.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Psr\Cache\CacheItemPoolInterface;
1515
use Psr\Cache\InvalidArgumentException;
16+
use Psr\Log\LoggerInterface;
1617

1718
/**
1819
* An implementation of CacheInterface for PSR-6 CacheItemPoolInterface classes.
@@ -37,7 +38,7 @@ public function delete(string $key): bool
3738
return $this->deleteItem($key);
3839
}
3940

40-
private function doGet(CacheItemPoolInterface $pool, string $key, callable $callback, ?float $beta, array &$metadata = null)
41+
private function doGet(CacheItemPoolInterface $pool, string $key, callable $callback, ?float $beta, array &$metadata = null, LoggerInterface $logger = null)
4142
{
4243
if (0 > $beta = $beta ?? 1.0) {
4344
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 {
@@ -52,9 +53,13 @@ private function doGet(CacheItemPoolInterface $pool, string $key, callable $call
5253
$expiry = $metadata[ItemInterface::METADATA_EXPIRY] ?? false;
5354
$ctime = $metadata[ItemInterface::METADATA_CTIME] ?? false;
5455

55-
if ($recompute = $ctime && $expiry && $expiry <= microtime(true) - $ctime / 1000 * $beta * log(random_int(1, PHP_INT_MAX) / PHP_INT_MAX)) {
56+
if ($recompute = $ctime && $expiry && $expiry <= ($now = microtime(true)) - $ctime / 1000 * $beta * log(random_int(1, PHP_INT_MAX) / PHP_INT_MAX)) {
5657
// force applying defaultLifetime to expiry
5758
$item->expiresAt(null);
59+
$this->logger && $this->logger->info('Item "{key}" elected for early recomputation {delta}s before its expiration', [
60+
'key' => $key,
61+
'delta' => sprintf('%.1f', $expiry - $now),
62+
]);
5863
}
5964
}
6065

0 commit comments

Comments
 (0)