diff --git a/composer.json b/composer.json index 34db8cd..d9a0f1f 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": { diff --git a/lib/Asm89/Twig/CacheExtension/CacheProvider/PsrSimpleCacheAdapter.php b/lib/Asm89/Twig/CacheExtension/CacheProvider/PsrSimpleCacheAdapter.php new file mode 100644 index 0000000..363178d --- /dev/null +++ b/lib/Asm89/Twig/CacheExtension/CacheProvider/PsrSimpleCacheAdapter.php @@ -0,0 +1,110 @@ + + * + * 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; + + /** + * @var \Psr\SimpleCache\CacheException + */ + private $lastException; + + /** + * @param \Psr\SimpleCache\CacheInterface $cache + */ + public function __construct(\Psr\SimpleCache\CacheInterface $cache) + { + $this->cache = $cache; + } + + /** + * @param string $key + * @return mixed|false False, if there was no value to be fetched. Null or a string otherwise. + */ + public function fetch($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 integer $lifetime + * + * @return bool + */ + public function save($key, $value, $lifetime = 0) + { + // 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; + } + } +}