Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

[DDC-1766] Initial implementation of hydration cache. #329

Merged
merged 9 commits into from about 2 years ago

4 participants

Benjamin Eberlei Guilherme Blanco Christophe Coevoet Marco Pivetta
Benjamin Eberlei
Owner

No description provided.

Guilherme Blanco guilhermeblanco commented on the diff April 04, 2012
lib/Doctrine/ORM/AbstractQuery.php
@@ -21,7 +21,8 @@
21 21
 
22 22
 use Doctrine\DBAL\Types\Type,
2
Guilherme Blanco Owner

Is it possible for us to split the "use"s in logical blocks? Like Doctrine\DBAL and Doctrine\ORM ones?

Marco Pivetta Collaborator
Ocramius added a note April 05, 2012

Also (not very important right now) PSR-1 says one use statement per row.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/AbstractQuery.php
((15 lines not shown))
  320
+     * @example
  321
+     * $lifetime = 100;
  322
+     * $resultKey = "abc";
  323
+     * $query->setHydrationCacheProfile(new QueryCacheProfile());
  324
+     * $query->setHydrationCacheProfile(new QueryCacheProfile($lifetime, $resultKey));
  325
+     *
  326
+     * @param \Doctrine\DBAL\Cache\QueryCacheProfile $profile
  327
+     * @return \Doctrine\ORM\AbstractQuery
  328
+     */
  329
+    public function setHydrationCacheProfile(QueryCacheProfile $profile = null)
  330
