Skip to content

Commit

Permalink
Enable Cache Query results
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Nov 8, 2024
1 parent e0e709c commit 0815a84
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"require": {
"php": ">=8.1 <8.4",
"ext-json": "*",
"byjg/anydataset-db": "^5.0"
"byjg/anydataset-db": "5.0.x-dev"
},
"require-dev": {
"phpunit/phpunit": "^9.6",
Expand Down
9 changes: 8 additions & 1 deletion src/CacheQueryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
class CacheQueryResult
{
protected CacheInterface $cache;
protected string $cacheKey;
protected int|DateInterval $ttl;

public function __construct(CacheInterface $cache, int|DateInterval $ttl)
public function __construct(CacheInterface $cache, string $cacheKey, int|DateInterval $ttl)
{
$this->cache = $cache;
$this->cacheKey = $cacheKey;
$this->ttl = $ttl;
}

Expand All @@ -21,6 +23,11 @@ public function getCache(): CacheInterface
return $this->cache;
}

public function getCacheKey(): string
{
return $this->cacheKey;
}

public function getTtl(): DateInterval|int
{
return $this->ttl;
Expand Down
7 changes: 6 additions & 1 deletion src/QueryBasic.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ByJG\AnyDataset\Core\GenericIterator;
use ByJG\AnyDataset\Db\DbDriverInterface;
use ByJG\AnyDataset\Db\SqlStatement;
use ByJG\MicroOrm\Exception\InvalidArgumentException;
use ByJG\MicroOrm\Interface\QueryBuilderInterface;
use ByJG\Serializer\Serialize;
Expand Down Expand Up @@ -275,6 +276,10 @@ public function build(?DbDriverInterface $dbDriver = null): SqlObject
public function buildAndGetIterator(?DbDriverInterface $dbDriver = null, ?CacheQueryResult $cache = null): GenericIterator
{
$sqlObject = $this->build($dbDriver);
return $dbDriver->getIterator($sqlObject->getSql(), $sqlObject->getParameters(), $cache?->getCache(), $cache?->getTtl() ?? 60);
$sqlStatement = new SqlStatement($sqlObject->getSql());
if (!empty($cache)) {
$sqlStatement->withCache($cache->getCache(), $cache->getCacheKey(), $cache->getTtl());
}
return $sqlStatement->getIterator($dbDriver, $sqlObject->getParameters());
}
}
8 changes: 6 additions & 2 deletions src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ByJG\AnyDataset\Core\IteratorFilter;
use ByJG\AnyDataset\Db\DbDriverInterface;
use ByJG\AnyDataset\Db\IteratorFilterSqlFormatter;
use ByJG\AnyDataset\Db\SqlStatement;
use ByJG\MicroOrm\Exception\InvalidArgumentException;
use ByJG\MicroOrm\Exception\OrmBeforeInvalidException;
use ByJG\MicroOrm\Exception\OrmInvalidFieldsException;
Expand Down Expand Up @@ -258,9 +259,12 @@ public function getByQuery(QueryBuilderInterface $query, array $mapper = [], ?Ca
$sqlBuild = $query->build($this->getDbDriver());

$params = $sqlBuild->getParameters();
$sql = $sqlBuild->getSql();
$sql = new SqlStatement($sqlBuild->getSql());
if (!empty($cache)) {
$sql->withCache($cache->getCache(), $cache->getCacheKey(), $cache->getTtl());
}
$result = [];
$iterator = $this->getDbDriver()->getIterator($sql, $params, $cache?->getCache(), $cache?->getTtl() ?? 60);
$iterator = $sql->getIterator($this->getDbDriver(), $params);

foreach ($iterator as $row) {
$collection = [];
Expand Down
7 changes: 6 additions & 1 deletion src/Union.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ByJG\AnyDataset\Core\GenericIterator;
use ByJG\AnyDataset\Db\DbDriverInterface;
use ByJG\AnyDataset\Db\SqlStatement;
use ByJG\MicroOrm\Exception\InvalidArgumentException;
use ByJG\MicroOrm\Interface\QueryBuilderInterface;

Expand Down Expand Up @@ -106,6 +107,10 @@ public function build(?DbDriverInterface $dbDriver = null): SqlObject
public function buildAndGetIterator(?DbDriverInterface $dbDriver = null, ?CacheQueryResult $cache = null): GenericIterator
{
$sqlObject = $this->build($dbDriver);
return $dbDriver->getIterator($sqlObject->getSql(), $sqlObject->getParameters(), $cache?->getCache(), $cache?->getTtl() ?? 60);
$sqlStatement = new SqlStatement($sqlObject->getSql());
if (!empty($cache)) {
$sqlStatement->withCache($cache->getCache(), $cache->getCacheKey(), $cache->getTtl());
}
return $sqlStatement->getIterator($dbDriver, $sqlObject->getParameters());
}
}
12 changes: 8 additions & 4 deletions tests/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,10 @@ public function testBuildAndGetIteratorWithCache()
$result = $query->buildAndGetIterator($this->repository->getDbDriver())->toArray();
$this->assertCount(1, $result);

$cacheObject = new CacheQueryResult($cacheEngine, "mykey", 120);

// Get the result and save to cache
$result = $query->buildAndGetIterator($this->repository->getDbDriver(), cache: new CacheQueryResult($cacheEngine, 120))->toArray();
$result = $query->buildAndGetIterator($this->repository->getDbDriver(), cache: $cacheObject)->toArray();
$this->assertCount(1, $result);

// Delete the record
Expand All @@ -202,7 +204,7 @@ public function testBuildAndGetIteratorWithCache()
$this->assertCount(0, $result);

// Check if query with cache the record is found
$result = $query->buildAndGetIterator($this->repository->getDbDriver(), cache: new CacheQueryResult($cacheEngine, 120))->toArray();
$result = $query->buildAndGetIterator($this->repository->getDbDriver(), cache: $cacheObject)->toArray();
$this->assertCount(1, $result);
}

Expand Down Expand Up @@ -1288,8 +1290,10 @@ public function testQueryWithCache()
$result = $infoRepository->getByQuery($query);
$this->assertCount(1, $result);

$cacheObject = new CacheQueryResult($cacheEngine, "qry", 120);

// Get the result and save to cache
$result = $infoRepository->getByQuery($query, cache: new CacheQueryResult($cacheEngine, 120));
$result = $infoRepository->getByQuery($query, cache: $cacheObject);
$this->assertCount(1, $result);

// Delete the record
Expand All @@ -1303,7 +1307,7 @@ public function testQueryWithCache()
$this->assertCount(0, $result);

// Check if query with cache the record is found
$result = $infoRepository->getByQuery($query, cache: new CacheQueryResult($cacheEngine, 120));
$result = $infoRepository->getByQuery($query, cache: $cacheObject);
$this->assertCount(1, $result);
}

Expand Down

0 comments on commit 0815a84

Please sign in to comment.