From 8ff3a841f1dd90e0e5baef02589bc12bb186f58f Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Mon, 15 Jan 2024 06:10:37 +0700 Subject: [PATCH] fix(`CachingMappingFactory`): Cache result in memory. --- CHANGELOG.md | 1 + src/Mapping/CachingMappingFactory.php | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1112c03..b1e91513 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ * fix: Wrong service id for `CopyTransformer`. * fix: Remove cache file & regenerate it if it is corrupt. * fix: `NullTransformer` bug. +* fix(`CachingMappingFactory`): Cache result in memory. ## 0.5.8 diff --git a/src/Mapping/CachingMappingFactory.php b/src/Mapping/CachingMappingFactory.php index 93e93b97..6fb0a2ec 100644 --- a/src/Mapping/CachingMappingFactory.php +++ b/src/Mapping/CachingMappingFactory.php @@ -21,7 +21,7 @@ final class CachingMappingFactory implements MappingFactoryInterface, CacheWarmerInterface { - private ?Mapping $innerMappingCache = null; + private ?Mapping $mapping = null; public function __construct( private MappingFactoryInterface $realMappingFactory, @@ -31,11 +31,7 @@ public function __construct( private function getMappingFromInnerFactory(): Mapping { - if ($this->innerMappingCache === null) { - $this->innerMappingCache = $this->realMappingFactory->getMapping(); - } - - return $this->innerMappingCache; + return $this->realMappingFactory->getMapping(); } private function warmUpAndGetMapping(): Mapping @@ -47,8 +43,12 @@ private function warmUpAndGetMapping(): Mapping public function getMapping(): Mapping { + if ($this->mapping !== null) { + return $this->mapping; + } + if (!file_exists($this->getCacheFilePath())) { - return $this->warmUpAndGetMapping(); + return $this->mapping = $this->warmUpAndGetMapping(); } try { @@ -57,16 +57,16 @@ public function getMapping(): Mapping } catch (\Throwable) { unlink($this->getCacheFilePath()); - return $this->warmUpAndGetMapping(); + return $this->mapping = $this->warmUpAndGetMapping(); } if (!$result instanceof Mapping) { unlink($this->getCacheFilePath()); - return $this->warmUpAndGetMapping(); + return $this->mapping = $this->warmUpAndGetMapping(); } - return $result; + return $this->mapping = $result; } public function isOptional(): bool