-
-
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
TransformerRegistry
.
- Loading branch information
Showing
5 changed files
with
91 additions
and
6 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
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\TransformerRegistry; | ||
|
||
use Psr\Cache\CacheItemPoolInterface; | ||
use Rekalogika\Mapper\Transformer\Contracts\TransformerInterface; | ||
|
||
class CachingTransformerRegistry implements TransformerRegistryInterface | ||
{ | ||
/** | ||
* @var array<string,SearchResult> | ||
*/ | ||
private array $findBySourceAndTargetTypesCache = []; | ||
|
||
public function __construct( | ||
private TransformerRegistryInterface $decorated, | ||
private CacheItemPoolInterface $cacheItemPool, | ||
) { | ||
} | ||
|
||
public function get(string $id): TransformerInterface | ||
{ | ||
return $this->decorated->get($id); | ||
} | ||
|
||
public function findBySourceAndTargetTypes( | ||
array $sourceTypes, | ||
array $targetTypes, | ||
): SearchResult { | ||
$cacheKey = md5(serialize($sourceTypes) . '--' . serialize($targetTypes)); | ||
|
||
if (isset($this->findBySourceAndTargetTypesCache[$cacheKey])) { | ||
return $this->findBySourceAndTargetTypesCache[$cacheKey]; | ||
} | ||
|
||
$cacheItem = $this->cacheItemPool->getItem($cacheKey); | ||
|
||
if ($cacheItem->isHit()) { | ||
/** @var mixed */ | ||
$result = $cacheItem->get(); | ||
|
||
if ($result instanceof SearchResult) { | ||
return $this->findBySourceAndTargetTypesCache[$cacheKey] = $result; | ||
} | ||
|
||
$this->cacheItemPool->deleteItem($cacheKey); | ||
} | ||
|
||
$result = $this->decorated->findBySourceAndTargetTypes($sourceTypes, $targetTypes); | ||
|
||
$cacheItem->set($result); | ||
$this->cacheItemPool->save($cacheItem); | ||
|
||
return $this->findBySourceAndTargetTypesCache[$cacheKey] = $result; | ||
} | ||
} |
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