Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Commit

Permalink
New CacheWrapper object to merge PSr6/16 cache routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Nik Barham committed Sep 19, 2017
1 parent 452ebf6 commit a7dcce8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 49 deletions.
74 changes: 74 additions & 0 deletions src/CacheWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Circuit;

use Psr\SimpleCache\CacheInterface as Psr16;
use Psr\Cache\CacheItemPoolInterface as Psr6;

/**
* CacheWrapper
*
* Wrap PSR 6/16 cache objects with simple interface
*
* @author Nik Barham <[email protected]>
*/
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);
}
}
}
54 changes: 5 additions & 49 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand All @@ -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
*
Expand Down

0 comments on commit a7dcce8

Please sign in to comment.