Skip to content

Commit

Permalink
Optimization. Multiple get in QueryCache
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Polyanin committed May 20, 2016
1 parent b3aa825 commit 06f8294
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
15 changes: 12 additions & 3 deletions lib/Doctrine/ORM/Cache/DefaultQueryCache.php
Expand Up @@ -112,20 +112,29 @@ public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = ar
$regionName = $region->getName();

$cm = $this->em->getClassMetadata($entityName);

$entityCacheKeys = [];
foreach ($entry->result as $index => $concreteEntry) {
$entityCacheKeys[$index] = new EntityCacheKey($cm->rootEntityName, $concreteEntry['identifier']);
}
$entries = $region->getMultiple(new CollectionCacheEntry($entityCacheKeys));

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

if (($entityEntry = $region->get($entityKey = new EntityCacheKey($cm->rootEntityName, $entry['identifier']))) === null) {
$entityEntry = is_array($entries) && array_key_exists($index, $entries) ? $entries[$index] : null;

if ($entityEntry === null) {

if ($this->cacheLogger !== null) {
$this->cacheLogger->entityCacheMiss($regionName, $entityKey);
$this->cacheLogger->entityCacheMiss($regionName, $entityCacheKeys[$index]);
}

return null;
}

if ($this->cacheLogger !== null) {
$this->cacheLogger->entityCacheHit($regionName, $entityKey);
$this->cacheLogger->entityCacheHit($regionName, $entityCacheKeys[$index]);
}

if ( ! $hasRelation) {
Expand Down
20 changes: 13 additions & 7 deletions tests/Doctrine/Tests/ORM/Cache/DefaultQueryCacheTest.php
Expand Up @@ -276,8 +276,10 @@ public function testGetBasicQueryResult()
);

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

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

Expand Down Expand Up @@ -434,8 +436,10 @@ public function testGetShouldIgnoreOldQueryCacheEntryResult()
$entry->time = time() - 100;

$this->region->addReturn('get', $entry);
$this->region->addReturn('get', new EntityCacheEntry(Country::CLASSNAME, $entities[0]));
$this->region->addReturn('get', new EntityCacheEntry(Country::CLASSNAME, $entities[1]));
$this->region->addReturn('getMultiple', array(
new EntityCacheEntry(Country::CLASSNAME, $entities[0]),
new EntityCacheEntry(Country::CLASSNAME, $entities[1])
));

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

Expand All @@ -457,8 +461,10 @@ public function testGetShouldIgnoreNonQueryCacheEntryResult()
);

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

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

Expand All @@ -475,7 +481,7 @@ public function testGetShouldIgnoreMissingEntityQueryCacheEntry()
));

$this->region->addReturn('get', $entry);
$this->region->addReturn('get', null);
$this->region->addReturn('getMultiple', [null]);

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

Expand Down

0 comments on commit 06f8294

Please sign in to comment.