* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org */ class CachedListing implements ListingInterface { /** @var \PSX\Api\ListingInterface */ protected $listing; /** @var \Psr\Cache\CacheItemPoolInterface */ protected $cache; /** @var integer|null */ protected $expire; /** * @param \PSX\Api\ListingInterface $listing * @param \Psr\Cache\CacheItemPoolInterface $cache * @param integer|null $expire */ public function __construct(ListingInterface $listing, CacheItemPoolInterface $cache, $expire = null) { $this->listing = $listing; $this->cache = $cache; $this->expire = $expire; } /** * @inheritdoc */ public function getAvailableRoutes(FilterInterface $filter = null): iterable { $item = $this->cache->getItem($this->getResourceIndexKey($filter)); if ($item->isHit()) { return $item->get(); } $result = $this->listing->getAvailableRoutes($filter); $item->set($result); $item->expiresAfter($this->expire); $this->cache->save($item); return $result; } /** * @inheritdoc */ public function find(string $path, ?string $version = null): ?SpecificationInterface { $item = $this->cache->getItem($this->getResourceKey($path, $version)); if ($item->isHit()) { return $item->get(); } $specification = $this->listing->find($path, $version); if (!$specification instanceof SpecificationInterface) { return null; } $item->set($specification); $item->expiresAfter($this->expire); $this->cache->save($item); return $specification; } /** * @inheritdoc */ public function findAll(?string $version = null, FilterInterface $filter = null): SpecificationInterface { $item = $this->cache->getItem($this->getResourceCollectionKey($version, $filter)); if ($item->isHit()) { return $item->get(); } $collection = $this->listing->findAll($version, $filter); $item->set($collection); $item->expiresAfter($this->expire); $this->cache->save($item); return $collection; } /** * Invalidates the cached resource index */ public function invalidateResourceIndex(FilterInterface $filter = null) { $this->cache->deleteItem($this->getResourceIndexKey($filter)); } /** * Invalidates a cached resource * * @param string $sourcePath * @param integer|null $version */ public function invalidateResource($sourcePath, $version = null) { $this->cache->deleteItem($this->getResourceKey($sourcePath, $version)); } /** * Invalidates the cached resource collection * * @param integer|null $version */ public function invalidateResourceCollection($version = null, FilterInterface $filter = null) { $this->cache->deleteItem($this->getResourceCollectionKey($version, $filter)); } /** * @return string */ protected function getResourceIndexKey(FilterInterface $filter = null) { return 'api-resource-index' . ($filter !== null ? '-' . $filter->getId() : ''); } /** * @param string $path * @param integer|null $version * @return string */ protected function getResourceKey($path, $version = null) { return 'api-resource-' . substr(md5($path), 0, 16) . '-' . intval($version); } /** * @param integer|null $version * @return string */ protected function getResourceCollectionKey($version = null, FilterInterface $filter = null) { return 'api-resource-collection-' . intval($version) . ($filter !== null ? '-' . $filter->getId() : ''); } } __halt_compiler();----SIGNATURE:----efyknxK9F2QHLNLs8EUpYCWItWhayxNvTqc6jlrKSKrCRq1z+UbZQiFpgj4VCqhdx/qK7NCFGHvfY1lzbl58+7gbwIoKW3SsHgdDDPHUu0kjOtgeL6bweGEfJjeuvlW37l15A8cDqJoh/Dm4dHVqGvFk83ydmHb3IDPBb48xL2G9M7vzvlDs4JBWY9oEq1zwk6hHA3vpXCtZ0Xhb0GGNumGg70329XDjyeAFgeHXS9GA35UtAusNiD+pM+6uCcreswo7TiK6TpG0AqMlaGwz2/TrHIAf8Drogp2PpyRBP6aJqpxrINC98tcSYjFUq/HXlxuqRNEcqim5d4QgPCMYY7NlyjK+k1SYahRddHyXJibJ6avWGR1K7kBu3fMFIo1DtjWw9GJXZRWSyU8Qv4nMSkFB8cUz57o78irunHNfV5x5kEECrvEuzOsIedoM06d+ha6hOaPoXPCmG7ZCTJpIB6swBjWGzeqZuZBoOCvSCQZzKr7Aj7PAmC9pwTliiG9Imec2YYHGCM4QfnA9ltGJ+yREDVBFaw+ej9xfKXcwVztCOUVnZ8WIOLKd3T93DqiOQpwTIoHeKUxt0ncvaxxYMGx+aYxQ+XMdynxss0jq6H3VdrtzU4pfIg1bOF6MjwJ5Lj99PBupcxJo1OKxqzD3zCaZ/NpyBQLlZJGrxuHxlhE=----ATTACHMENT:----NjY3ODE2Mzk3NDQ5NDU5NSA5NTI2NjY5NzUyNTA3MTE2IDMyMTE1MjQ5NDk2NDc2Mjg=