Skip to content

Commit

Permalink
Added result caching for QueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
twoleds committed Jul 29, 2022
1 parent d6aaae9 commit bb591f8
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 27 deletions.
23 changes: 23 additions & 0 deletions docs/en/reference/query-builder.rst
Expand Up @@ -369,3 +369,26 @@ in your query as a return value:
->where('email = ' . $queryBuilder->createPositionalParameter($userInputEmail))
;
// SELECT id, name FROM users WHERE email = ?
Caching
-------

To use the result cache, it is necessary to call the method
``enableResultCache($cacheProfile)`` and pass a instance of
``Doctrine\DBAL\Cache\QueryCacheProfile`` with a cache lifetime
value in seconds. A cache key can optionally be added if needed.

.. code-block:: php
<?php
$queryBuilder
->select('id', 'name')
->from('users')
->enableResultCache(new QueryCacheProfile(300, 'some-key'))
;
.. note::

For more details and how to configure the result cache check
`caching section in documentation <./caching.rst>`.
64 changes: 54 additions & 10 deletions src/Query/QueryBuilder.php
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\DBAL\Query;

use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\ParameterType;
Expand Down Expand Up @@ -123,6 +124,11 @@ class QueryBuilder
*/
private int $boundCounter = 0;

/**
* The query cache profile used for caching results.
*/
private ?QueryCacheProfile $resultCacheProfile = null;

/**
* Initializes a new <tt>QueryBuilder</tt>.
*
Expand Down Expand Up @@ -194,7 +200,7 @@ public function getState()
*/
public function fetchAssociative()
{
return $this->connection->fetchAssociative($this->getSQL(), $this->params, $this->paramTypes);
return $this->executeQuery()->fetchAssociative();
}

/**
Expand All @@ -207,7 +213,7 @@ public function fetchAssociative()
*/
public function fetchNumeric()
{
return $this->connection->fetchNumeric($this->getSQL(), $this->params, $this->paramTypes);
return $this->executeQuery()->fetchNumeric();
}

/**
Expand All @@ -220,7 +226,7 @@ public function fetchNumeric()
*/
public function fetchOne()
{
return $this->connection->fetchOne($this->getSQL(), $this->params, $this->paramTypes);
return $this->executeQuery()->fetchOne();
}

/**
Expand All @@ -232,7 +238,7 @@ public function fetchOne()
*/
public function fetchAllNumeric(): array
{
return $this->connection->fetchAllNumeric($this->getSQL(), $this->params, $this->paramTypes);
return $this->executeQuery()->fetchAllNumeric();
}

/**
Expand All @@ -244,7 +250,7 @@ public function fetchAllNumeric(): array
*/
public function fetchAllAssociative(): array
{
return $this->connection->fetchAllAssociative($this->getSQL(), $this->params, $this->paramTypes);
return $this->executeQuery()->fetchAllAssociative();
}

/**
Expand All @@ -257,7 +263,7 @@ public function fetchAllAssociative(): array
*/
public function fetchAllKeyValue(): array
{
return $this->connection->fetchAllKeyValue($this->getSQL(), $this->params, $this->paramTypes);
return $this->executeQuery()->fetchAllKeyValue();
}

/**
Expand All @@ -271,7 +277,7 @@ public function fetchAllKeyValue(): array
*/
public function fetchAllAssociativeIndexed(): array
{
return $this->connection->fetchAllAssociativeIndexed($this->getSQL(), $this->params, $this->paramTypes);
return $this->executeQuery()->fetchAllAssociativeIndexed();
}

/**
Expand All @@ -283,7 +289,7 @@ public function fetchAllAssociativeIndexed(): array
*/
public function fetchFirstColumn(): array
{
return $this->connection->fetchFirstColumn($this->getSQL(), $this->params, $this->paramTypes);
return $this->executeQuery()->fetchFirstColumn();
}

/**
Expand All @@ -293,7 +299,12 @@ public function fetchFirstColumn(): array
*/
public function executeQuery(): Result
{
return $this->connection->executeQuery($this->getSQL(), $this->params, $this->paramTypes);
return $this->connection->executeQuery(
$this->getSQL(),
$this->params,
$this->paramTypes,
$this->resultCacheProfile
);
}

/**
Expand Down Expand Up @@ -328,7 +339,7 @@ public function execute()
'QueryBuilder::execute() is deprecated, use QueryBuilder::executeQuery() for SQL queries instead.'
);

return $this->connection->executeQuery($this->getSQL(), $this->params, $this->paramTypes);
return $this->executeQuery();
}

Deprecation::trigger(
Expand Down Expand Up @@ -1548,4 +1559,37 @@ public function __clone()
$this->params[$name] = clone $param;
}
}

/**
* Enables caching of the results of this query, for given amount of seconds
* and optionally specified witch key to use for the cache entry.
*
* @return $this
*/
public function enableResultCache(QueryCacheProfile $cacheProfile): self
{
$this->resultCacheProfile = $cacheProfile;

return $this;
}

/**
* Disables caching of the results of this query.
*
* @return $this
*/
public function disableResultCache(): self
{
$this->resultCacheProfile = null;

return $this;
}

/**
* Gets the cache profile for the result caching.
*/
public function getResultCacheProfile(): ?QueryCacheProfile
{
return $this->resultCacheProfile;
}
}

0 comments on commit bb591f8

Please sign in to comment.