Skip to content

Commit 1c89a02

Browse files
committed
Allow using the library without providing a PSR-20 clock implementation
1 parent a8d04f6 commit 1c89a02

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## [Unreleased]
44

5+
* The Cache can now be instantiated without providing a [PSR-20](https://www.php-fig.org/psr/psr-20/) clock implementation.
6+
* The library doesn't depend on the [`beste/clock` library](https://github.com/beste/clock) anymore.
7+
58
## [1.0.0] - 2023-12-09
69

710
Initial Release

README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ composer require beste/in-memory-cache beste/clock
2121

2222
```php
2323
use Beste\Cache\InMemoryCache;
24-
use Beste\Clock\SystemClock;
2524

26-
$clock = SystemClock::create();
27-
$cache = new InMemoryCache($clock);
25+
$cache = new InMemoryCache();
2826

2927
$item = $cache->getItem('key');
3028

@@ -42,7 +40,26 @@ assert($item->isHit() === true);
4240
assert($item->get() === 'value');
4341
```
4442

45-
The test suite
43+
You can also provide your own [PSR-20](https://www.php-fig.org/psr/psr-20/) clock implementation, for example a frozen
44+
clock for testing, for example from the [`beste/clock` library](https://github.com/beste/clock).
45+
46+
```php
47+
use Beste\Clock\FrozenClock;
48+
use Beste\Cache\InMemoryCache;
49+
50+
$clock = FrozenClock::fromUTC()
51+
$cache = new InMemoryCache();
52+
53+
$item = $cache->getItem('key');
54+
$item->set('value')->expiresAfter(new DateInterval('PT5M'));
55+
$cache->save($item);
56+
57+
$clock->setTo($clock->now()->add(new DateInterval('PT2M')));
58+
assert($cache->getItem('key')->isHit() === true);
59+
60+
$clock->setTo($clock->now()->add(new DateInterval('PT5M')));
61+
assert($cache->getItem('key')->isHit() === false);
62+
```
4663

4764
## Running tests
4865

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"require": {
1414
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
1515
"psr/cache": "^2.0 || ^3.0",
16-
"psr/clock": "^1.0",
17-
"psr/clock-implementation": "^1.0"
16+
"psr/clock": "^1.0"
1817
},
1918
"require-dev": {
2019
"beste/clock": "^3.0",
@@ -30,6 +29,9 @@
3029
"provide": {
3130
"psr/cache-implementation": "2.0 || 3.0"
3231
},
32+
"suggest": {
33+
"psr/clock-implementation": "Allows injecting a Clock, for example a frozen clock for testing"
34+
},
3335
"autoload": {
3436
"psr-4": {
3537
"Beste\\Cache\\": "src"

src/InMemoryCache.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,30 @@
22

33
namespace Beste\Cache;
44

5+
use DateTimeImmutable;
56
use Psr\Cache\CacheItemInterface;
67
use Psr\Cache\CacheItemPoolInterface;
78
use Psr\Clock\ClockInterface;
89

910
final class InMemoryCache implements CacheItemPoolInterface
1011
{
12+
private readonly ClockInterface $clock;
13+
1114
/** @var array<string, CacheItemInterface> */
1215
private array $items;
1316
/** @var array<string, CacheItemInterface> */
1417
private array $deferredItems;
1518

16-
public function __construct(private readonly ClockInterface $clock)
17-
{
19+
public function __construct(
20+
ClockInterface $clock = null
21+
) {
22+
$this->clock = $clock ?? new class () implements ClockInterface {
23+
public function now(): DateTimeImmutable
24+
{
25+
return new DateTimeImmutable();
26+
}
27+
28+
};
1829
$this->items = [];
1930
$this->deferredItems = [];
2031
}

tests/InMemoryCacheTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ protected function setUp(): void
2020
$this->pool = new InMemoryCache($this->clock);
2121
}
2222

23+
public function testItWorksWithouProvidingAClock(): void
24+
{
25+
$pool = new InMemoryCache();
26+
$item = $pool->getItem('item');
27+
28+
self::assertFalse($item->isHit());
29+
30+
$item->set('value');
31+
$pool->save($item);
32+
33+
$item = $pool->getItem('item');
34+
self::assertTrue($item->isHit());
35+
}
36+
2337
public function testItReturnsANewItem(): void
2438
{
2539
$item = $this->pool->getItem('item');

0 commit comments

Comments
 (0)