Skip to content

Commit

Permalink
Merge pull request #1 from vondrasekpavel/master
Browse files Browse the repository at this point in the history
WS, CS, typehints :)
  • Loading branch information
janatjak committed Dec 31, 2016
2 parents e59b665 + eae5cab commit e06fc1b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 41 deletions.
69 changes: 48 additions & 21 deletions src/Cache.php
Expand Up @@ -6,39 +6,42 @@
* For the full copyright and license information, please view the LICENSE file.
*/

declare(strict_types=1);

namespace FreezyBee\NetteCachingPsr6;

use Closure;
use FreezyBee\NetteCachingPsr6\Exception\InvalidArgumentException;
use Nette\Caching\IStorage;
use Nette\Caching\Cache as NCache;
use Nette\Caching\Cache as NetteCache;
use Nette\InvalidArgumentException as NetteArgumentException;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;

/**
* Class Cache
* @package FreezyBee\NetteCachingPsr6
* @author Jakub Janata <jakubjanata@gmail.com>
*/
class Cache implements CacheItemPoolInterface
{
/** @var NCache */
protected $nCache;
/** @var NetteCache */
protected $netteCache;

/** @var CacheItem[] */
protected $deferred;

/** @var \Closure */
/** @var Closure */
protected $createCacheItem;

/**
* @param IStorage $storage
*
* @param string $namespace
*/
public function __construct(IStorage $storage, $namespace = NULL)
public function __construct(IStorage $storage, string $namespace = null)
{
$this->nCache = new NCache($storage, $namespace);
$this->netteCache = new NetteCache($storage, $namespace);

$this->createCacheItem = \Closure::bind(
$this->createCacheItem = Closure::bind(
function (string $key, $value, bool $isHit) {
$item = new CacheItem();
$item->key = $key;
Expand All @@ -55,21 +58,28 @@ function (string $key, $value, bool $isHit) {

/**
* @param string $key
*
* @return CacheItem
*
* @throws InvalidArgumentException
*/
public function getItem($key)
{
self::validateKey($key);

$value = $this->nCache->load($key);
$value = $this->netteCache->load($key);

$f = $this->createCacheItem;

return $f($key, $value, $value !== null);
}

/**
* @param string[] $keys
*
* @return CacheItem[]|array|\Traversable
*
* @throws InvalidArgumentException
*/
public function getItems(array $keys = [])
{
Expand All @@ -78,7 +88,7 @@ public function getItems(array $keys = [])
}

$items = [];
$rawItems = $this->nCache->bulkLoad($keys);
$rawItems = $this->netteCache->bulkLoad($keys);
$f = $this->createCacheItem;

foreach ($rawItems as $key => $value) {
Expand All @@ -90,9 +100,12 @@ public function getItems(array $keys = [])

/**
* @param string $key
*
* @return bool
*
* @throws InvalidArgumentException
*/
public function hasItem($key)
public function hasItem($key): bool
{
return $this->getItem($key)->isHit();
}
Expand All @@ -103,13 +116,16 @@ public function hasItem($key)
public function clear()
{
$this->deferred = [];
$this->nCache->clean([NCache::ALL]);
$this->netteCache->clean([NetteCache::ALL]);
return true;
}

/**
* @param string $key
*
* @return bool
*
* @throws InvalidArgumentException
*/
public function deleteItem($key)
{
Expand All @@ -118,27 +134,33 @@ public function deleteItem($key)

/**
* @param string[] $keys
*
* @return bool
*
* @throws InvalidArgumentException
*/
public function deleteItems(array $keys)
public function deleteItems(array $keys): bool
{
foreach ($keys as $key) {
self::validateKey($key);
}

foreach ($keys as $key) {
unset($this->deferred[$key]);
$this->nCache->remove($key);
$this->netteCache->remove($key);
}

return true;
}

/**
* @param CacheItem|CacheItemInterface $item
*
* @return bool
*
* @throws NetteArgumentException
*/
public function save(CacheItemInterface $item)
public function save(CacheItemInterface $item): bool
{
if (!$item instanceof CacheItem) {
return false;
Expand All @@ -150,9 +172,10 @@ public function save(CacheItemInterface $item)

/**
* @param CacheItemInterface|CacheItem $item
*
* @return bool
*/
public function saveDeferred(CacheItemInterface $item)
public function saveDeferred(CacheItemInterface $item): bool
{
if (!$item instanceof CacheItem) {
return false;
Expand All @@ -164,18 +187,20 @@ public function saveDeferred(CacheItemInterface $item)

/**
* @return bool
*
* @throws NetteArgumentException
*/
public function commit()
public function commit(): bool
{
foreach ($this->deferred as $item) {
$this->nCache->save($item->getKey(), $item->get(), [NCache::EXPIRE => $item->getExpiry()]);
$this->netteCache->save($item->getKey(), $item->get(), [NetteCache::EXPIRE => $item->getExpiry()]);
}

return true;
}

/**
*
* @throws NetteArgumentException
*/
public function __destruct()
{
Expand All @@ -186,7 +211,9 @@ public function __destruct()

/**
* Validates a cache key according to PSR-6.
* @param string $key The key to validate
*
* @param mixed $key The key to validate
*
* @throws InvalidArgumentException When $key is not valid.
*/
public static function validateKey($key)
Expand Down
45 changes: 31 additions & 14 deletions src/CacheItem.php
Expand Up @@ -6,8 +6,11 @@
* For the full copyright and license information, please view the LICENSE file.
*/

declare(strict_types=1);

namespace FreezyBee\NetteCachingPsr6;

use DateTimeInterface;
use FreezyBee\NetteCachingPsr6\Exception\InvalidArgumentException;
use Psr\Cache\CacheItemInterface;

Expand All @@ -17,25 +20,35 @@
*/
class CacheItem implements CacheItemInterface
{
/** @var string */
/**
* @var string
*/
protected $key;

/** @var mixed */
/**
* @var mixed
*/
protected $value;

/** @var bool */
/**
* @var bool
*/
protected $isHit;

/** @var int */
/**
* @var null|int
*/
protected $expiry;

/** @var int */
/**
* @var int
*/
protected $defaultLifetime;

/**
* {@inheritdoc}
*/
public function getKey()
public function getKey(): string
{
return $this->key;
}
Expand All @@ -51,29 +64,31 @@ public function get()
/**
* {@inheritdoc}
*/
public function isHit()
public function isHit(): bool
{
return $this->isHit;
}

/**
* {@inheritdoc}
*/
public function set($value)
public function set($value): CacheItemInterface
{
$this->value = $value;
return $this;
}

/**
* {@inheritdoc}
*
* @throws InvalidArgumentException
*/
public function expiresAt($expiration)
public function expiresAt($expiration): CacheItemInterface
{
if (null === $expiration) {
if ($expiration === null) {
$this->expiry = $this->defaultLifetime > 0 ? time() + $this->defaultLifetime : null;
} elseif ($expiration instanceof \DateTimeInterface) {
$this->expiry = (int)$expiration->format('U');
} elseif ($expiration instanceof DateTimeInterface) {
$this->expiry = (int) $expiration->format('U');
} else {
throw new InvalidArgumentException(sprintf(
'Expiration date must implement DateTimeInterface or be null, "%s" given',
Expand All @@ -86,8 +101,10 @@ public function expiresAt($expiration)

/**
* {@inheritdoc}
*
* @throws InvalidArgumentException
*/
public function expiresAfter($time)
public function expiresAfter($time): CacheItemInterface
{
if (null === $time) {
$this->expiry = $this->defaultLifetime > 0 ? time() + $this->defaultLifetime : null;
Expand All @@ -106,7 +123,7 @@ public function expiresAfter($time)
}

/**
* @return int
* @return null|int
*/
public function getExpiry()
{
Expand Down
8 changes: 5 additions & 3 deletions src/Exception/CacheException.php
Expand Up @@ -8,11 +8,13 @@

namespace FreezyBee\NetteCachingPsr6\Exception;

use Exception;
use Psr\Cache\CacheException as PsrCacheException;

/**
* Class CacheException
* @package FreezyBee\NetteCachingPsr6
* @author Jakub Janata <jakubjanata@gmail.com>
*/
class CacheException extends \Exception implements \Psr\Cache\CacheException
class CacheException extends Exception implements PsrCacheException
{

}
7 changes: 4 additions & 3 deletions src/Exception/InvalidArgumentException.php
Expand Up @@ -8,11 +8,12 @@

namespace FreezyBee\NetteCachingPsr6\Exception;

use Psr\Cache\InvalidArgumentException as PsrCacheArgumentException;

/**
* Class InvalidArgumentException
* @package FreezyBee\NetteCachingPsr6
* @author Jakub Janata <jakubjanata@gmail.com>
*/
class InvalidArgumentException extends CacheException implements \Psr\Cache\InvalidArgumentException
class InvalidArgumentException extends CacheException implements PsrCacheArgumentException
{

}

0 comments on commit e06fc1b

Please sign in to comment.