+    {
  331
+        if ( ! $profile->getResultCacheDriver()) {
  332
+            $profile = $profile->setResultCacheDriver($this->_em->getConfiguration()->getResultCacheImpl());
  333
+        }
  334
+
  335
+        $this->_hydrationCacheProfile = $profile;
1
Guilherme Blanco Owner

Add a line space between assignments and control structures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/AbstractQuery.php
((12 lines not shown))
  317
+     * some form of caching with UnitOfWork registration you should use
  318
+     * {@see AbstractQuery::setResultCacheProfile()}.
  319
+     *
  320
+     * @example
  321
+     * $lifetime = 100;
  322
+     * $resultKey = "abc";
  323
+     * $query->setHydrationCacheProfile(new QueryCacheProfile());
  324
+     * $query->setHydrationCacheProfile(new QueryCacheProfile($lifetime, $resultKey));
  325
+     *
  326
+     * @param \Doctrine\DBAL\Cache\QueryCacheProfile $profile
  327
+     * @return \Doctrine\ORM\AbstractQuery
  328
+     */
  329
+    public function setHydrationCacheProfile(QueryCacheProfile $profile = null)
  330
+    {
  331
+        if ( ! $profile->getResultCacheDriver()) {
  332
+            $profile = $profile->setResultCacheDriver($this->_em->getConfiguration()->getResultCacheImpl());
1
Guilherme Blanco Owner

Split the operation in 2 lines:

$resultCache = $this->_em->getConfiguration()->getResultCacheImpl();
$profile     = $profile->setResultCacheDriver($resultCache);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/AbstractQuery.php
((42 lines not shown))
  347
+    /**
  348
+     * Set a cache profile for the result cache.
  349
+     *
  350
+     * If no result cache driver is set in the QueryCacheProfile, the default
  351
+     * result cache driver is used from the configuration.
  352
+     *
  353
+     * @param \Doctrine\DBAL\Cache\QueryCacheProfile $profile
  354
+     * @return \Doctrine\ORM\AbstractQuery
  355
+     */
  356
+    public function setResultCacheProfile(QueryCacheProfile $profile = null)
  357
+    {
  358
+        if ( ! $profile->getResultCacheDriver()) {
  359
+            $profile = $profile->setResultCacheDriver($this->_em->getConfiguration()->getResultCacheImpl());
  360
+        }
  361
+
  362
+        $this->_queryCacheProfile = $profile;
1
Guilherme Blanco Owner

Line space between assignments and control structures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/AbstractQuery.php
((39 lines not shown))
  344
+        return $this->_hydrationCacheProfile;
  345
+    }
  346
+
  347
+    /**
  348
+     * Set a cache profile for the result cache.
  349
+     *
  350
+     * If no result cache driver is set in the QueryCacheProfile, the default
  351
+     * result cache driver is used from the configuration.
  352
+     *
  353
+     * @param \Doctrine\DBAL\Cache\QueryCacheProfile $profile
  354
+     * @return \Doctrine\ORM\AbstractQuery
  355
+     */
  356
+    public function setResultCacheProfile(QueryCacheProfile $profile = null)
  357
+    {
  358
+        if ( ! $profile->getResultCacheDriver()) {
  359
+            $profile = $profile->setResultCacheDriver($this->_em->getConfiguration()->getResultCacheImpl());
1
Guilherme Blanco Owner

Split the operation in 2 lines:

$resultCache = $this->_em->getConfiguration()->getResultCacheImpl();
$profile     = $profile->setResultCacheDriver($resultCache);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Guilherme Blanco guilhermeblanco commented on the diff April 04, 2012
lib/Doctrine/ORM/AbstractQuery.php
@@ -644,15 +708,73 @@ public function execute($params = array(), $hydrationMode = null)
644 708
             $this->setParameters($params);
645 709
         }
646 710
 
  711
+        $setCacheEntry = function() {};
2
Guilherme Blanco Owner

Line space between assignment and control structures.

Guilherme Blanco Owner

WTH is that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/AbstractQuery.php
@@ -644,15 +708,73 @@ public function execute($params = array(), $hydrationMode = null)
644 708
             $this->setParameters($params);
645 709
         }
646 710
 
  711
+        $setCacheEntry = function() {};
  712
+        if ($this->_hydrationCacheProfile !== null) {
  713
+            list($cacheKey, $realCacheKey) = $this->getHydrationCacheId();
  714
+
  715
+            $qcp = $this->getHydrationCacheProfile();
1
Guilherme Blanco Owner

Align the assignment blocks "=" signs for better code readability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/AbstractQuery.php
@@ -644,15 +708,73 @@ public function execute($params = array(), $hydrationMode = null)
644 708
             $this->setParameters($params);
645 709
         }
646 710
 
  711
+        $setCacheEntry = function() {};
  712
+        if ($this->_hydrationCacheProfile !== null) {
  713
+            list($cacheKey, $realCacheKey) = $this->getHydrationCacheId();
  714
+
  715
+            $qcp = $this->getHydrationCacheProfile();
  716
+            $cache = $qcp->getResultCacheDriver();
  717
+
  718
+            $result = $cache->fetch($cacheKey);
1
Guilherme Blanco Owner

This LOC is locally part of the previous block. Remove the line space previous to this line and add one after.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Guilherme Blanco guilhermeblanco commented on the diff April 04, 2012
lib/Doctrine/ORM/AbstractQuery.php
((26 lines not shown))
647 733
         $stmt = $this->_doExecute();
648 734
 
649 735
         if (is_numeric($stmt)) {
  736
+            $setCacheEntry($stmt);
1
Guilherme Blanco Owner

Line space.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Guilherme Blanco guilhermeblanco commented on the diff April 04, 2012
lib/Doctrine/ORM/AbstractQuery.php
((37 lines not shown))
  743
+
  744
+        $setCacheEntry($data);
  745
+
  746
+        return $data;
  747
+    }
  748
+
  749
+    /**
  750
+     * Get the result cache id to use to store the result set cache entry.
  751
+     * Will return the configured id if it exists otherwise a hash will be
  752
+     * automatically generated for you.
  753
+     *
  754
+     * @return array ($key, $hash)
  755
+     */
  756
+    protected function getHydrationCacheId()
  757
+    {
  758
+        $params = $this->getParameters();
1
Guilherme Blanco Owner

Line space between assignment and control structure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/AbstractQuery.php
((39 lines not shown))
  745
+
  746
+        return $data;
  747
+    }
  748
+
  749
+    /**
  750
+     * Get the result cache id to use to store the result set cache entry.
  751
+     * Will return the configured id if it exists otherwise a hash will be
  752
+     * automatically generated for you.
  753
+     *
  754
+     * @return array ($key, $hash)
  755
+     */
  756
+    protected function getHydrationCacheId()
  757
+    {
  758
+        $params = $this->getParameters();
  759
+        foreach ($params AS $key => $value) {
  760
+            if (is_object($value) && $this->_em->getMetadataFactory()->hasMetadataFor(get_class($value))) {
1
Guilherme Blanco Owner

Early return can be applied here to reduce one indentation level.

foreach (...) {
    if ( ! (is_object($value) && $this->_em->getMetadataFactory()->hasMetadataFor(get_class($value)))) {
        continue;
    }

    // ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Guilherme Blanco guilhermeblanco commented on the diff April 04, 2012
lib/Doctrine/ORM/AbstractQuery.php
((37 lines not shown))
654 747
             $stmt, $this->_resultSetMapping, $this->_hints
655 748
         );
  749
+
  750
+        $setCacheEntry($data);
  751
+
  752
+        return $data;
  753
+    }
  754
+
  755
+    /**
  756
+     * Get the result cache id to use to store the result set cache entry.
  757
+     * Will return the configured id if it exists otherwise a hash will be
  758
+     * automatically generated for you.
  759
+     *
  760
+     * @return array ($key, $hash)
  761
+     */
1
Guilherme Blanco Owner

I'd rather encapsulate this block in a separate function.

protected function getEntityIdentifierValueList($entity)
{
    $unitOfWork = $this->_em->getUnitOfWork();

    if ($unitOfWork->getEntityState($entity) === UnitOfWork::STATE_MANAGED) {
        return $unitOfWork->getEntityIdentifier($entity);
    }

    $classMetadata = $this->_em->getClassMetadata(get_class($entity));

    return $classMetadata->getIdentifierValues($entity);
}

Then just reuse it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/AbstractQuery.php
((46 lines not shown))
  756
+     * Get the result cache id to use to store the result set cache entry.
  757
+     * Will return the configured id if it exists otherwise a hash will be
  758
+     * automatically generated for you.
  759
+     *
  760
+     * @return array ($key, $hash)
  761
+     */
  762
+    protected function getHydrationCacheId()
  763
+    {
  764
+        $params = $this->getParameters();
  765
+
  766
+        foreach ($params AS $key => $value) {
  767
+            if (is_object($value) && $this->_em->getMetadataFactory()->hasMetadataFor(get_class($value))) {
  768
+                if ($this->_em->getUnitOfWork()->getEntityState($value) == UnitOfWork::STATE_MANAGED) {
  769
+                    $idValues = $this->_em->getUnitOfWork()->getEntityIdentifier($value);
  770
+                } else {
  771
+                    $class    = $this->_em->getClassMetadata(get_class($value));
1
Guilherme Blanco Owner

Align "=" signs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/AbstractQuery.php
((49 lines not shown))
  759
+     *
  760
+     * @return array ($key, $hash)
  761
+     */
  762
+    protected function getHydrationCacheId()
  763
+    {
  764
+        $params = $this->getParameters();
  765
+
  766
+        foreach ($params AS $key => $value) {
  767
+            if (is_object($value) && $this->_em->getMetadataFactory()->hasMetadataFor(get_class($value))) {
  768
+                if ($this->_em->getUnitOfWork()->getEntityState($value) == UnitOfWork::STATE_MANAGED) {
  769
+                    $idValues = $this->_em->getUnitOfWork()->getEntityIdentifier($value);
  770
+                } else {
  771
+                    $class    = $this->_em->getClassMetadata(get_class($value));
  772
+                    $idValues = $class->getIdentifierValues($value);
  773
+                }
  774
+                $params[$key] = $idValues;
1
Guilherme Blanco Owner

Line space before

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/AbstractQuery.php
((51 lines not shown))
  761
+     */
  762
+    protected function getHydrationCacheId()
  763
+    {
  764
+        $params = $this->getParameters();
  765
+
  766
+        foreach ($params AS $key => $value) {
  767
+            if (is_object($value) && $this->_em->getMetadataFactory()->hasMetadataFor(get_class($value))) {
  768
+                if ($this->_em->getUnitOfWork()->getEntityState($value) == UnitOfWork::STATE_MANAGED) {
  769
+                    $idValues = $this->_em->getUnitOfWork()->getEntityIdentifier($value);
  770
+                } else {
  771
+                    $class    = $this->_em->getClassMetadata(get_class($value));
  772
+                    $idValues = $class->getIdentifierValues($value);
  773
+                }
  774
+                $params[$key] = $idValues;
  775
+            }
  776
+        }
2
Guilherme Blanco Owner

Do not abbreviate. What's qcp? Quebec Cops Party?

Marco Pivetta Collaborator
Ocramius added a note April 05, 2012

I lol'd hard X°D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
lib/Doctrine/ORM/AbstractQuery.php
((12 lines not shown))
  317
+     * some form of caching with UnitOfWork registration you should use
  318
+     * {@see AbstractQuery::setResultCacheProfile()}.
  319
+     *
  320
+     * @example
  321
+     * $lifetime = 100;
  322
+     * $resultKey = "abc";
  323
+     * $query->setHydrationCacheProfile(new QueryCacheProfile());
  324
+     * $query->setHydrationCacheProfile(new QueryCacheProfile($lifetime, $resultKey));
  325
+     *
  326
+     * @param \Doctrine\DBAL\Cache\QueryCacheProfile $profile
  327
+     * @return \Doctrine\ORM\AbstractQuery
  328
+     */
  329
+    public function setHydrationCacheProfile(QueryCacheProfile $profile = null)
  330
+    {
  331
+        if ( ! $profile->getResultCacheDriver()) {
  332
+            $resultCacheDriver = $this->_em->getConfiguration()->getResultCacheImpl();
2
Christophe Coevoet
stof added a note April 04, 2012

is it expected to use the ResultCache implementation for the hydration cache too ? Currently the place using caching in the ORM are using separate cache implementations

Marco Pivetta Collaborator
Ocramius added a note April 05, 2012

Indeed, a $hydrationCacheDriver could be a nice addition to Doctrine\ORM\Configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Marco Pivetta Ocramius commented on the diff April 05, 2012
lib/Doctrine/ORM/AbstractQuery.php
@@ -102,6 +103,11 @@
102 103
     protected $_expireResultCache = false;
103 104
 
104 105
     /**
  106
+     * @param \Doctrine\DBAL\Cache\QueryCacheProfile
  107
+     */
  108
+    protected $_hydrationCacheProfile;
1
Marco Pivetta Collaborator
Ocramius added a note April 05, 2012

Just wondering about the _. When will this be dropped?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Benjamin Eberlei
Owner

@guilhermeblanco please see last commit 0b3577f and merge if everything ok now. From me this is finished, and then i rebase this into 2.2

Guilherme Blanco

@beberlei seems perfect! Feel free to merge! =D

Guilherme Blanco guilhermeblanco merged commit a5c13a5 into from April 06, 2012
Guilherme Blanco guilhermeblanco closed this April 06, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
126  lib/Doctrine/ORM/AbstractQuery.php
@@ -20,8 +20,9 @@
20 20
 namespace Doctrine\ORM;
21 21
 
22 22
 use Doctrine\DBAL\Types\Type,
  23
+    Doctrine\DBAL\Cache\QueryCacheProfile,
23 24
     Doctrine\ORM\Query\QueryException,
24  
-    Doctrine\DBAL\Cache\QueryCacheProfile;
  25
+    Doctrine\ORM\Internal\Hydration\CacheHydrator;
25 26
 
26 27
 /**
27 28
  * Base contract for ORM queries. Base class for Query and NativeQuery.
@@ -29,7 +30,6 @@
29 30
  * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
30 31
  * @link    www.doctrine-project.org
31 32
  * @since   2.0
32  
- * @version $Revision$
33 33
  * @author  Benjamin Eberlei <kontakt@beberlei.de>
34 34
  * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
35 35
  * @author  Jonathan Wage <jonwage@gmail.com>
@@ -102,6 +102,11 @@
102 102
     protected $_expireResultCache = false;
103 103
 
104 104
     /**
  105
+     * @param \Doctrine\DBAL\Cache\QueryCacheProfile
  106
+     */
  107
+    protected $_hydrationCacheProfile;
  108
+
  109
+    /**
105 110
      * Initializes a new instance of a class derived from <tt>AbstractQuery</tt>.
106 111
      *
107 112
      * @param \Doctrine\ORM\EntityManager $entityManager
@@ -300,6 +305,68 @@ public function setResultSetMapping(Query\ResultSetMapping $rsm)
300 305
     }
301 306
 
302 307
     /**
  308
+     * Set a cache profile for hydration caching.
  309
+     *
  310
+     * If no result cache driver is set in the QueryCacheProfile, the default
  311
+     * result cache driver is used from the configuration.
  312
+     *
  313
+     * Important: Hydration caching does NOT register entities in the
  314
+     * UnitOfWork when retrieved from the cache. Never use result cached
  315
+     * entities for requests that also flush the EntityManager. If you want
  316
+     * some form of caching with UnitOfWork registration you should use
  317
+     * {@see AbstractQuery::setResultCacheProfile()}.
  318
+     *
  319
+     * @example
  320
+     * $lifetime = 100;
  321
+     * $resultKey = "abc";
  322
+     * $query->setHydrationCacheProfile(new QueryCacheProfile());
  323
+     * $query->setHydrationCacheProfile(new QueryCacheProfile($lifetime, $resultKey));
  324
+     *
  325
+     * @param \Doctrine\DBAL\Cache\QueryCacheProfile $profile
  326
+     * @return \Doctrine\ORM\AbstractQuery
  327
+     */
  328
+    public function setHydrationCacheProfile(QueryCacheProfile $profile = null)
  329
+    {
  330
+        if ( ! $profile->getResultCacheDriver()) {
  331
+            $resultCacheDriver = $this->_em->getConfiguration()->getHydrationCacheImpl();
  332
+            $profile = $profile->setResultCacheDriver($resultCacheDriver);
  333
+        }
  334
+
  335
+        $this->_hydrationCacheProfile = $profile;
  336
+
  337
+        return $this;
  338
+    }
  339
+
  340
+    /**
  341
+     * @return \Doctrine\DBAL\Cache\QueryCacheProfile
  342
+     */
  343
+    public function getHydrationCacheProfile()
  344
+    {
  345
+        return $this->_hydrationCacheProfile;
  346
+    }
  347
+
  348
+    /**
  349
+     * Set a cache profile for the result cache.
  350
+     *
  351
+     * If no result cache driver is set in the QueryCacheProfile, the default
  352
+     * result cache driver is used from the configuration.
  353
+     *
  354
+     * @param \Doctrine\DBAL\Cache\QueryCacheProfile $profile
  355
+     * @return \Doctrine\ORM\AbstractQuery
  356
+     */
  357
+    public function setResultCacheProfile(QueryCacheProfile $profile = null)
  358
+    {
  359
+        if ( ! $profile->getResultCacheDriver()) {
  360
+            $resultCacheDriver = $this->_em->getConfiguration()->getResultCacheImpl();
  361
+            $profile = $profile->setResultCacheDriver($resultCacheDriver);
  362
+        }
  363
+
  364
+        $this->_queryCacheProfile = $profile;
  365
+
  366
+        return $this;
  367
+    }
  368
+
  369
+    /**
303 370
      * Defines a cache driver to be used for caching result sets and implictly enables caching.
304 371
      *
305 372
      * @param \Doctrine\Common\Cache\Cache $driver Cache driver
@@ -644,15 +711,68 @@ public function execute($params = array(), $hydrationMode = null)
644 711
             $this->setParameters($params);
645 712
         }
646 713
 
  714
+        $setCacheEntry = function() {};
  715
+
  716
+        if ($this->_hydrationCacheProfile !== null) {
  717
+            list($cacheKey, $realCacheKey) = $this->getHydrationCacheId();
  718
+
  719
+            $queryCacheProfile = $this->getHydrationCacheProfile();
  720
+            $cache             = $queryCacheProfile->getResultCacheDriver();
  721
+            $result            = $cache->fetch($cacheKey);
  722
+
  723
+            if (isset($result[$realCacheKey])) {
  724
+                return $result[$realCacheKey];
  725
+            }
  726
+
  727
+            if ( ! $result) {
  728
+                $result = array();
  729
+            }
  730
+
  731
+            $setCacheEntry = function($data) use ($cache, $result, $cacheKey, $realCacheKey, $queryCacheProfile) {
  732
+                $result[$realCacheKey] = $data;
  733
+                $cache->save($cacheKey, $result, $queryCacheProfile->getLifetime());
  734
+            };
  735
+        }
  736
+
647 737
         $stmt = $this->_doExecute();
648 738
 
649 739
         if (is_numeric($stmt)) {
  740
+            $setCacheEntry($stmt);
  741
+
650 742
             return $stmt;
651 743
         }
652 744
 
653  
-        return $this->_em->getHydrator($this->_hydrationMode)->hydrateAll(
  745
+        $data = $this->_em->getHydrator($this->_hydrationMode)->hydrateAll(
654 746
             $stmt, $this->_resultSetMapping, $this->_hints
655 747
         );
  748
+
  749
+        $setCacheEntry($data);
  750
+
  751
+        return $data;
  752
+    }
  753
+
  754
+    /**
  755
+     * Get the result cache id to use to store the result set cache entry.
  756
+     * Will return the configured id if it exists otherwise a hash will be
  757
+     * automatically generated for you.
  758
+     *
  759
+     * @return array ($key, $hash)
  760
+     */
  761
+    protected function getHydrationCacheId()
  762
+    {
  763
+        $params = $this->getParameters();
  764
+
  765
+        foreach ($params AS $key => $value) {
  766
+            $params[$key] = $this->processParameterValue($value);
  767
+        }
  768
+
  769
+        $sql                    = $this->getSQL();
  770
+        $queryCacheProfile      = $this->getHydrationCacheProfile();
  771
+        $hints                  = $this->getHints();
  772
+        $hints['hydrationMode'] = $this->getHydrationMode();
  773
+        ksort($hints);
  774
+
  775
+        return $queryCacheProfile->generateCacheKeys($sql, $params, $hints);
656 776
     }
657 777
 
658 778
     /**
22  lib/Doctrine/ORM/Configuration.php
@@ -247,6 +247,28 @@ public function setQueryCacheImpl(Cache $cacheImpl)
247 247
     }
248 248
 
249 249
     /**
  250
+     * Gets the cache driver implementation that is used for the hydration cache (SQL cache).
  251
+     *
  252
+     * @return \Doctrine\Common\Cache\Cache
  253
+     */
  254
+    public function getHydrationCacheImpl()
  255
+    {
  256
+        return isset($this->_attributes['hydrationCacheImpl'])
  257
+            ? $this->_attributes['hydrationCacheImpl']
  258
+            : null;
  259
+    }
  260
+
  261
+    /**
  262
+     * Sets the cache driver implementation that is used for the hydration cache (SQL cache).
  263
+     *
  264
+     * @param \Doctrine\Common\Cache\Cache $cacheImpl
  265
+     */
  266
+    public function setHydrationCacheImpl(Cache $cacheImpl)
  267
+    {
  268
+        $this->_attributes['hydrationCacheImpl'] = $cacheImpl;
  269
+    }
  270
+
  271
+    /**
250 272
      * Gets the cache driver implementation that is used for metadata caching.
251 273
      *
252 274
      * @return \Doctrine\Common\Cache\Cache
86  tests/Doctrine/Tests/ORM/Functional/HydrationCacheTest.php
... ...
@@ -0,0 +1,86 @@
  1
+<?php
  2
+namespace Doctrine\Tests\ORM\Functional;
  3
+
  4
+use Doctrine\Tests\OrmFunctionalTestCase;
  5
+use Doctrine\Tests\Models\Cms\CmsUser;
  6
+use Doctrine\DBAL\Cache\QueryCacheProfile;
  7
+use Doctrine\Common\Cache\ArrayCache;
  8
+
  9
+/**
  10
+ * @group DDC-1766
  11
+ */
  12
+class HydrationCacheTest extends OrmFunctionalTestCase
  13
+{
  14
+    public function setUp()
  15
+    {
  16
+        $this->useModelSet('cms');
  17
+        parent::setUp();
  18
+
  19
+        $user = new CmsUser;
  20
+        $user->name = "Benjamin";
  21
+        $user->username = "beberlei";
  22
+        $user->status = 'active';
  23
+
  24
+        $this->_em->persist($user);
  25
+        $this->_em->flush();
  26
+        $this->_em->clear();
  27
+    }
  28
+
  29
+    public function testHydrationCache()
  30
+    {
  31
+        $cache = new ArrayCache();
  32
+        $dql = "SELECT u FROM Doctrine\Tests\Models\Cms\CmsUser u";
  33
+
  34
+        $users = $this->_em->createQuery($dql)
  35
+                      ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache))
  36
+                      ->getResult();
  37
+
  38
+        $c = $this->getCurrentQueryCount();
  39
+        $users = $this->_em->createQuery($dql)
  40
+                      ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache))
  41
+                      ->getResult();
  42
+
  43
+        $this->assertEquals($c, $this->getCurrentQueryCount(), "Should not execute query. Its cached!");
  44
+
  45
+        $users = $this->_em->createQuery($dql)
  46
+                      ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache))
  47
+                      ->getArrayResult();
  48
+
  49
+        $this->assertEquals($c + 1, $this->getCurrentQueryCount(), "Hydration is part of cache key.");
  50
+
  51
+        $users = $this->_em->createQuery($dql)
  52
+                      ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache))
  53
+                      ->getArrayResult();
  54
+
  55
+        $this->assertEquals($c + 1, $this->getCurrentQueryCount(), "Hydration now cached");
  56
+
  57
+        $users = $this->_em->createQuery($dql)
  58
+                      ->setHydrationCacheProfile(new QueryCacheProfile(null, 'cachekey', $cache))
  59
+                      ->getArrayResult();
  60
+
  61
+        $this->assertTrue($cache->contains('cachekey'), 'Explicit cache key');
  62
+
  63
+        $users = $this->_em->createQuery($dql)
  64
+                      ->setHydrationCacheProfile(new QueryCacheProfile(null, 'cachekey', $cache))
  65
+                      ->getArrayResult();
  66
+        $this->assertEquals($c + 2, $this->getCurrentQueryCount(), "Hydration now cached");
  67
+    }
  68
+
  69
+    public function testHydrationParametersSerialization()
  70
+    {
  71
+        $cache = new ArrayCache();
  72
+        $user = new CmsUser();
  73
+        $user->id = 1;
  74
+
  75
+        $dql = "SELECT u FROM Doctrine\Tests\Models\Cms\CmsUser u WHERE u.id = ?1";
  76
+        $query = $this->_em->createQuery($dql)
  77
+                      ->setParameter(1, $user)
  78
+                      ->setHydrationCacheProfile(new QueryCacheProfile(null, null, $cache));
  79
+
  80
+        $query->getResult();
  81
+        $c = $this->getCurrentQueryCount();
  82
+        $query->getResult();
  83
+        $this->assertEquals($c, $this->getCurrentQueryCount(), "Should not execute query. Its cached!");
  84
+    }
  85
+}
  86
+
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.