Skip to content

Commit

Permalink
Merge pull request #133 from moufmouf/cache_findObjectsQueryFactory
Browse files Browse the repository at this point in the history
Performance: adding cache for results of FindObjectsQueryFactory::compute
  • Loading branch information
moufmouf authored Apr 24, 2019
2 parents 63a2071 + 44979d5 commit 42960ab
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/QueryFactory/FindObjectsQueryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace TheCodingMachine\TDBM\QueryFactory;

use Doctrine\Common\Cache\Cache;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Schema\Schema;
use TheCodingMachine\TDBM\OrderByAnalyzer;
Expand All @@ -16,17 +17,32 @@ class FindObjectsQueryFactory extends AbstractQueryFactory
private $mainTable;
private $additionalTablesFetch;
private $filterString;
/**
* @var Cache
*/
private $cache;

public function __construct(string $mainTable, array $additionalTablesFetch, $filterString, $orderBy, TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer)
public function __construct(string $mainTable, array $additionalTablesFetch, $filterString, $orderBy, TDBMService $tdbmService, Schema $schema, OrderByAnalyzer $orderByAnalyzer, Cache $cache)
{
parent::__construct($tdbmService, $schema, $orderByAnalyzer, $orderBy);
$this->mainTable = $mainTable;
$this->additionalTablesFetch = $additionalTablesFetch;
$this->filterString = $filterString;
$this->cache = $cache;
}

protected function compute(): void
{
$key = 'FindObjectsQueryFactory_'.$this->mainTable.'__'.implode('_/_',$this->additionalTablesFetch).'__'.$this->filterString.'__'.$this->orderBy;
if ($this->cache->contains($key)) {
[
$this->magicSql,
$this->magicSqlCount,
$this->columnDescList
] = $this->cache->fetch($key);
return;
}

list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, $this->additionalTablesFetch, $this->orderBy, true);

$sql = 'SELECT DISTINCT '.implode(', ', $columnsList).' FROM MAGICJOIN('.$this->mainTable.')';
Expand Down Expand Up @@ -54,5 +70,11 @@ protected function compute(): void
$this->magicSql = $sql;
$this->magicSqlCount = $countSql;
$this->columnDescList = $columnDescList;

$this->cache->save($key, [
$this->magicSql,
$this->magicSqlCount,
$this->columnDescList,
]);
}
}
2 changes: 1 addition & 1 deletion src/TDBMService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@ public function findObjects(string $mainTable, $filter = null, array $parameters

$parameters = array_merge($parameters, $additionalParameters);

$queryFactory = new FindObjectsQueryFactory($mainTable, $additionalTablesFetch, $filterString, $orderString, $this, $this->tdbmSchemaAnalyzer->getSchema(), $this->orderByAnalyzer);
$queryFactory = new FindObjectsQueryFactory($mainTable, $additionalTablesFetch, $filterString, $orderString, $this, $this->tdbmSchemaAnalyzer->getSchema(), $this->orderByAnalyzer, $this->cache);

return new ResultIterator($queryFactory, $parameters, $this->objectStorage, $className, $this, $this->magicQuery, $mode, $this->logger);
}
Expand Down
8 changes: 8 additions & 0 deletions src/UncheckedOrderBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ public function getOrderBy() : string
{
return $this->orderBy;
}

/**
* @return string
*/
public function __toString()
{
return 'UncheckedOrderBy_'.$this->orderBy;
}
}

0 comments on commit 42960ab

Please sign in to comment.