From a57e9876f244d1dc18617ca14380dbaf7fe4adee Mon Sep 17 00:00:00 2001 From: "Heino H. Gehlsen" Date: Thu, 3 Aug 2017 21:32:32 +0200 Subject: [PATCH 1/4] Add PSR-16 compatible adapter --- .../CacheProvider/PsrSimpleCacheAdapter.php | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 lib/Asm89/Twig/CacheExtension/CacheProvider/PsrSimpleCacheAdapter.php diff --git a/lib/Asm89/Twig/CacheExtension/CacheProvider/PsrSimpleCacheAdapter.php b/lib/Asm89/Twig/CacheExtension/CacheProvider/PsrSimpleCacheAdapter.php new file mode 100644 index 0000000..490ec6c --- /dev/null +++ b/lib/Asm89/Twig/CacheExtension/CacheProvider/PsrSimpleCacheAdapter.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Asm89\Twig\CacheExtension\CacheProvider; + +/** + * Adapter class to make the Asm89 Twig caching extension interoperable with every PSR-16 adapter. + * + * @see http://www.php-fig.org/psr/psr-16/ + * + * @author Heino H. Gehlsen + * @copyright 2017 Heino H. Gehlsen + * @license MIT + */ +class PsrSimpleCacheAdapter + implements \Asm89\Twig\CacheExtension\CacheProviderInterface +{ + /** + * @var \Psr\SimpleCache\CacheInterface + */ + private $cache; + + /** + * @param \Psr\SimpleCache\CacheInterface $cache + */ + public function __construct(\Psr\SimpleCache\CacheInterface $cache) + { + $this->cache = $cache; + } + + /** + * @param string $key + * @return mixed|false + */ + public function fetch($key) + { + return $this->cache->get($key); + } + + /** + * @param string $key + * @param string $value + * @param int|\DateInterval $lifetime + * @return bool + */ + public function save($key, $value, $lifetime = 0) + { + return $this->cache->set($key, $value, $lifetime); + } +} From e0d453af862eeeeb6e65dc374c7533b6ec6cb892 Mon Sep 17 00:00:00 2001 From: "Heino H. Gehlsen" Date: Thu, 3 Aug 2017 21:33:00 +0200 Subject: [PATCH 2/4] Update composer.json to also suggest 'psr/simple-cache-implementation' --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 34db8cd..fded3a1 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ "doctrine/cache": "~1.0" }, "suggest": { - "psr/cache-implementation": "To make use of PSR-6 cache implementation via PsrCacheAdapter." + "psr/cache-implementation": "To make use of PSR-6 cache implementation via PsrCacheAdapter.", + "psr/simple-cache-implementation": "To make use of PSR-16 cache implementation via PsrSimpleCacheAdapter.", }, "autoload": { "psr-4": { From 247d555846bab601997891434fbe3add70bbf5aa Mon Sep 17 00:00:00 2001 From: "Heino H. Gehlsen" Date: Thu, 3 Aug 2017 21:41:23 +0200 Subject: [PATCH 3/4] Remove comma from composer.json to make son valid... --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index fded3a1..d9a0f1f 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ }, "suggest": { "psr/cache-implementation": "To make use of PSR-6 cache implementation via PsrCacheAdapter.", - "psr/simple-cache-implementation": "To make use of PSR-16 cache implementation via PsrSimpleCacheAdapter.", + "psr/simple-cache-implementation": "To make use of PSR-16 cache implementation via PsrSimpleCacheAdapter." }, "autoload": { "psr-4": { From 12bcf00b89721654e1c9aa4f173b9232a244f042 Mon Sep 17 00:00:00 2001 From: "Heino H. Gehlsen" Date: Wed, 9 Aug 2017 11:52:40 +0200 Subject: [PATCH 4/4] Add exception handling --- .../CacheProvider/PsrSimpleCacheAdapter.php | 64 +++++++++++++++++-- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/lib/Asm89/Twig/CacheExtension/CacheProvider/PsrSimpleCacheAdapter.php b/lib/Asm89/Twig/CacheExtension/CacheProvider/PsrSimpleCacheAdapter.php index 490ec6c..363178d 100644 --- a/lib/Asm89/Twig/CacheExtension/CacheProvider/PsrSimpleCacheAdapter.php +++ b/lib/Asm89/Twig/CacheExtension/CacheProvider/PsrSimpleCacheAdapter.php @@ -28,6 +28,11 @@ class PsrSimpleCacheAdapter */ private $cache; + /** + * @var \Psr\SimpleCache\CacheException + */ + private $lastException; + /** * @param \Psr\SimpleCache\CacheInterface $cache */ @@ -38,21 +43,68 @@ public function __construct(\Psr\SimpleCache\CacheInterface $cache) /** * @param string $key - * @return mixed|false + * @return mixed|false False, if there was no value to be fetched. Null or a string otherwise. */ public function fetch($key) { - return $this->cache->get($key); + // Reset last exception + $this->lastException = null; + + try { + // Get value from implementing library. Return false if there was no value to be fetched + $value = $this->cache->get($key, false); + + // Return cached value + return $value; + + // "Exception interface for invalid cache arguments." + } catch (\Psr\SimpleCache\InvalidArgumentException $e) { + // Save exception for retrieval by caller + $this->lastException = $e; + // Return fail to caller + return false; + + // "Interface used for all types of exceptions thrown by the implementing library." + } catch (\Psr\SimpleCache\CacheException $e) { + // Save exception for retrieval by caller + $this->lastException = $e; + // Return fail to caller + return false; + } } /** - * @param string $key - * @param string $value - * @param int|\DateInterval $lifetime + * @param string $key + * @param string $value + * @param integer $lifetime + * * @return bool */ public function save($key, $value, $lifetime = 0) { - return $this->cache->set($key, $value, $lifetime); + // Reset last exception + $this->lastException = null; + + try { + // Send value to implementing library. + $success = $this->cache->set($key, $value, $lifetime); + + // Return boolean from implementing library. + return $success; + + // "Exception interface for invalid cache arguments." + } catch (\Psr\SimpleCache\InvalidArgumentException $e) { + // Save exception for retrieval by caller + $this->lastException = $e; + // Return fail to caller + return false; + + // "Interface used for all types of exceptions thrown by the implementing library." + } catch (\Psr\SimpleCache\CacheException $e) { + // Save exception for retrieval by caller + $this->lastException = $e; + // Return fail to caller + return false; + } } }