Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use getMultiple() to fetch associations as well in L2 cache fetch operations #6245

Merged
merged 1 commit into from
Jun 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHP7 only feature!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

master already dropped PHP 5.6

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guilhermeblanco we dropped PHP 5 support in master as well

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