* @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:----A28ncSp+FfimVyrGwx6wWsMucxLMR3stCfBYHA86JxIOKYFW0KVGLY//cU2Yb8rgwvUAJr/yTpdZ85pedl/QwwPXqL3MSiLJivBufp2tnsibZPI97IWMstPP8FafEJ1Tl/pTVsWKRhrvGReAZUjIgIbmkzTGogkJ+u4sKXWXnNUCuLnIegMYbysz4zDNmOqSxjEdq8Unh400MyBW8eRn+qgt7doUoECKL/b1ZhH9SbjrgiIpVbQG4c/A12gNlHMVsNJyV8pxvlFx0j6BA4va+FIevYXxkoceD7zcLsTj4fMGNg83z0vFaDPNlNR8ong01bvt0VEaZGM7kB/Bs8ptRsX6Sri+Gkq6EjTh6RRQUU/gNHVnsC2G9qVrcO6mWWgIC/9/I0efC4THjBxBrevTHnD/RUPk8IOBDhlE3dZGFLK2sY29wjVoiA6WNofs90YcTRYW2QR9BtayVjat7hSR772pDFBfd9cKt0PGeUy/EM+CjprIv+mcViaDH3MeNkR/FAo9d7bYOfdm2v3Evs05OX4d8g+55GyRolgJEUj7wxzl7RDOlpzVim4MD+4sBOc1hQ7CQjWBSwn3GqikWabBslD1S7pffozO8MFg7EaE6m/HK0JlO06DSiCl1Gxujxkknp8Sv4UPH7WXSBqJrpIUrCvnrJAXs/9KPf3HI+HrBVg=----ATTACHMENT:----OTUxOTQ4ODMxNTkxODE5NyAxNzY1NjcyMTI3ODMwNDk1IDYzNTA5MjIxOTU2NzYxMTE=