From 1eb7f9295680dacf4760f569fe29553a4fc09f1e Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sat, 26 Feb 2011 00:38:06 +0100 Subject: [PATCH] DDC-1026 - Fix Result Cache Seperate chaining implementation that was wrong since DDC-892 was applied. --- lib/Doctrine/ORM/AbstractQuery.php | 12 ++++---- .../Tests/ORM/Functional/ResultCacheTest.php | 29 +++++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/lib/Doctrine/ORM/AbstractQuery.php b/lib/Doctrine/ORM/AbstractQuery.php index df7fb4e62..b1b24230b 100644 --- a/lib/Doctrine/ORM/AbstractQuery.php +++ b/lib/Doctrine/ORM/AbstractQuery.php @@ -506,10 +506,10 @@ abstract class AbstractQuery // Check result cache if ($this->_useResultCache && $cacheDriver = $this->getResultCacheDriver()) { - list($id, $hash) = $this->getResultCacheId(); - $cached = $this->_expireResultCache ? false : $cacheDriver->fetch($id); + list($key, $hash) = $this->getResultCacheId(); + $cached = $this->_expireResultCache ? false : $cacheDriver->fetch($hash); - if ($cached === false || !isset($cached[$id])) { + if ($cached === false || !isset($cached[$key])) { // Cache miss. $stmt = $this->_doExecute(); @@ -517,12 +517,12 @@ abstract class AbstractQuery $stmt, $this->_resultSetMapping, $this->_hints ); - $cacheDriver->save($id, $result, $this->_resultCacheTTL); + $cacheDriver->save($hash, array($key => $result), $this->_resultCacheTTL); return $result; } else { // Cache hit. - return $cached[$id]; + return $cached[$key]; } } @@ -556,7 +556,7 @@ abstract class AbstractQuery * Will return the configured id if it exists otherwise a hash will be * automatically generated for you. * - * @return array ($id, $hash) + * @return array ($key, $hash) */ protected function getResultCacheId() { diff --git a/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php b/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php index 68616f99b..b1b39a7be 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php @@ -86,6 +86,35 @@ class ResultCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->getConfiguration()->setResultCacheImpl(new ArrayCache()); } + /** + * @group DDC-1026 + */ + public function testUseResultCacheParams() + { + $cache = new \Doctrine\Common\Cache\ArrayCache(); + $this->_em->getConfiguration()->setResultCacheImpl($cache); + + $sqlCount = count($this->_sqlLoggerStack->queries); + $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux WHERE ux.id = ?1'); + $query->setParameter(1, 1); + $query->useResultCache(true); + $query->getResult(); + + $query->setParameter(1, 2); + $query->getResult(); + + $this->assertEquals($sqlCount + 2, count($this->_sqlLoggerStack->queries), "Two non-cached queries."); + + $query->setParameter(1, 1); + $query->useResultCache(true); + $query->getResult(); + + $query->setParameter(1, 2); + $query->getResult(); + + $this->assertEquals($sqlCount + 2, count($this->_sqlLoggerStack->queries), "The next two sql should have been cached, but were not."); + } + public function testNativeQueryResultCaching() { $rsm = new \Doctrine\ORM\Query\ResultSetMapping();