From a7dcce8b1d3894b69c64c48162ca12c9f9b8e97e Mon Sep 17 00:00:00 2001 From: Nik Barham Date: Tue, 19 Sep 2017 17:05:59 +0000 Subject: [PATCH] New CacheWrapper object to merge PSr6/16 cache routes --- src/CacheWrapper.php | 74 ++++++++++++++++++++++++++++++++++++++++++++ src/Router.php | 54 +++----------------------------- 2 files changed, 79 insertions(+), 49 deletions(-) create mode 100644 src/CacheWrapper.php diff --git a/src/CacheWrapper.php b/src/CacheWrapper.php new file mode 100644 index 0000000..0456e59 --- /dev/null +++ b/src/CacheWrapper.php @@ -0,0 +1,74 @@ + + */ +class CacheWrapper +{ + /** @var Psr16|Psr6 PSR6/16 compatible cache item */ + protected $cache; + + /** + * @param Psr16|Psr6 $cache A PSR-6 or PSR-16 compatible Cache object + */ + public function __construct($cache = null) + { + $this->cache = $cache; + + // PSR-16 Cache + if (!$this->cache instanceof Psr16 && !$this->cache instanceof Psr6) { + throw new \InvalidArgumentException('Expect PSR 6/16 compatible cache object'); + } + } + + /** + * @param mixed $key Key to get from the cache + */ + public function get($key) + { + // PSR-16 Cache + if ($this->cache instanceof Psr16) { + return $this->cache->get($key); + } + + // PSR-6 Cache + if ($this->cache instanceof Psr6) { + $item = $this->cache->getItem($key); + if ($item->isHit()) { + return $item->get(); + } + } + + return null; + } + + /** + * @param mixed $key Key to set in the cache + * @param mixed $value Value to set in the cache + * @param mixed $expiry Cache expiry in seconds + */ + public function set($key, $value, $expiry = 3600) + { + // PSR-16 Cache + if ($this->cache instanceof Psr16) { + return $this->cache->set($key, $value, $expiry); + } + + // PSR-6 Cache + if ($this->cache instanceof Psr6) { + $item = $this->cache->getItem($key); + $item->set($value); + $item->expiresAt(new \DateTime('now + ' . $expiry . 'seconds')); + return $this->cache->save($item); + } + } +} diff --git a/src/Router.php b/src/Router.php index cfdc55e..446a3d9 100644 --- a/src/Router.php +++ b/src/Router.php @@ -40,7 +40,7 @@ class Router implements Delegate, LoggerAwareInterface /** @var float Timer for debug logging purposes */ protected $stopwatch; - /** @var Psr16|Psr6 PSR6/16 compatible cache item */ + /** @var CacheWrapper PSR6/16 compatible cache item */ protected $cache; /** @var bool Did we pull results from cache i.e. do we need to call the RouteCollector callback */ @@ -77,21 +77,9 @@ public function __construct(array $options = [], $cache = null, LoggerInterface 'routeCollector' => 'Circuit\\RouteCollector', 'cacheTimeout' => 3600 ]; - $this->cache = $cache; $this->logger = $logger; - - // PSR-16 Cache - if ($this->cache instanceof Psr16) { - $this->routeCollection = $this->cache->get(static::CACHE_KEY); - } - - // PSR-6 Cache - if ($this->cache instanceof Psr6) { - $item = $this->cache->getItem(static::CACHE_KEY); - if ($item->isHit()) { - $this->routeCollection = $item->get(); - } - } + $this->cache = $cache ? new CacheWrapper($cache) : null; + $this->routeCollection = $this->cache ? $this->cache->get(static::CACHE_KEY) : null; if ($this->routeCollection) { $this->cached = true; @@ -113,48 +101,16 @@ public function defineRoutes(callable $definitionCallback) { if (!$this->cached) { $definitionCallback($this->routeCollection); - // PSR-16 Cache - if ($this->cache instanceof Psr16) { + // Cache + if ($this->cache) { $this->cache->set(static::CACHE_KEY, $this->routeCollection, $this->options['cacheTimeout']); } - - // PSR-6 Cache - if ($this->cache instanceof Psr6) { - $item = $this->cache->getItem(static::CACHE_KEY); - $item->set($this->routeCollection); - $item->expiresAt(new \DateTime('now + ' . $this->options['cacheTimeout'] . 'seconds')); - $this->cache->save($item); - } } $this->dispatcher = new $this->options['dispatcher']($this->routeCollection->getData()); return $this; } - /** - * Internal function to retrieve a cached value from PSR-6/16 cache object - * - * @param string $key Cache key to retrieve from cache - */ - protected function getCachedValue($key) - { - if (!$this->cache) { - return null; - } - - if ($this->cache instanceof Psr\SimpleCache\CacheInterface) { - return $this->cache->get($key); - } - - if ($this->cache instanceof Psr\Cache\CachePoolInterface) { - $item = $this->cache->getItem($key); - if (!$item->isHit()) { - return null; - } - return $item->get(); - } - } - /** * Execute a route *