From e73bd9e9bbd3e93e19fc0932036882f6d5d0e4f9 Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Sat, 17 Jan 2015 18:35:28 +0100 Subject: [PATCH] New buildCacheEntry way bo build a entry for a cached collection --- .../ORM/Cache/DefaultCollectionHydrator.php | 28 ++--- .../ORM/Cache/MultiGetCollectionHydrator.php | 110 ------------------ lib/Doctrine/ORM/Cache/MultiGetRegion.php | 6 +- .../AbstractCollectionPersister.php | 3 +- .../Cache/Region/DefaultMultiGetRegion.php | 7 +- .../Tests/ORM/Cache/MultiGetRegionTest.php | 6 +- 6 files changed, 18 insertions(+), 142 deletions(-) delete mode 100644 lib/Doctrine/ORM/Cache/MultiGetCollectionHydrator.php diff --git a/lib/Doctrine/ORM/Cache/DefaultCollectionHydrator.php b/lib/Doctrine/ORM/Cache/DefaultCollectionHydrator.php index 9440f5c95..716bd846f 100644 --- a/lib/Doctrine/ORM/Cache/DefaultCollectionHydrator.php +++ b/lib/Doctrine/ORM/Cache/DefaultCollectionHydrator.php @@ -65,9 +65,8 @@ class DefaultCollectionHydrator implements CollectionHydrator $data = array(); foreach ($collection as $index => $entity) { - $data[$index] = $this->uow->getEntityIdentifier($entity); + $data[$index] = new EntityCacheKey($metadata->name, $this->uow->getEntityIdentifier($entity)); } - return new CollectionCacheEntry($data); } @@ -81,36 +80,25 @@ class DefaultCollectionHydrator implements CollectionHydrator $targetRegion = $targetPersister->getCacheRegion(); $list = array(); - $keys = array(); - foreach ($entry->identifiers as $index => $identifier) { - $keys[$index] = new EntityCacheKey($assoc['targetEntity'], $identifier); - } - - if ($targetRegion instanceof MultiGetRegion) { - $entityEntries = $targetRegion->getMulti($keys); + $entityEntries = $targetRegion->getMulti($entry); if ($entityEntries === null) { return null; } - - foreach ($entityEntries as $index => $entityEntry) { - $list[$index] = $this->uow->createEntity($entityEntry->class, $entityEntry->data, self::$hints); - } - } else { + $entityEntries = array(); foreach ($entry->identifiers as $index => $identifier) { - - $entityEntry = $targetRegion->get(new EntityCacheKey($assoc['targetEntity'], $identifier)); - - if ($entityEntry === null) { + if (null === ($entityEntries[$index] = $targetRegion->get($identifier))) { return null; } - - $list[$index] = $this->uow->createEntity($entityEntry->class, $entityEntry->resolveAssociationEntries($this->em), self::$hints); } } + foreach ($entityEntries as $index => $entityEntry) { + $list[$index] = $this->uow->createEntity($entityEntry->class, $entityEntry->data, self::$hints); + } + array_walk($list, function($entity, $index) use ($collection) { $collection->hydrateSet($index, $entity); }); diff --git a/lib/Doctrine/ORM/Cache/MultiGetCollectionHydrator.php b/lib/Doctrine/ORM/Cache/MultiGetCollectionHydrator.php deleted file mode 100644 index 8770a48e2..000000000 --- a/lib/Doctrine/ORM/Cache/MultiGetCollectionHydrator.php +++ /dev/null @@ -1,110 +0,0 @@ -. - */ - -namespace Doctrine\ORM\Cache; - -use Doctrine\ORM\Query; -use Doctrine\ORM\PersistentCollection; -use Doctrine\ORM\Mapping\ClassMetadata; -use Doctrine\ORM\EntityManagerInterface; - -/** - * Collection hydrator that expects a target region to be instance of {Doctrine\ORM\Cache\MultiGetRegion}, - * to enable loading the entire collection with one cache request. - * - * @since 2.5 - * @author Asmir Mustafic - */ -class MultiGetCollectionHydrator implements CollectionHydrator -{ - /** - * @var \Doctrine\ORM\EntityManagerInterface - */ - private $em; - - /** - * @var \Doctrine\ORM\UnitOfWork - */ - private $uow; - - /** - * @var MultiGetRegion - */ - private $targetRegion; - - /** - * @var array - */ - private static $hints = array(Query::HINT_CACHE_ENABLED => true); - - /** - * @param \Doctrine\ORM\EntityManagerInterface $em The entity manager. - */ - public function __construct(EntityManagerInterface $em, MultiGetRegion $targetRegion) - { - $this->em = $em; - $this->uow = $em->getUnitOfWork(); - $this->targetRegion = $targetRegion; - } - - /** - * {@inheritdoc} - */ - public function buildCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, $collection) - { - $data = array(); - - foreach ($collection as $index => $entity) { - $data[$index] = $this->uow->getEntityIdentifier($entity); - } - - return new CollectionCacheEntry($data); - } - - /** - * {@inheritdoc} - */ - public function loadCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, CollectionCacheEntry $entry, PersistentCollection $collection) - { - $assoc = $metadata->associationMappings[$key->association]; - $list = array(); - - $keys = array(); - foreach ($entry->identifiers as $index => $identifier) { - $keys[$index] = new EntityCacheKey($assoc['targetEntity'], $identifier); - } - - $entityEntries = $this->targetRegion->getMulti($keys); - - if ($entityEntries === null) { - return null; - } - - foreach ($entityEntries as $index => $entityEntry) { - $list[$index] = $this->uow->createEntity($entityEntry->class, $entityEntry->data, self::$hints); - } - - array_walk($list, function ($entity, $index) use ($collection) { - $collection->hydrateSet($index, $entity); - }); - - return $list; - } -} diff --git a/lib/Doctrine/ORM/Cache/MultiGetRegion.php b/lib/Doctrine/ORM/Cache/MultiGetRegion.php index bbec206aa..96ca5867b 100644 --- a/lib/Doctrine/ORM/Cache/MultiGetRegion.php +++ b/lib/Doctrine/ORM/Cache/MultiGetRegion.php @@ -20,8 +20,6 @@ namespace Doctrine\ORM\Cache; -use Doctrine\ORM\Cache\CacheKey; - /** * Defines a region that supports multi-get reading. * @@ -36,8 +34,8 @@ interface MultiGetRegion * Get all items from the cache indentifed by $keys. * It returns NULL if some elements can not be found. * - * @param CacheKey[] $key The keys of the items to be retrieved. + * @param CollectionCacheEntry[] $collection The collection of the items to be retrieved. * @return array The cached entries or NULL if one or more entries can not be found */ - public function getMulti(array $keys); + public function getMulti(CollectionCacheEntry $collection); } diff --git a/lib/Doctrine/ORM/Cache/Persister/Collection/AbstractCollectionPersister.php b/lib/Doctrine/ORM/Cache/Persister/Collection/AbstractCollectionPersister.php index 33d437253..f62156763 100644 --- a/lib/Doctrine/ORM/Cache/Persister/Collection/AbstractCollectionPersister.php +++ b/lib/Doctrine/ORM/Cache/Persister/Collection/AbstractCollectionPersister.php @@ -167,8 +167,7 @@ abstract class AbstractCollectionPersister implements CachedCollectionPersister $targetHydrator = $targetPersister->getEntityHydrator(); $entry = $this->hydrator->buildCacheEntry($this->targetEntity, $key, $elements); - foreach ($entry->identifiers as $index => $identifier) { - $entityKey = new EntityCacheKey($this->targetEntity->rootEntityName, $identifier); + foreach ($entry->identifiers as $index => $entityKey) { if ($targetRegion->contains($entityKey)) { continue; diff --git a/lib/Doctrine/ORM/Cache/Region/DefaultMultiGetRegion.php b/lib/Doctrine/ORM/Cache/Region/DefaultMultiGetRegion.php index 876c6dce3..ef7b2b912 100644 --- a/lib/Doctrine/ORM/Cache/Region/DefaultMultiGetRegion.php +++ b/lib/Doctrine/ORM/Cache/Region/DefaultMultiGetRegion.php @@ -26,7 +26,7 @@ use Doctrine\ORM\Cache\CacheKey; use Doctrine\ORM\Cache\CacheEntry; use Doctrine\Common\Cache\CacheProvider; use Doctrine\ORM\Cache\MultiGetRegion; -use Doctrine\Common\Cache\CacheMultiGet; +use Doctrine\ORM\Cache\CollectionCacheEntry; /** * A cache region that enables the retrieval of multiple elements with one call @@ -39,10 +39,10 @@ class DefaultMultiGetRegion extends DefaultRegion implements MultiGetRegion /** * {@inheritdoc} */ - public function getMulti(array $keys) + public function getMulti(CollectionCacheEntry $collection) { $keysToRetrieve = array(); - foreach ($keys as $index => $key) { + foreach ($collection->identifiers as $index => $key) { $keysToRetrieve[$index] = $this->name . '_' . $key->hash; } @@ -55,7 +55,6 @@ class DefaultMultiGetRegion extends DefaultRegion implements MultiGetRegion foreach ($keysToRetrieve as $index => $key) { $returnableItems[$index] = $items[$key]; } - return $returnableItems; } } diff --git a/tests/Doctrine/Tests/ORM/Cache/MultiGetRegionTest.php b/tests/Doctrine/Tests/ORM/Cache/MultiGetRegionTest.php index fd0c0bda2..00ad09e02 100644 --- a/tests/Doctrine/Tests/ORM/Cache/MultiGetRegionTest.php +++ b/tests/Doctrine/Tests/ORM/Cache/MultiGetRegionTest.php @@ -6,6 +6,8 @@ use Doctrine\Common\Cache\ArrayCache; use Doctrine\Tests\Mocks\CacheEntryMock; use Doctrine\Tests\Mocks\CacheKeyMock; use Doctrine\ORM\Cache\Region\DefaultMultiGetRegion; +use Doctrine\ORM\Cache\CollectionCacheEntry; +use Doctrine\ORM\Cache\EntityCacheKey; /** * @author Asmir Mustafic @@ -21,7 +23,7 @@ class MultiGetRegionTest extends AbstractRegionTest { $key1 = new CacheKeyMock('key.1'); $value1 = new CacheEntryMock(array('id'=>1, 'name' => 'bar')); - + $key2 = new CacheKeyMock('key.2'); $value2 = new CacheEntryMock(array('id'=>2, 'name' => 'bar')); @@ -31,7 +33,7 @@ class MultiGetRegionTest extends AbstractRegionTest $this->region->put($key1, $value1); $this->region->put($key2, $value2); - $actual = $this->region->getMulti(array($key1, $key2)); + $actual = $this->region->getMulti(new CollectionCacheEntry(array($key1, $key2))); $this->assertEquals($value1, $actual[0]); $this->assertEquals($value2, $actual[1]);