8000 feature #57836 [Cache] Add optional ClockInterface to ArrayAdapter (j… · symfony/symfony@b360af7 · GitHub
[go: up one dir, main page]

Skip to content

Commit b360af7

Browse files
committed
feature #57836 [Cache] Add optional ClockInterface to ArrayAdapter (jasiolpn)
This PR was squashed before being merged into the 7.2 branch. Discussion ---------- [Cache] Add optional ClockInterface to ArrayAdapter | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | none | License | MIT Hello. It is common to use `ArrayAdapter` as a cache adapter in test environments. Unfortunately, currently cache item expiration is untestable. On the other hand, `Psr\Clock\ClockInterface` and Symfony Clock component are available for some time. I think it would be great if we make use of `ClockInterface` in `ArrayAdapter`. Commits ------- 351c33d [Cache] Add optional ClockInterface to ArrayAdapter
2 parents 4c82ad4 + 351c33d commit b360af7

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

src/Symfony/Component/Cache/Adapter/ArrayAdapter.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Cache\Adapter;
1313

1414
use Psr\Cache\CacheItemInterface;
15+
use Psr\Clock\ClockInterface;
1516
use Psr\Log\LoggerAwareInterface;
1617
use Psr\Log\LoggerAwareTrait;
1718
use Symfony\Component\Cache\CacheItem;
@@ -44,6 +45,7 @@ public function __construct(
4445
private bool $storeSerialized = true,
4546
private float $maxLifetime = 0,
4647
private int $maxItems = 0,
48+
private ?ClockInterface $clock = null,
4749
) {
4850
if (0 > $maxLifetime) {
4951
throw new InvalidArgumentException(\sprintf('Argument $maxLifetime must be positive, %F passed.', $maxLifetime));
@@ -94,7 +96,7 @@ public function delete(string $key): bool
9496

9597
public function hasItem(mixed $key): bool
9698
{
97-
if (\is_string($key) && isset($this->expiries[$key]) && $this->expiries[$key] > microtime(true)) {
99+
if (\is_string($key) && isset($this->expiries[$key]) && $this->expiries[$key] > $this->getCurrentTime()) {
98100
if ($this->maxItems) {
99101
// Move the item last in the storage
100102
$value = $this->values[$key];
@@ -129,7 +131,7 @@ public function getItems(array $keys = []): iterable
129131
{
130132
\assert(self::validateKeys($keys));
131133

132-
return $this->generateItems($keys, microtime(true), self::$createCacheItem);
134+
return $this->generateItems($keys, $this->getCurrentTime(), self::$createCacheItem);
133135
}
134136

135137
public function deleteItem(mixed $key): bool
@@ -159,7 +161,7 @@ public function save(CacheItemInterface $item): bool
159161
$value = $item["\0*\0value"];
160162
$expiry = $item["\0*\0expiry"];
161163

162-
$now = microtime(true);
164+
$now = $this->getCurrentTime();
163165

164166
if (null !== $expiry) {
165167
if (!$expiry) {
@@ -216,7 +218,7 @@ public function commit(): bool
216218
public function clear(string $prefix = ''): bool
217219
{
218220
if ('' !== $prefix) {
219-
$now = microtime(true);
221+
$now = $this->getCurrentTime();
220222

221223
foreach ($this->values as $key => $value) {
222224
if (!isset($this->expiries[$key]) || $this->expiries[$key] <= $now || str_starts_with($key, $prefix)) {
@@ -356,4 +358,9 @@ private function validateKeys(array $keys): bool
356358

357359
return true;
358360
}
361+
362+
private function getCurrentTime(): float
363+
{
364+
return $this->clock?->now()->format('U.u') ?? microtime(true);
365+
}
359366
}

src/Symfony/Component/Cache/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.2
5+
---
6+
7+
* Add optional `Psr\Clock\ClockInterface` parameter to `ArrayAdapter`
8+
49
7.1
510
---
611

src/Symfony/Component/Cache/Tests/Adapter/ArrayAdapterTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Cache\CacheItemPoolInterface;
1515
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1616
use Symfony\Component\Cache\Tests\Fixtures\TestEnum;
17+
use Symfony\Component\Clock\MockClock;
1718

1819
/**
1920
* @group time-sensitive
@@ -102,4 +103,17 @@ public function testEnum()
102103

103104
$this->assertSame(TestEnum::Foo, $cache->getItem('foo')->get());
104105
}
106+
107+
public function testClockAware()
108+
{
109+
$clock = new MockClock();
110+
$cache = new ArrayAdapter(10, false, 0, 0, $clock);
111+
112+
$cache->save($cache->getItem('foo'));
113+
$this->assertTrue($cache->hasItem('foo'));
114+
115+
$clock->modify('+11 seconds');
116+
117+
$this->assertFalse($cache->hasItem('foo'));
118+
}
105119
}

src/Symfony/Component/Cache/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"doctrine/dbal": "^3.6|^4",
3535
"predis/predis": "^1.1|^2.0",
3636
"psr/simple-cache": "^1.0|^2.0|^3.0",
37+
"symfony/clock": "^6.4|^7.0",
3738
"symfony/config": "^6.4|^7.0",
3839
"symfony/dependency-injection": "^6.4|^7.0",
3940
"symfony/filesystem": "^6.4|^7.0",

0 commit comments

Comments
 (0)
0