Evict result set cache if Query#expireResultCache() was called
This commit is contained in:
parent
85a52d781e
commit
e71272e2b4
@ -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
|
||||
*/
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user