@@ 25-72 (lines=48) @@ | ||
22 | * |
|
23 | * @author Teoh Han Hui <[email protected]> |
|
24 | */ |
|
25 | final class CachedPropertyMetadataFactory implements PropertyMetadataFactoryInterface |
|
26 | { |
|
27 | const CACHE_KEY_PREFIX = 'property_metadata_'; |
|
28 | ||
29 | private $cacheItemPool; |
|
30 | private $decorated; |
|
31 | private $memoryCache = []; |
|
32 | ||
33 | public function __construct(CacheItemPoolInterface $cacheItemPool, PropertyMetadataFactoryInterface $decorated) |
|
34 | { |
|
35 | $this->cacheItemPool = $cacheItemPool; |
|
36 | $this->decorated = $decorated; |
|
37 | } |
|
38 | ||
39 | /** |
|
40 | * {@inheritdoc} |
|
41 | */ |
|
42 | public function create(string $resourceClass, string $property, array $options = []): PropertyMetadata |
|
43 | { |
|
44 | $localKey = serialize([$resourceClass, $property, $options]); |
|
45 | if (isset($this->memoryCache[$localKey])) { |
|
46 | return $this->memoryCache[$localKey]; |
|
47 | } |
|
48 | ||
49 | $cacheKey = self::CACHE_KEY_PREFIX.md5($localKey); |
|
50 | ||
51 | try { |
|
52 | $cacheItem = $this->cacheItemPool->getItem($cacheKey); |
|
53 | ||
54 | if ($cacheItem->isHit()) { |
|
55 | return $this->memoryCache[$localKey] = $cacheItem->get(); |
|
56 | } |
|
57 | } catch (CacheException $e) { |
|
58 | // do nothing |
|
59 | } |
|
60 | ||
61 | $propertyMetadata = $this->decorated->create($resourceClass, $property, $options); |
|
62 | ||
63 | if (!isset($cacheItem)) { |
|
64 | return $this->memoryCache[$localKey] = $propertyMetadata; |
|
65 | } |
|
66 | ||
67 | $cacheItem->set($propertyMetadata); |
|
68 | $this->cacheItemPool->save($cacheItem); |
|
69 | ||
70 | return $this->memoryCache[$localKey] = $propertyMetadata; |
|
71 | } |
|
72 | } |
|
73 |
@@ 25-72 (lines=48) @@ | ||
22 | * |
|
23 | * @author Teoh Han Hui <[email protected]> |
|
24 | */ |
|
25 | final class CachedPropertyNameCollectionFactory implements PropertyNameCollectionFactoryInterface |
|
26 | { |
|
27 | const CACHE_KEY_PREFIX = 'property_name_collection_'; |
|
28 | ||
29 | private $cacheItemPool; |
|
30 | private $decorated; |
|
31 | private $memoryCache = []; |
|
32 | ||
33 | public function __construct(CacheItemPoolInterface $cacheItemPool, PropertyNameCollectionFactoryInterface $decorated) |
|
34 | { |
|
35 | $this->cacheItemPool = $cacheItemPool; |
|
36 | $this->decorated = $decorated; |
|
37 | } |
|
38 | ||
39 | /** |
|
40 | * {@inheritdoc} |
|
41 | */ |
|
42 | public function create(string $resourceClass, array $options = []): PropertyNameCollection |
|
43 | { |
|
44 | $localKey = serialize([$resourceClass, $options]); |
|
45 | if (isset($this->memoryCache[$localKey])) { |
|
46 | return $this->memoryCache[$localKey]; |
|
47 | } |
|
48 | ||
49 | $cacheKey = self::CACHE_KEY_PREFIX.md5($localKey); |
|
50 | ||
51 | try { |
|
52 | $cacheItem = $this->cacheItemPool->getItem($cacheKey); |
|
53 | ||
54 | if ($cacheItem->isHit()) { |
|
55 | return $this->memoryCache[$localKey] = $cacheItem->get(); |
|
56 | } |
|
57 | } catch (CacheException $e) { |
|
58 | // do nothing |
|
59 | } |
|
60 | ||
61 | $propertyNameCollection = $this->decorated->create($resourceClass, $options); |
|
62 | ||
63 | if (!isset($cacheItem)) { |
|
64 | return $this->memoryCache[$localKey] = $propertyNameCollection; |
|
65 | } |
|
66 | ||
67 | $cacheItem->set($propertyNameCollection); |
|
68 | $this->cacheItemPool->save($cacheItem); |
|
69 | ||
70 | return $this->memoryCache[$localKey] = $propertyNameCollection; |
|
71 | } |
|
72 | } |
|
73 |