Add new SessionCache class

This commit is contained in:
Matias Griese
2017-08-29 10:37:14 +03:00
parent 0644eac7dc
commit a4795a9df1
3 changed files with 84 additions and 0 deletions

View File

@@ -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)
{

View File

@@ -0,0 +1,72 @@
<?php
/**
* @package Grav\Framework\Cache
*
* @copyright Copyright (C) 2014 - 2017 RocketTheme, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/
namespace Grav\Framework\Cache\Adapter;
use Grav\Framework\Cache\AbstractCache;
/**
* Cache class for PSR-16 compatible "Simple Cache" implementation using session backend.
*
* @package Grav\Framework\Cache
*/
class SessionCache extends AbstractCache
{
const VALUE = 0;
const LIFETIME = 1;
public function doGet($key, $miss)
{
$stored = $this->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;
}
}

View File

@@ -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)
{