allow vendord
All checks were successful
Build, Push and Deploy / build-and-push (push) Successful in 3m3s
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 16s
Build, Push and Deploy / deploy-staging (push) Successful in 32s
Build, Push and Deploy / deploy-production (push) Has been skipped
Build, Push and Deploy / cleanup (push) Successful in 1s

This commit is contained in:
2026-03-30 14:54:57 +07:00
parent 66aed7c4e8
commit b5e3a778ce
21316 changed files with 2777892 additions and 13 deletions

View File

@@ -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;
}
}

View File

@@ -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_.-]+$/u', $value) !== 1) {
throw InvalidArgument::invalidKey();
}
return new self($value);
}
public function toString(): string
{
return $this->value;
}
}

View File

@@ -0,0 +1,119 @@
<?php
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(
?ClockInterface $clock = null,
) {
$this->clock = $clock ?? new class implements ClockInterface {
public function now(): DateTimeImmutable
{
return new DateTimeImmutable();
}
};
$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;
}
}

View File

@@ -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');
}
}