forked from zendframework/zend-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a PSR-16 decorator for storage adapters
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
1 parent
8817c52
commit 808b998
Showing
6 changed files
with
638 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
Oops, something went wrong.