File tree Expand file tree Collapse file tree 5 files changed +55
-8
lines changed Expand file tree Collapse file tree 5 files changed +55
-8
lines changed Original file line number Diff line number Diff line change 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
710Initial Release
Original file line number Diff line number Diff line change @@ -21,10 +21,8 @@ composer require beste/in-memory-cache beste/clock
2121
2222``` php
2323use 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);
4240assert($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
Original file line number Diff line number Diff line change 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" ,
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"
Original file line number Diff line number Diff line change 22
33namespace Beste \Cache ;
44
5+ use DateTimeImmutable ;
56use Psr \Cache \CacheItemInterface ;
67use Psr \Cache \CacheItemPoolInterface ;
78use Psr \Clock \ClockInterface ;
89
910final 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 }
Original file line number Diff line number Diff 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 ' );
You can’t perform that action at this time.
0 commit comments