Skip to content

Commit

Permalink
Allow using the library without providing a PSR-20 clock implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Mar 1, 2024
1 parent a8d04f6 commit 1c89a02
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## [Unreleased]

* The Cache can now be instantiated without providing a [PSR-20](https://www.php-fig.org/psr/psr-20/) clock implementation.
* The library doesn't depend on the [`beste/clock` library](https://github.com/beste/clock) anymore.

## [1.0.0] - 2023-12-09

Initial Release
Expand Down
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ composer require beste/in-memory-cache beste/clock

```php
use Beste\Cache\InMemoryCache;
use Beste\Clock\SystemClock;

$clock = SystemClock::create();
$cache = new InMemoryCache($clock);
$cache = new InMemoryCache();

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

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

The test suite
You can also provide your own [PSR-20](https://www.php-fig.org/psr/psr-20/) clock implementation, for example a frozen
clock for testing, for example from the [`beste/clock` library](https://github.com/beste/clock).

```php
use Beste\Clock\FrozenClock;
use Beste\Cache\InMemoryCache;

$clock = FrozenClock::fromUTC()
$cache = new InMemoryCache();

$item = $cache->getItem('key');
$item->set('value')->expiresAfter(new DateInterval('PT5M'));
$cache->save($item);

$clock->setTo($clock->now()->add(new DateInterval('PT2M')));
assert($cache->getItem('key')->isHit() === true);

$clock->setTo($clock->now()->add(new DateInterval('PT5M')));
assert($cache->getItem('key')->isHit() === false);
```

## Running tests

Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"require": {
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"psr/cache": "^2.0 || ^3.0",
"psr/clock": "^1.0",
"psr/clock-implementation": "^1.0"
"psr/clock": "^1.0"
},
"require-dev": {
"beste/clock": "^3.0",
Expand All @@ -30,6 +29,9 @@
"provide": {
"psr/cache-implementation": "2.0 || 3.0"
},
"suggest": {
"psr/clock-implementation": "Allows injecting a Clock, for example a frozen clock for testing"
},
"autoload": {
"psr-4": {
"Beste\\Cache\\": "src"
Expand Down
15 changes: 13 additions & 2 deletions src/InMemoryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@

namespace Beste\Cache;

use DateTimeImmutable;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Clock\ClockInterface;

final class InMemoryCache implements CacheItemPoolInterface
{
private readonly ClockInterface $clock;

/** @var array<string, CacheItemInterface> */
private array $items;
/** @var array<string, CacheItemInterface> */
private array $deferredItems;

public function __construct(private readonly ClockInterface $clock)
{
public function __construct(
ClockInterface $clock = null
) {
$this->clock = $clock ?? new class () implements ClockInterface {
public function now(): DateTimeImmutable
{
return new DateTimeImmutable();
}

};
$this->items = [];
$this->deferredItems = [];
}
Expand Down
14 changes: 14 additions & 0 deletions tests/InMemoryCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ protected function setUp(): void
$this->pool = new InMemoryCache($this->clock);
}

public function testItWorksWithouProvidingAClock(): void
{
$pool = new InMemoryCache();
$item = $pool->getItem('item');

self::assertFalse($item->isHit());

$item->set('value');
$pool->save($item);

$item = $pool->getItem('item');
self::assertTrue($item->isHit());
}

public function testItReturnsANewItem(): void
{
$item = $this->pool->getItem('item');
Expand Down

0 comments on commit 1c89a02

Please sign in to comment.