-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3704432
commit 885e52a
Showing
14 changed files
with
598 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ jobs: | |
fail-fast: true | ||
matrix: | ||
php: | ||
- "8.1" | ||
- "8.2" | ||
- "8.3" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.