Skip to content

Commit

Permalink
fix(CachingMappingFactory): Cache result in memory.
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 14, 2024
1 parent c6e09a1 commit 8ff3a84
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 10 additions & 10 deletions src/Mapping/CachingMappingFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class CachingMappingFactory implements
MappingFactoryInterface,
CacheWarmerInterface
{
private ?Mapping $innerMappingCache = null;
private ?Mapping $mapping = null;

public function __construct(
private MappingFactoryInterface $realMappingFactory,
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 8ff3a84

Please sign in to comment.