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