Skip to content

Commit

Permalink
Optimise storing metadata in cache
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Dec 12, 2020
1 parent 118d9a6 commit c2e4044
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
use Symfony\Component\Cache\DoctrineProvider;

use function array_combine;
use function array_keys;
use function array_map;
use function array_reverse;
use function array_unshift;
use function explode;
Expand Down Expand Up @@ -225,9 +228,18 @@ public function getMetadataFor($className)

$this->wakeupReflection($cached, $this->getReflectionService());
} else {
foreach ($this->loadMetadata($realClassName) as $loadedClassName) {
$item = $this->cache->getItem($this->getCacheKey($loadedClassName));
$item->set($this->loadedMetadata[$loadedClassName]);
$loadedMetadata = $this->loadMetadata($realClassName);
$classNames = array_combine(
array_map([$this, 'getCacheKey'], $loadedMetadata),
$loadedMetadata
);

foreach ($this->cache->getItems(array_keys($classNames)) as $item) {
if (! isset($classNames[$item->getKey()])) {
continue;
}

$item->set($this->loadedMetadata[$classNames[$item->getKey()]]);
$this->cache->saveDeferred($item);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ public function testWillIgnoreCacheEntriesThatAreNotMetadataInstances(): void

$item = $this->createMock(CacheItemInterface::class);

$item
->expects(self::any())
->method('getKey')
->willReturn($key);
$item
->expects(self::any())
->method('get')
Expand All @@ -185,6 +189,11 @@ public function testWillIgnoreCacheEntriesThatAreNotMetadataInstances(): void
->method('getItem')
->with($key)
->willReturn($item);
$cacheDriver
->expects(self::once())
->method('getItems')
->with([$key])
->willReturn([$item]);
$cacheDriver
->expects(self::once())
->method('saveDeferred')
Expand Down

0 comments on commit c2e4044

Please sign in to comment.