1
0
mirror of synced 2025-02-02 21:41:45 +03:00

Evict result set cache if Query#expireResultCache() was called

This commit is contained in:
Luís Cobucci 2017-04-30 21:12:40 +02:00
parent 85a52d781e
commit e71272e2b4
No known key found for this signature in database
GPG Key ID: EC61C5F01750ED3C
2 changed files with 52 additions and 0 deletions

View File

@ -20,6 +20,7 @@
namespace Doctrine\ORM;
use Doctrine\DBAL\LockMode;
use Doctrine\ORM\Query\Exec\AbstractSqlExecutor;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\ParserResult;
use Doctrine\ORM\Query\QueryException;
@ -322,9 +323,27 @@ final class Query extends AbstractQuery
list($sqlParams, $types) = $this->processParameterMappings($paramMappings);
$this->evictResultSetCache($executor, $sqlParams, $types);
return $executor->execute($this->_em->getConnection(), $sqlParams, $types);
}
private function evictResultSetCache(AbstractSqlExecutor $executor, array $sqlParams, array $types)
{
if (null === $this->_queryCacheProfile || ! $this->getExpireResultCache()) {
return;
}
$cacheDriver = $this->_queryCacheProfile->getResultCacheDriver();
$statements = (array) $executor->getSqlStatements(); // Type casted since it can either be a string or an array
foreach ($statements as $statement) {
$cacheKeys = $this->_queryCacheProfile->generateCacheKeys($statement, $sqlParams, $types);
$cacheDriver->delete(reset($cacheKeys));
}
}
/**
* Evict entity cache region
*/

View File

@ -243,4 +243,37 @@ class QueryTest extends OrmTestCase
$query->setHydrationCacheProfile(null);
$this->assertNull($query->getHydrationCacheProfile());
}
/**
* @group 2947
*/
public function testResultCacheEviction()
{
$this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache());
$query = $this->_em->createQuery("SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u")
->useResultCache(true);
/** @var DriverConnectionMock $driverConnectionMock */
$driverConnectionMock = $this->_em->getConnection()
->getWrappedConnection();
$driverConnectionMock->setStatementMock(new StatementArrayMock([['id_0' => 1]]));
// Performs the query and sets up the initial cache
self::assertCount(1, $query->getResult());
$driverConnectionMock->setStatementMock(new StatementArrayMock([['id_0' => 1], ['id_0' => 2]]));
// Retrieves cached data since expire flag is false and we have a cached result set
self::assertCount(1, $query->getResult());
// Performs the query and caches the result set since expire flag is true
self::assertCount(2, $query->expireResultCache(true)->getResult());
$driverConnectionMock->setStatementMock(new StatementArrayMock([['id_0' => 1]]));
// Retrieves cached data since expire flag is false and we have a cached result set
self::assertCount(2, $query->expireResultCache(false)->getResult());
}
}