diff --git a/lib/Doctrine/ORM/AbstractQuery.php b/lib/Doctrine/ORM/AbstractQuery.php index 769588fda..86ad2f1ed 100644 --- a/lib/Doctrine/ORM/AbstractQuery.php +++ b/lib/Doctrine/ORM/AbstractQuery.php @@ -94,6 +94,13 @@ abstract class AbstractQuery */ protected $_resultCache; + /** + * The id to store the result cache entry under. + * + * @var string + */ + protected $_resultCacheId; + /** * @var boolean Boolean value that indicates whether or not expire the result cache. */ @@ -437,9 +444,8 @@ abstract class AbstractQuery // Check result cache if ($cacheDriver = $this->getResultCacheDriver()) { - // Calculate hash for DQL query. - $hash = md5($this->getDql() . var_export($params, true)); - $cached = ($this->_expireResultCache) ? false : $cacheDriver->fetch($hash); + $id = $this->_getResultCacheId($params); + $cached = ($this->_expireResultCache) ? false : $cacheDriver->fetch($id); if ($cached === false) { // Cache miss. @@ -449,7 +455,7 @@ abstract class AbstractQuery $stmt, $this->_resultSetMapping, $this->_hints ); - $cacheDriver->save($hash, $result, $this->_resultCacheTTL); + $cacheDriver->save($id, $result, $this->_resultCacheTTL); return $result; } else { @@ -469,6 +475,36 @@ abstract class AbstractQuery ); } + /** + * Set the result cache id to use to store the result set cache entry. + * If this is not explicitely set by the developer then a hash is automatically + * generated for you. + * + * @param string $id + * @return void + */ + public function setResultCacheId($id) + { + $this->_resultCacheId = $id; + } + + /** + * Get the result cache id to use to store the result set cache entry. + * Will return the configured id if it exists otherwise a hash will be + * automatically generated for you. + * + * @param array $params + * @return string $id + */ + protected function _getResultCacheId(array $params) + { + if ($this->_resultCacheId) { + return $this->_resultCacheId; + } else { + return md5($this->getDql() . var_export($params, true)); + } + } + /** * Prepares the given parameters for execution in an SQL statement. * diff --git a/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php b/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php index 00d34f4ab..519dc1dd9 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ResultCacheTest.php @@ -19,7 +19,7 @@ class ResultCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase parent::setUp(); } - public function testQueryCache() + public function testResultCache() { $user = new CmsUser; $user->name = 'Roman'; @@ -55,5 +55,16 @@ class ResultCacheTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals(1, count($users)); $this->assertEquals('Roman', $users[0]->name); } -} + public function testSetResultCacheId() + { + $cache = new ArrayCache; + + $query = $this->_em->createQuery('select ux from Doctrine\Tests\Models\CMS\CmsUser ux'); + $query->setResultCache($cache); + $query->setResultCacheId('testing_result_cache_id'); + $users = $query->getResult(); + + $this->assertTrue($cache->contains('testing_result_cache_id')); + } +} \ No newline at end of file