-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters