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 Nov 29, 2020
1 parent 69af1c2 commit 4c21399
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
use Symfony\Component\Cache\DoctrineProvider;

use function array_keys;
use function array_reverse;
use function array_unshift;
use function explode;
Expand Down Expand Up @@ -225,9 +226,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 Expand Up @@ -450,6 +460,11 @@ protected function getCacheKey(string $realClassName): string
return str_replace('\\', '__', $realClassName) . $this->cacheSalt;
}

protected function getClassNameFromCacheKey(string $cacheKey): string
{
return str_replace('__', '\\', substr($cacheKey, -strlen($this->cacheSalt)));
}

/**
* Gets the real class name of a class name that could be a proxy.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ public function testWillIgnoreCacheEntriesThatAreNotMetadataInstances(): void
$item = $this->createMock(CacheItemInterface::class);
assert($item instanceof CacheItemInterface || $item instanceof MockObject);

$item
->expects(self::any())
->method('getKey')
->willReturn($key);
$item
->expects(self::any())
->method('get')
Expand All @@ -192,6 +196,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 4c21399

Please sign in to comment.