1
0
mirror of synced 2025-01-30 12:01:44 +03:00

[DDC-1766] Rewrite getHydrationCacheId() to use existing processParameterValue() method. Other code style changes.

This commit is contained in:
Benjamin Eberlei 2012-04-05 22:40:40 +02:00
parent 1095fb39cb
commit 0b3577f2d2
3 changed files with 52 additions and 25 deletions

View File

@ -20,8 +20,8 @@
namespace Doctrine\ORM; namespace Doctrine\ORM;
use Doctrine\DBAL\Types\Type, use Doctrine\DBAL\Types\Type,
Doctrine\ORM\Query\QueryException,
Doctrine\DBAL\Cache\QueryCacheProfile, Doctrine\DBAL\Cache\QueryCacheProfile,
Doctrine\ORM\Query\QueryException,
Doctrine\ORM\Internal\Hydration\CacheHydrator; Doctrine\ORM\Internal\Hydration\CacheHydrator;
/** /**
@ -30,7 +30,6 @@ use Doctrine\DBAL\Types\Type,
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org * @link www.doctrine-project.org
* @since 2.0 * @since 2.0
* @version $Revision$
* @author Benjamin Eberlei <kontakt@beberlei.de> * @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
@ -329,7 +328,7 @@ abstract class AbstractQuery
public function setHydrationCacheProfile(QueryCacheProfile $profile = null) public function setHydrationCacheProfile(QueryCacheProfile $profile = null)
{ {
if ( ! $profile->getResultCacheDriver()) { if ( ! $profile->getResultCacheDriver()) {
$resultCacheDriver = $this->_em->getConfiguration()->getResultCacheImpl(); $resultCacheDriver = $this->_em->getConfiguration()->getHydrationCacheImpl();
$profile = $profile->setResultCacheDriver($resultCacheDriver); $profile = $profile->setResultCacheDriver($resultCacheDriver);
} }
@ -717,8 +716,8 @@ abstract class AbstractQuery
if ($this->_hydrationCacheProfile !== null) { if ($this->_hydrationCacheProfile !== null) {
list($cacheKey, $realCacheKey) = $this->getHydrationCacheId(); list($cacheKey, $realCacheKey) = $this->getHydrationCacheId();
$qcp = $this->getHydrationCacheProfile(); $queryCacheProfile = $this->getHydrationCacheProfile();
$cache = $qcp->getResultCacheDriver(); $cache = $queryCacheProfile->getResultCacheDriver();
$result = $cache->fetch($cacheKey); $result = $cache->fetch($cacheKey);
if (isset($result[$realCacheKey])) { if (isset($result[$realCacheKey])) {
@ -729,9 +728,9 @@ abstract class AbstractQuery
$result = array(); $result = array();
} }
$setCacheEntry = function($data) use ($cache, $result, $cacheKey, $realCacheKey, $qcp) { $setCacheEntry = function($data) use ($cache, $result, $cacheKey, $realCacheKey, $queryCacheProfile) {
$result[$realCacheKey] = $data; $result[$realCacheKey] = $data;
$cache->save($cacheKey, $result, $qcp->getLifetime()); $cache->save($cacheKey, $result, $queryCacheProfile->getLifetime());
}; };
} }
@ -764,24 +763,16 @@ abstract class AbstractQuery
$params = $this->getParameters(); $params = $this->getParameters();
foreach ($params AS $key => $value) { foreach ($params AS $key => $value) {
if (is_object($value) && $this->_em->getMetadataFactory()->hasMetadataFor(get_class($value))) { $params[$key] = $this->processParameterValue($value);
if ($this->_em->getUnitOfWork()->getEntityState($value) == UnitOfWork::STATE_MANAGED) {
$idValues = $this->_em->getUnitOfWork()->getEntityIdentifier($value);
} else {
$class = $this->_em->getClassMetadata(get_class($value));
$idValues = $class->getIdentifierValues($value);
}
$params[$key] = $idValues;
}
} }
$sql = $this->getSQL(); $sql = $this->getSQL();
$queryCacheProfile = $this->getHydrationCacheProfile();
$hints = $this->getHints(); $hints = $this->getHints();
$hints['hydrationMode'] = $this->getHydrationMode(); $hints['hydrationMode'] = $this->getHydrationMode();
$qcp = $this->getHydrationCacheProfile();
ksort($hints); ksort($hints);
return $qcp->generateCacheKeys($sql, $params, $hints); return $queryCacheProfile->generateCacheKeys($sql, $params, $hints);
} }
/** /**

View File

@ -246,6 +246,28 @@ class Configuration extends \Doctrine\DBAL\Configuration
$this->_attributes['queryCacheImpl'] = $cacheImpl; $this->_attributes['queryCacheImpl'] = $cacheImpl;
} }
/**
* Gets the cache driver implementation that is used for the hydration cache (SQL cache).
*
* @return \Doctrine\Common\Cache\Cache
*/
public function getHydrationCacheImpl()
{
return isset($this->_attributes['hydrationCacheImpl'])
? $this->_attributes['hydrationCacheImpl']
: null;
}
/**
* Sets the cache driver implementation that is used for the hydration cache (SQL cache).
*
* @param \Doctrine\Common\Cache\Cache $cacheImpl
*/
public function setHydrationCacheImpl(Cache $cacheImpl)
{
$this->_attributes['hydrationCacheImpl'] = $cacheImpl;
}
/** /**
* Gets the cache driver implementation that is used for metadata caching. * Gets the cache driver implementation that is used for metadata caching.
* *

View File

@ -15,11 +15,6 @@ class HydrationCacheTest extends OrmFunctionalTestCase
{ {
$this->useModelSet('cms'); $this->useModelSet('cms');
parent::setUp(); parent::setUp();
}
public function testHydrationCache()
{
$cache = new ArrayCache();
$user = new CmsUser; $user = new CmsUser;
$user->name = "Benjamin"; $user->name = "Benjamin";
@ -29,8 +24,11 @@ class HydrationCacheTest extends OrmFunctionalTestCase
$this->_em->persist($user); $this->_em->persist($user);
$this->_em->flush(); $this->_em->flush();
$this->_em->clear(); $this->_em->clear();
}
public function testHydrationCache()
{
$cache = new ArrayCache();
$dql = "SELECT u FROM Doctrine\Tests\Models\Cms\CmsUser u"; $dql = "SELECT u FROM Doctrine\Tests\Models\Cms\CmsUser u";
$users = $this->_em->createQuery($dql) $users = $this->_em->createQuery($dql)
@ -66,7 +64,23 @@ class HydrationCacheTest extends OrmFunctionalTestCase
->setHydrationCacheProfile(new QueryCacheProfile(null, 'cachekey', $cache)) ->setHydrationCacheProfile(new QueryCacheProfile(null, 'cachekey', $cache))
->getArrayResult(); ->getArrayResult();
$this->assertEquals($c + 2, $this->getCurrentQueryCount(), "Hydration now cached"); $this->assertEquals($c + 2, $this->getCurrentQueryCount(), "Hydration now cached");
}
public function testHydrationParametersSerialization()
{
$cache = new ArrayCache();
$user = new CmsUser();
$user->id = 1;
$dql = "SELECT u FROM Doctrine\Tests\Models\Cms\CmsUser u WHERE u.id = ?1";
$query = $this->_em->createQuery($dql)
->setParameter(1, $user)
->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache));
$query->getResult();
$c = $this->getCurrentQueryCount();
$query->getResult();
$this->assertEquals($c, $this->getCurrentQueryCount(), "Should not execute query. Its cached!");
} }
} }