ulmus/src/CacheTrait.php
2024-12-02 15:22:30 -05:00

117 lines
3.8 KiB
PHP

<?php
namespace Ulmus;
use Psr\SimpleCache\CacheInterface;
use Ulmus\Entity\EntityInterface;
use Ulmus\QueryBuilder\QueryBuilderInterface;
use Ulmus\Repository\RepositoryInterface;
trait CacheTrait
{
protected CacheInterface $cache;
public function attachCachingObject(CacheInterface $cache) : self
{
$cacheKey = "";
$this->cache = $cache;
# Reading from cache
$this->eventRegister(new class($cacheKey) implements Event\Repository\CollectionFromQueryDatasetInterface {
public function __construct(
protected string & $cacheKey
) {}
public function execute(RepositoryInterface $repository, array &$data): void
{
$this->cacheKey = $repository->queryBuilder->hashSerializedQuery();
$data = $repository->getFromCache( $this->cacheKey) ?: [];
}
});
# Setting to cache
$this->eventRegister(new class($cacheKey) implements Event\Repository\CollectionFromQueryInterface {
public function __construct(
protected string & $cacheKey
) {}
public function execute(RepositoryInterface $repository, EntityCollection $collection): EntityCollection
{
$repository->setToCache( $this->cacheKey, $collection->map(fn(EntityInterface $e) => $e->entityGetDataset(false, true)));
$this->cacheKey = "";
return $collection;
}
});
$this->eventRegister(new class($this->cache, $this->entityCacheKey()) implements Event\Query\Insert {
use Cache\CacheEventTrait;
public function execute(RepositoryInterface $repository, object|array $entity, ?array $dataset = null, bool $replace = false): void
{
$this->purgeCache();
}
});
# Cache invalidation
$this->eventRegister(new class($this->cache, $this->entityCacheKey()) implements Event\Query\Update {
use Cache\CacheEventTrait;
public function execute(RepositoryInterface $repository, object|array $entity, ?array $dataset = null, bool $replace = false): void
{
$this->purgeCache();
}
});
$this->eventRegister(new class($this->cache, $this->entityCacheKey()) implements Event\Query\Delete {
use Cache\CacheEventTrait;
public function execute(RepositoryInterface $repository, EntityInterface $entity): void
{
$this->purgeCache();
}
});
$this->eventRegister(new class($this->cache, $this->entityCacheKey()) implements Event\Query\Truncate {
use Cache\CacheEventTrait;
public function execute(RepositoryInterface $repository, ?string $table = null, ?string $alias = null, ?string $schema = null): void
{
$this->purgeCache();
}
});
return $this;
}
protected function entityCacheKey() : string
{
return sprintf("%s.%s", $this->entityResolver->databaseName(), $this->entityResolver->tableName());
}
public function getFromCache(string $key) : mixed
{
$keys = $this->cache->get($this->entityCacheKey(), []);
if (in_array($key, $keys)) {
return $this->cache->get(sprintf("%s:%s:", $this->entityCacheKey(), $key));
}
return null;
}
public function setToCache(string $key, mixed $value) : void
{
$keys = $this->cache->get($this->entityCacheKey(), []);
if (! in_array($key, $keys)) {
$keys[] = $key;
$this->cache->set($this->entityCacheKey(), $keys);
}
$this->cache->set(sprintf("%s:%s:", $this->entityCacheKey(), $key), $value);
}
}