From 6ee306bf33a152b49a8103e142a942c9f0b23519 Mon Sep 17 00:00:00 2001 From: Bastien Jaillot Date: Thu, 19 Dec 2019 14:29:37 +0100 Subject: [PATCH] [Cache] add array cache in front of serializer cache --- .../Mapping/Factory/CacheClassMetadataFactory.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Serializer/Mapping/Factory/CacheClassMetadataFactory.php b/src/Symfony/Component/Serializer/Mapping/Factory/CacheClassMetadataFactory.php index 0b904c14400d..4737dcabd168 100644 --- a/src/Symfony/Component/Serializer/Mapping/Factory/CacheClassMetadataFactory.php +++ b/src/Symfony/Component/Serializer/Mapping/Factory/CacheClassMetadataFactory.php @@ -32,6 +32,8 @@ class CacheClassMetadataFactory implements ClassMetadataFactoryInterface */ private $cacheItemPool; + private $loadedClasses = []; + public function __construct(ClassMetadataFactoryInterface $decorated, CacheItemPoolInterface $cacheItemPool) { $this->decorated = $decorated; @@ -44,18 +46,23 @@ public function __construct(ClassMetadataFactoryInterface $decorated, CacheItemP public function getMetadataFor($value) { $class = $this->getClass($value); + + if (isset($this->loadedClasses[$class])) { + return $this->loadedClasses[$class]; + } + // Key cannot contain backslashes according to PSR-6 $key = strtr($class, '\\', '_'); $item = $this->cacheItemPool->getItem($key); if ($item->isHit()) { - return $item->get(); + return $this->loadedClasses[$class] = $item->get(); } $metadata = $this->decorated->getMetadataFor($value); $this->cacheItemPool->save($item->set($metadata)); - return $metadata; + return $this->loadedClasses[$class] = $metadata; } /**