1
0
mirror of synced 2025-01-18 22:41:43 +03:00

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

This commit is contained in:
jwage 2009-10-22 22:39:37 +00:00
parent 93e6cabe04
commit da38026bc2
2 changed files with 53 additions and 6 deletions

View File

@ -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.
*

View File

@ -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'));
}
}