- Added caching mechanism as an event listener
This commit is contained in:
1 parent
b601939459
commit
9eed2fe1a8
25 files changed
+607
-402
No files matched your search
@@ -0,0 +1,117 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user