Commit da38026b authored by jwage's avatar jwage

[2.0][DDC-47] Added ability to set the result cache id used to store the cache entry

parent 93e6cabe
......@@ -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.
*
......
......@@ -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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment