e1307d330d74f6ac0e9047229b94f0c93f0c440b
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 declare(strict_types=1);
4
5 namespace CuyZ\Valinor\Definition\Repository\Cache;
6
7 use CuyZ\Valinor\Definition\ClassDefinition;
8 use CuyZ\Valinor\Definition\Repository\ClassDefinitionRepository;
9 use CuyZ\Valinor\Type\ClassType;
10 use Psr\SimpleCache\CacheInterface;
11
12 /** @internal */
13 final class CacheClassDefinitionRepository implements ClassDefinitionRepository
14 {
15 public function __construct(
16 private ClassDefinitionRepository $delegate,
17 /** @var CacheInterface<ClassDefinition> */
18 private CacheInterface $cache
19 ) {}
20
21 public function for(ClassType $type): ClassDefinition
22 {
23 $key = "class-definition-{$type->toString()}";
24
25 $entry = $this->cache->get($key);
26
27 if ($entry) {
28 return $entry;
29 }
30
31 $class = $this->delegate->for($type);
32
33 $this->cache->set($key, $class);
34
35 return $class;
36 }
37 }