-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Add caching for
ObjectMappingResolver
.
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/Transformer/ObjectMappingResolver/CachingObjectMappingResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Transformer\ObjectMappingResolver; | ||
|
||
use Psr\Cache\CacheItemPoolInterface; | ||
use Rekalogika\Mapper\Context\Context; | ||
use Rekalogika\Mapper\Transformer\ObjectMappingResolver\Contracts\ObjectMapping; | ||
use Rekalogika\Mapper\Transformer\ObjectMappingResolver\Contracts\ObjectMappingResolverInterface; | ||
|
||
final class CachingObjectMappingResolver implements ObjectMappingResolverInterface | ||
{ | ||
/** | ||
* @var array<string,ObjectMapping> | ||
*/ | ||
private array $cache = []; | ||
|
||
public function __construct( | ||
private ObjectMappingResolverInterface $decorated, | ||
private CacheItemPoolInterface $cacheItemPool, | ||
) { | ||
} | ||
|
||
public function resolveObjectMapping( | ||
string $sourceClass, | ||
string $targetClass, | ||
Context $context | ||
): ObjectMapping { | ||
$cacheKey = $sourceClass . ':' . $targetClass; | ||
|
||
if (isset($this->cache[$cacheKey])) { | ||
return $this->cache[$cacheKey]; | ||
} | ||
|
||
$cacheItem = $this->cacheItemPool->getItem($cacheKey); | ||
|
||
if ($cacheItem->isHit()) { | ||
/** @var mixed */ | ||
$cached = $cacheItem->get(); | ||
|
||
if ($cached instanceof ObjectMapping) { | ||
return $this->cache[$cacheKey] = $cached; | ||
} | ||
|
||
unset($this->cache[$cacheKey]); | ||
$this->cacheItemPool->deleteItem($cacheKey); | ||
} | ||
|
||
$objectMapping = $this->decorated | ||
->resolveObjectMapping($sourceClass, $targetClass, $context); | ||
|
||
$cacheItem->set($objectMapping); | ||
$this->cacheItemPool->save($cacheItem); | ||
|
||
return $this->cache[$cacheKey] = $objectMapping; | ||
} | ||
} |