Skip to content

Commit

Permalink
Merge pull request #6245 from lcobucci/l2c-use-getMultiple
Browse files Browse the repository at this point in the history
Use `getMultiple()` to fetch associations as well in L2 cache fetch operations
  • Loading branch information
Ocramius committed Jun 24, 2017
2 parents 5d98247 + ccaa4b8 commit 388afb4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
19 changes: 11 additions & 8 deletions lib/Doctrine/ORM/Cache/DefaultQueryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,9 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []

// @TODO - move to cache hydration component
foreach ($entry->result as $index => $entry) {

$entityEntry = is_array($entries) && array_key_exists($index, $entries) ? $entries[$index] : null;

if ($entityEntry === null) {

if ($this->cacheLogger !== null) {
$this->cacheLogger->entityCacheMiss($regionName, $cacheKeys->identifiers[$index]);
}
Expand All @@ -139,7 +137,6 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []
}

if ( ! $hasRelation) {

$result[$index] = $this->uow->createEntity($entityEntry->class, $entityEntry->resolveAssociationEntries($this->em), self::$hints);

continue;
Expand Down Expand Up @@ -178,14 +175,20 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []
continue;
}

$collection = new PersistentCollection($this->em, $assocMetadata, new ArrayCollection());
$generateKeys = function ($id) use ($assocMetadata): EntityCacheKey {
return new EntityCacheKey($assocMetadata->rootEntityName, $id);
};

foreach ($assoc['list'] as $assocIndex => $assocId) {
$collection = new PersistentCollection($this->em, $assocMetadata, new ArrayCollection());
$assocKeys = new CollectionCacheEntry(array_map($generateKeys, $assoc['list']));
$assocEntries = $assocRegion->getMultiple($assocKeys);

if (($assocEntry = $assocRegion->get($assocKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocId))) === null) {
foreach ($assoc['list'] as $assocIndex => $assocId) {
$assocEntry = is_array($assocEntries) && array_key_exists($assocIndex, $assocEntries) ? $assocEntries[$assocIndex] : null;

if ($assocEntry === null) {
if ($this->cacheLogger !== null) {
$this->cacheLogger->entityCacheMiss($assocRegion->getName(), $assocKey);
$this->cacheLogger->entityCacheMiss($assocRegion->getName(), $assocKeys->identifiers[$assocIndex]);
}

$this->uow->hydrationComplete();
Expand All @@ -198,7 +201,7 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []
$collection->hydrateSet($assocIndex, $element);

if ($this->cacheLogger !== null) {
$this->cacheLogger->entityCacheHit($assocRegion->getName(), $assocKey);
$this->cacheLogger->entityCacheHit($assocRegion->getName(), $assocKeys->identifiers[$assocIndex]);
}
}

Expand Down
46 changes: 42 additions & 4 deletions tests/Doctrine/Tests/ORM/Cache/DefaultQueryCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ public function testPutToOneAssociationQueryResult()
$stateClass = $this->em->getClassMetadata(State::class);

$rsm->addRootEntityFromClassMetadata(City::class, 'c');
$rsm->addJoinedEntityFromClassMetadata(State::class, 's', 'c', 'state', ['id'=>'state_id', 'name'=>'state_name']
);
$rsm->addJoinedEntityFromClassMetadata(State::class, 's', 'c', 'state', ['id'=>'state_id', 'name'=>'state_name']);

for ($i = 0; $i < 4; $i++) {
$state = new State("State $i");
Expand Down Expand Up @@ -283,8 +282,47 @@ public function testGetBasicQueryResult()
$key = new QueryCacheKey('query.key1', 0);
$entry = new QueryCacheEntry(
[
['identifier' => ['id' => 1]],
['identifier' => ['id' => 2]]
['identifier' => ['id' => 1]],
['identifier' => ['id' => 2]]
]
);

$data = [
['id'=>1, 'name' => 'Foo'],
['id'=>2, 'name' => 'Bar']
];

$this->region->addReturn('get', $entry);

$this->region->addReturn(
'getMultiple',
[
new EntityCacheEntry(Country::class, $data[0]),
new EntityCacheEntry(Country::class, $data[1])
]
);

$rsm->addRootEntityFromClassMetadata(Country::class, 'c');

$result = $this->queryCache->get($key, $rsm);

$this->assertCount(2, $result);
$this->assertInstanceOf(Country::class, $result[0]);
$this->assertInstanceOf(Country::class, $result[1]);
$this->assertEquals(1, $result[0]->getId());
$this->assertEquals(2, $result[1]->getId());
$this->assertEquals('Foo', $result[0]->getName());
$this->assertEquals('Bar', $result[1]->getName());
}

public function testGetWithAssociation()
{
$rsm = new ResultSetMappingBuilder($this->em);
$key = new QueryCacheKey('query.key1', 0);
$entry = new QueryCacheEntry(
[
['identifier' => ['id' => 1]],
['identifier' => ['id' => 2]]
]
);

Expand Down

0 comments on commit 388afb4

Please sign in to comment.