Skip to content

[Cache] Add optional ClockInterface to ArrayAdapter #57836

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
merged 1 commit into from
Jul 26, 2024
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
15 changes: 11 additions & 4 deletions src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Cache\Adapter;

use Psr\Cache\CacheItemInterface;
use Psr\Clock\ClockInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\Cache\CacheItem;
Expand Down Expand Up @@ -44,6 +45,7 @@
private bool $storeSerialized = true,
private float $maxLifetime = 0,
private int $maxItems = 0,
private ?ClockInterface $clock = null,
) {
if (0 > $maxLifetime) {
throw new InvalidArgumentException(\sprintf('Argument $maxLifetime must be positive, %F passed.', $maxLifetime));
Expand Down Expand Up @@ -94,7 +96,7 @@

public function hasItem(mixed $key): bool
{
if (\is_string($key) && isset($this->expiries[$key]) && $this->expiries[$key] > microtime(true)) {
if (\is_string($key) && isset($this->expiries[$key]) && $this->expiries[$key] > $this->getCurrentTime()) {
if ($this->maxItems) {
// Move the item last in the storage
$value = $this->values[$key];
Expand Down Expand Up @@ -129,7 +131,7 @@
{
\assert(self::validateKeys($keys));

return $this->generateItems($keys, microtime(true), self::$createCacheItem);
return $this->generateItems($keys, $this->getCurrentTime(), self::$createCacheItem);
}

public function deleteItem(mixed $key): bool
Expand Down Expand Up @@ -159,7 +161,7 @@
$value = $item["\0*\0value"];
$expiry = $item["\0*\0expiry"];

$now = microtime(true);
$now = $this->getCurrentTime();

if (null !== $expiry) {
if (!$expiry) {
Expand Down Expand Up @@ -216,7 +218,7 @@
public function clear(string $prefix = ''): bool
{
if ('' !== $prefix) {
$now = microtime(true);
$now = $this->getCurrentTime();

foreach ($this->values as $key => $value) {
if (!isset($this->expiries[$key]) || $this->expiries[$key] <= $now || str_starts_with($key, $prefix)) {
Expand Down Expand Up @@ -356,4 +358,9 @@

return true;
}

private function getCurrentTime(): float

Check failure on line 362 in src/Symfony/Component/Cache/Adapter/ArrayAdapter.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidReturnType

src/Symfony/Component/Cache/Adapter/ArrayAdapter.php:362:40: InvalidReturnType: The declared return type 'float' for Symfony\Component\Cache\Adapter\ArrayAdapter::getCurrentTime is incorrect, got 'float|non-empty-string' (see https://psalm.dev/011)

Check failure on line 362 in src/Symfony/Component/Cache/Adapter/ArrayAdapter.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidReturnType

src/Symfony/Component/Cache/Adapter/ArrayAdapter.php:362:40: InvalidReturnType: The declared return type 'float' for Symfony\Component\Cache\Adapter\ArrayAdapter::getCurrentTime is incorrect, got 'float|non-empty-string' (see https://psalm.dev/011)
{
return $this->clock?->now()->format('U.u') ?? microtime(true);

Check failure on line 364 in src/Symfony/Component/Cache/Adapter/ArrayAdapter.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidReturnStatement

src/Symfony/Component/Cache/Adapter/ArrayAdapter.php:364:16: InvalidReturnStatement: The inferred type 'float|non-empty-string' does not match the declared return type 'float' for Symfony\Component\Cache\Adapter\ArrayAdapter::getCurrentTime (see https://psalm.dev/128)

Check failure on line 364 in src/Symfony/Component/Cache/Adapter/ArrayAdapter.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidReturnStatement

src/Symfony/Component/Cache/Adapter/ArrayAdapter.php:364:16: InvalidReturnStatement: The inferred type 'float|non-empty-string' does not match the declared return type 'float' for Symfony\Component\Cache\Adapter\ArrayAdapter::getCurrentTime (see https://psalm.dev/128)
}
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/Cache/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Add optional `Psr\Clock\ClockInterface` parameter to `ArrayAdapter`

7.1
---

Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/ArrayAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Tests\Fixtures\TestEnum;
use Symfony\Component\Clock\MockClock;

/**
* @group time-sensitive
Expand Down Expand Up @@ -102,4 +103,17 @@ public function testEnum()

$this->assertSame(TestEnum::Foo, $cache->getItem('foo')->get());
}

public function testClockAware()
{
$clock = new MockClock();
$cache = new ArrayAdapter(10, false, 0, 0, $clock);

$cache->save($cache->getItem('foo'));
$this->assertTrue($cache->hasItem('foo'));

$clock->modify('+11 seconds');

$this->assertFalse($cache->hasItem('foo'));
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Cache/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"doctrine/dbal": "^3.6|^4",
"predis/predis": "^1.1|^2.0",
"psr/simple-cache": "^1.0|^2.0|^3.0",
"symfony/clock": "^6.4|^7.0",
"symfony/config": "^6.4|^7.0",
"symfony/dependency-injection": "^6.4|^7.0",
"symfony/filesystem": "^6.4|^7.0",
Expand Down
Loading