diff --git a/system/src/Grav/Framework/Cache/AbstractCache.php b/system/src/Grav/Framework/Cache/AbstractCache.php index 90269213c..b9447f108 100644 --- a/system/src/Grav/Framework/Cache/AbstractCache.php +++ b/system/src/Grav/Framework/Cache/AbstractCache.php @@ -8,6 +8,8 @@ namespace Grav\Framework\Cache; +use Grav\Framework\Cache\Exception\InvalidArgumentException; + /** * Cache trait for PSR-16 compatible "Simple Cache" implementation * @package Grav\Framework\Cache @@ -19,6 +21,7 @@ abstract class AbstractCache implements CacheInterface /** * @param string $namespace * @param null|int|\DateInterval $defaultLifetime + * @throws InvalidArgumentException */ public function __construct($namespace = '', $defaultLifetime = null) { diff --git a/system/src/Grav/Framework/Cache/Adapter/SessionCache.php b/system/src/Grav/Framework/Cache/Adapter/SessionCache.php new file mode 100644 index 000000000..6a2b8ae53 --- /dev/null +++ b/system/src/Grav/Framework/Cache/Adapter/SessionCache.php @@ -0,0 +1,72 @@ +doGetStored($key); + + return $stored ? $stored[self::VALUE] : $miss; + } + + public function doSet($key, $value, $ttl) + { + $_SESSION[$this->getNamespace()][$key] = [self::VALUE => $value, self::LIFETIME => time() + $ttl]; + + return true; + } + + public function doDelete($key) + { + unset($_SESSION[$this->getNamespace()][$key]); + + return true; + } + + public function doClear() + { + $_SESSION[$this->getNamespace()] = []; + + return true; + } + + public function doHas($key) + { + return $this->doGetStored($key) !== null; + } + + public function getNamespace() + { + return 'cache-' . parent::getNamespace(); + } + + protected function doGetStored($key) + { + $stored = isset($_SESSION[$this->getNamespace()][$key]) ? $_SESSION[$this->getNamespace()][$key] : null; + + if ($stored && $stored[self::LIFETIME] < time()) { + unset($_SESSION[$this->getNamespace()][$key]); + $stored = null; + } + + return $stored ?: null; + } +} diff --git a/system/src/Grav/Framework/Cache/CacheTrait.php b/system/src/Grav/Framework/Cache/CacheTrait.php index 8be0165bd..2b1b937e7 100644 --- a/system/src/Grav/Framework/Cache/CacheTrait.php +++ b/system/src/Grav/Framework/Cache/CacheTrait.php @@ -36,6 +36,7 @@ trait CacheTrait * * @param string $namespace * @param null|int|\DateInterval $defaultLifetime + * @throws InvalidArgumentException */ protected function init($namespace = '', $defaultLifetime = null) { @@ -62,6 +63,7 @@ trait CacheTrait /** * @inheritdoc + * @throws InvalidArgumentException */ public function get($key, $default = null) { @@ -74,6 +76,7 @@ trait CacheTrait /** * @inheritdoc + * @throws InvalidArgumentException */ public function set($key, $value, $ttl = null) { @@ -87,6 +90,7 @@ trait CacheTrait /** * @inheritdoc + * @throws InvalidArgumentException */ public function delete($key) { @@ -145,6 +149,7 @@ trait CacheTrait /** * @inheritdoc + * @throws InvalidArgumentException */ public function setMultiple($values, $ttl = null) { @@ -175,6 +180,7 @@ trait CacheTrait /** * @inheritdoc + * @throws InvalidArgumentException */ public function deleteMultiple($keys) { @@ -200,6 +206,7 @@ trait CacheTrait /** * @inheritdoc + * @throws InvalidArgumentException */ public function has($key) { @@ -253,6 +260,7 @@ trait CacheTrait /** * @param string $key + * @throws InvalidArgumentException */ protected function validateKey($key) { @@ -290,6 +298,7 @@ trait CacheTrait * @param null|int|\DateInterval $ttl * @param bool $ignoreDefault Used internally inside $this->init(). * @return int|null + * @throws InvalidArgumentException */ protected function convertTtl($ttl, $ignoreDefault = false) {