Skip to content

Commit

Permalink
Add the implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Dec 8, 2023
1 parent 3704432 commit 83beca3
Show file tree
Hide file tree
Showing 13 changed files with 597 additions and 73 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,6 @@
## [Unreleased]

<!--
[Unreleased]: https://github.com/beste/array-cache/compare/1.0.0...main
[1.0.0]: https://github.com/beste/array-cache/tree/1.0.0
[Unreleased]: https://github.com/beste/in-memory-cache-php/compare/1.0.0...main
[1.0.0]: https://github.com/beste/in-memory-cache-php/tree/1.0.0
-->
48 changes: 38 additions & 10 deletions README.md
@@ -1,21 +1,49 @@
# PSR-6 Array Cache
# PSR-6 In-Memory Cache

A PSR-6 Array Cache
A [PSR-6](https://www.php-fig.org/psr/psr-6/) In-Memory cache that can be used as a default implementation and in tests.

<!--
[![Current version](https://img.shields.io/packagist/v/beste/array-cache.svg?logo=composer)](https://packagist.org/packages/beste/array-cache)
[![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/beste/array-cache)](https://packagist.org/packages/beste/array-cache)
[![Monthly Downloads](https://img.shields.io/packagist/dm/beste/array-cache.svg)](https://packagist.org/packages/beste/array-cache/stats)
[![Total Downloads](https://img.shields.io/packagist/dt/beste/array-cache.svg)](https://packagist.org/packages/beste/array-cache/stats)
[![Tests](https://github.com/beste/array-cache/actions/workflows/tests.yml/badge.svg)](https://github.com/beste/array-cache/actions/workflows/tests.yml)
-->
[![Current version](https://img.shields.io/packagist/v/beste/in-memory-cache-php.svg?logo=composer)](https://packagist.org/packages/beste/in-memory-cache-php)
[![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/beste/in-memory-cache-php)](https://packagist.org/packages/beste/in-memory-cache-php)
[![Monthly Downloads](https://img.shields.io/packagist/dm/beste/in-memory-cache-php.svg)](https://packagist.org/packages/beste/in-memory-cache-php/stats)
[![Total Downloads](https://img.shields.io/packagist/dt/beste/in-memory-cache-php.svg)](https://packagist.org/packages/beste/in-memory-cache-php/stats)
[![Tests](https://github.com/beste/in-memory-cache-php/actions/workflows/tests.yml/badge.svg)](https://github.com/beste/in-memory-cache-php/actions/workflows/tests.yml)

## Installation

In order to use this cache implementation, you also need to install a [PSR-20](https://www.php-fig.org/psr/psr-20/) [Clock Implementation](https://packagist.org/providers/psr/clock-implementation),
for example, the [`beste/clock`](https://packagist.org/packages/beste/clock).

```shell
composer require beste/array-cache
composer require beste/in-memory-cache beste/clock
```

## Usage

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

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

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

assert($item->isHit() === false);
assert($item->get() === null);

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

// Later...

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

assert($item->isHit() === true);
assert($item->get() === 'value');
```

The test suite

## Running tests

```shell
Expand Down
26 changes: 17 additions & 9 deletions composer.json
@@ -1,7 +1,8 @@
{
"name": "beste/array-cache",
"description": "A PSR-6 Array Cache",
"license": "ISC",
"name": "beste/in-memory-cache",
"description": "A PSR-6 In-Memory cache that can be used as a fallback implementation and/or in tests.",
"keywords": ["cache", "psr-6", "beste"],
"license": "MIT",
"type": "library",
"authors": [
{
Expand All @@ -10,26 +11,33 @@
}
],
"require": {
"php": "~8.2.0 || ~8.3.0"
"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"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.40.2",
"beste/clock": "^3.0",
"friendsofphp/php-cs-fixer": "^3.41.0",
"phpstan/extension-installer": "^1.3.1",
"phpstan/phpstan": "^1.10.47",
"phpstan/phpstan": "^1.10.48",
"phpstan/phpstan-deprecation-rules": "^1.1.4",
"phpstan/phpstan-phpunit": "^1.3.15",
"phpstan/phpstan-strict-rules": "^1.5.2",
"phpunit/phpunit": "^10.5.1",
"phpunit/phpunit": "^10.5.2",
"symfony/var-dumper": "^6.4.0"
},
"provide": {
"psr/cache-implementation": "2.0 || 3.0"
},
"autoload": {
"psr-4": {
"Beste\\Psr\\Cache\\": "src"
"Beste\\Cache\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Beste\\Psr\\Cache\\Tests\\": "tests"
"Beste\\Cache\\Tests\\": "tests"
}
},
"config": {
Expand Down
1 change: 0 additions & 1 deletion phpunit.dist.xml
Expand Up @@ -2,7 +2,6 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResultFile=".build/.phpunit.result.cache"
colors="true"
>
<testsuites>
Expand Down
81 changes: 81 additions & 0 deletions src/CacheItem.php
@@ -0,0 +1,81 @@
<?php

namespace Beste\Cache;

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

/**
* @internal
*/
final class CacheItem implements CacheItemInterface
{
private mixed $value;
private ?\DateTimeInterface $expiresAt;
private bool $isHit;

public function __construct(private readonly CacheKey $key, private readonly ClockInterface $clock)
{
$this->value = null;
$this->expiresAt = null;
$this->isHit = false;
}

public function getKey(): string
{
return $this->key->toString();
}

public function get(): mixed
{
if ($this->isHit()) {
return $this->value;
}

return null;
}

public function isHit(): bool
{
if ($this->isHit === false) {
return false;
}

if ($this->expiresAt === null) {
return true;
}

return $this->clock->now()->getTimestamp() < $this->expiresAt->getTimestamp();
}

public function set(mixed $value): static
{
$this->isHit = true;
$this->value = $value;

return $this;
}

public function expiresAt(?\DateTimeInterface $expiration): static
{
$this->expiresAt = $expiration;

return $this;
}

public function expiresAfter(\DateInterval|int|null $time): static
{
if ($time === null) {
$this->expiresAt = null;
return $this;
}

if (is_int($time)) {
$time = new \DateInterval("PT{$time}S");
}

$this->expiresAt = $this->clock->now()->add($time);

return $this;
}
}
25 changes: 25 additions & 0 deletions src/CacheKey.php
@@ -0,0 +1,25 @@
<?php

namespace Beste\Cache;

/**
* @internal
*/
final class CacheKey
{
private function __construct(private readonly string $value) {}

public static function fromString(string $value): self
{
if (preg_match('/^[a-zA-Z0-9_.]{1,64}$/u', $value) !== 1) {
throw InvalidArgument::invalidKey();
}

return new self($value);
}

public function toString(): string
{
return $this->value;
}
}
108 changes: 108 additions & 0 deletions src/InMemoryCache.php
@@ -0,0 +1,108 @@
<?php

namespace Beste\Cache;

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

final class InMemoryCache implements CacheItemPoolInterface
{
/** @var array<string, CacheItemInterface> */
private array $items;
/** @var array<string, CacheItemInterface> */
private array $deferredItems;

public function __construct(private readonly ClockInterface $clock)
{
$this->items = [];
$this->deferredItems = [];
}

public function getItem(string $key): CacheItemInterface
{
$key = CacheKey::fromString($key);

$item = $this->items[$key->toString()] ?? null;

if ($item === null) {
return new CacheItem($key, $this->clock);
}

return clone $item;
}

/**
* @return iterable<CacheItemInterface>
*/
public function getItems(array $keys = []): iterable
{
if ($keys === []) {
return [];
}

$items = [];

foreach ($keys as $key) {
$items[$key] = $this->getItem($key);
}

return $items;
}

public function hasItem(string $key): bool
{
return $this->getItem($key)->isHit();
}

public function clear(): bool
{
$this->items = [];
$this->deferredItems = [];

return true;
}

public function deleteItem(string $key): bool
{
$key = CacheKey::fromString($key);

unset($this->items[$key->toString()]);

return true;
}

public function deleteItems(array $keys): bool
{
foreach ($keys as $key) {
$this->deleteItem($key);
}

return true;
}

public function save(CacheItemInterface $item): bool
{
$this->items[$item->getKey()] = $item;

return true;
}

public function saveDeferred(CacheItemInterface $item): bool
{
$this->deferredItems[$item->getKey()] = $item;

return true;
}

public function commit(): bool
{
foreach ($this->deferredItems as $item) {
$this->save($item);
}

$this->deferredItems = [];

return true;
}
}
11 changes: 11 additions & 0 deletions src/InvalidArgument.php
@@ -0,0 +1,11 @@
<?php

namespace Beste\Cache;

final class InvalidArgument extends \InvalidArgumentException implements \Psr\Cache\InvalidArgumentException
{
public static function invalidKey(): self
{
return new self('The given key is not valid');
}
}
20 changes: 0 additions & 20 deletions src/Placeholder.php

This file was deleted.

0 comments on commit 83beca3

Please sign in to comment.