Skip to content

Commit

Permalink
Provide a PSR-16 decorator for storage adapters
Browse files Browse the repository at this point in the history
This patch builds on both the approach made in zendframework#126 as well as a comment
to that patch made by @marc-mabe (zendframework#126 (comment))
in order to provide a decorator for zend-cache storage adapters that
fulfills the PSR-16 `SimpleCacheInterface`.

Usage is as follows:

```php
// $adapter is an instance of StorageInterface
$cache = new SimpleCacheDecorator($adapter);
```

From there, you can then use `$cache` anywhere you would normally use a
PSR-16 instance.
  • Loading branch information
weierophinney committed Apr 18, 2018
1 parent 8817c52 commit 808b998
Show file tree
Hide file tree
Showing 6 changed files with 638 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"require": {
"php": "^5.6 || ^7.0",
"psr/cache": "^1.0",
"psr/simple-cache": "^1.0",
"zendframework/zend-eventmanager": "^2.6.3 || ^3.2",
"zendframework/zend-servicemanager": "^2.7.8 || ^3.3",
"zendframework/zend-stdlib": "^2.7.7 || ^3.1"
Expand Down
50 changes: 49 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

187 changes: 187 additions & 0 deletions src/Psr/SimpleCacheDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php
/**
* @see https://github.com/zendframework/zend-cache for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-cache/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Cache\Psr;

use Exception;
use Psr\SimpleCache\CacheInterface as SimpleCacheInterface;
use Throwable;
use Zend\Cache\Exception\InvalidArgumentException as ZendCacheInvalidArgumentException;
use Zend\Cache\Storage\FlushableInterface;
use Zend\Cache\Storage\StorageInterface;

/**
* Decoreate a zend-cache storage adapter for usage as a PSR-16 implementation.
*/
class SimpleCacheDecorator implements SimpleCacheInterface
{
/**
* @var StorageInterface
*/
private $storage;

/**
* Reference used by storage when calling getItem() to indicate status of
* operation.
*
* @var null|bool
*/
private $success;

public function __construct(StorageInterface $storage)
{
$this->storage = $storage;
}

/**
* {@inheritDoc}
*/
public function get($key, $default = null)
{
$this->success = null;
try {
$result = $this->storage->getItem($key, $this->success);
$result = $result === null ? $default : $result;
return $this->success ? $result : $default;
} catch (Throwable $e) {
throw static::translateException($e);
} catch (Exception $e) {
throw static::translateException($e);
}
}

/**
* {@inheritDoc}
*/
public function set($key, $value, $ttl = null)
{
$options = $this->storage->getOptions();
$previousTtl = $options->getTtl();
$options->setTtl($ttl);

try {
$result = $this->storage->setItem($key, $value);
} catch (Throwable $e) {
throw static::translateException($e);
} catch (Exception $e) {
throw static::translateException($e);
}

$options->setTtl($previousTtl);
return $result;
}

/**
* {@inheritDoc}
*/
public function delete($key)
{
try {
return $this->storage->removeItem($key);
} catch (Throwable $e) {
throw static::translateException($e);
} catch (Exception $e) {
throw static::translateException($e);
}
}

/**
* {@inheritDoc}
*/
public function clear()
{
if (! $this->storage instanceof FlushableInterface) {
return false;
}
return $this->storage->flush();
}

/**
* {@inheritDoc}
*/
public function getMultiple($keys, $default = null)
{
try {
$results = $this->storage->getItems($keys);
} catch (Throwable $e) {
throw static::translateException($e);
} catch (Exception $e) {
throw static::translateException($e);
}

foreach ($keys as $key) {
if (! isset($results[$key]) && null !== $default) {
$results[$key] = $default;
}
}

return $results;
}

/**
* {@inheritDoc}
*/
public function setMultiple($values, $ttl = null)
{
$options = $this->storage->getOptions();
$previousTtl = $options->getTtl();
$options->setTtl($ttl);

try {
$result = $this->storage->setItems($values);
} catch (Throwable $e) {
throw static::translateException($e);
} catch (Exception $e) {
throw static::translateException($e);
}

$options->setTtl($previousTtl);
return $result;
}

/**
* {@inheritDoc}
*/
public function deleteMultiple($keys)
{
try {
$result = $this->storage->removeItems($keys);
return empty($result);
} catch (Throwable $e) {
throw static::translateException($e);
} catch (Exception $e) {
throw static::translateException($e);
}
}

/**
* {@inheritDoc}
*/
public function has($key)
{
try {
return $this->storage->hasItem($key);
} catch (Throwable $e) {
throw static::translateException($e);
} catch (Exception $e) {
throw static::translateException($e);
}
}

/**
* @param Throwable|Exception $e
* @return SimpleCacheException
*/
private static function translateException($e)
{
$exceptionClass = $e instanceof ZendCacheInvalidArgumentException
? SimpleCacheInvalidArgumentException::class
: SimpleCacheException::class;

return new $exceptionClass($e->getMessage(), $e->getCode(), $e);
}
}
15 changes: 15 additions & 0 deletions src/Psr/SimpleCacheException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* @see https://github.com/zendframework/zend-cache for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-cache/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Cache\Psr;

use Psr\SimpleCache\CacheException as PsrCacheException;
use RuntimeException;

class SimpleCacheException extends RuntimeException implements PsrCacheException
{
}
14 changes: 14 additions & 0 deletions src/Psr/SimpleCacheInvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* @see https://github.com/zendframework/zend-cache for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-cache/blob/master/LICENSE.md New BSD License
*/

namespace Zend\Cache\Psr;

use Psr\SimpleCache\InvalidArgumentException as PsrInvalidArgumentException;

class SimpleCacheInvalidArgumentException extends \InvalidArgumentException implements PsrInvalidArgumentException
{
}
Loading

0 comments on commit 808b998

Please sign in to comment.