Skip to content

Commit

Permalink
Add new SessionCache class
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Aug 29, 2017
1 parent 0644eac commit a4795a9
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions system/src/Grav/Framework/Cache/AbstractCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
{
Expand Down
72 changes: 72 additions & 0 deletions system/src/Grav/Framework/Cache/Adapter/SessionCache.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
9 changes: 9 additions & 0 deletions system/src/Grav/Framework/Cache/CacheTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ trait CacheTrait
*
* @param string $namespace
* @param null|int|\DateInterval $defaultLifetime
* @throws InvalidArgumentException
*/
protected function init($namespace = '', $defaultLifetime = null)
{
Expand All @@ -62,6 +63,7 @@ protected function getDefaultLifetime()

/**
* @inheritdoc
* @throws InvalidArgumentException
*/
public function get($key, $default = null)
{
Expand All @@ -74,6 +76,7 @@ public function get($key, $default = null)

/**
* @inheritdoc
* @throws InvalidArgumentException
*/
public function set($key, $value, $ttl = null)
{
Expand All @@ -87,6 +90,7 @@ public function set($key, $value, $ttl = null)

/**
* @inheritdoc
* @throws InvalidArgumentException
*/
public function delete($key)
{
Expand Down Expand Up @@ -145,6 +149,7 @@ public function getMultiple($keys, $default = null)

/**
* @inheritdoc
* @throws InvalidArgumentException
*/
public function setMultiple($values, $ttl = null)
{
Expand Down Expand Up @@ -175,6 +180,7 @@ public function setMultiple($values, $ttl = null)

/**
* @inheritdoc
* @throws InvalidArgumentException
*/
public function deleteMultiple($keys)
{
Expand All @@ -200,6 +206,7 @@ public function deleteMultiple($keys)

/**
* @inheritdoc
* @throws InvalidArgumentException
*/
public function has($key)
{
Expand Down Expand Up @@ -253,6 +260,7 @@ abstract public function doHas($key);

/**
* @param string $key
* @throws InvalidArgumentException
*/
protected function validateKey($key)
{
Expand Down Expand Up @@ -290,6 +298,7 @@ protected function validateKeys($keys)
* @param null|int|\DateInterval $ttl
* @param bool $ignoreDefault Used internally inside $this->init().
* @return int|null
* @throws InvalidArgumentException
*/
protected function convertTtl($ttl, $ignoreDefault = false)
{
Expand Down

0 comments on commit a4795a9

Please sign in to comment.