mirror of
https://github.com/Aviortheking/DeltaCMS.git
synced 2025-04-23 19:32:13 +00:00
Added session cache manager
This commit is contained in:
parent
caead40204
commit
ee266e719f
71
src/AdminPanel/Cache/SessionCache.php
Normal file
71
src/AdminPanel/Cache/SessionCache.php
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace AdminPanel\Cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Session Cache (different type of cache because is per-user cache)
|
||||||
|
* ttl can be overriden by cookie clearing
|
||||||
|
*
|
||||||
|
* @SuppressWarnings(PHPMD.Superglobals)
|
||||||
|
*/
|
||||||
|
class SessionCache extends AbstractCache
|
||||||
|
{
|
||||||
|
private $ttl = 86400;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
if (session_status() == PHP_SESSION_NONE) {
|
||||||
|
session_start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get($key, $default = null)
|
||||||
|
{
|
||||||
|
if (!$this->checkKey($key)) {
|
||||||
|
throw new InvalidArgumentException("Invalid Key");
|
||||||
|
}
|
||||||
|
if (isset($_SESSION[$key])) {
|
||||||
|
$item = $_SESSION[$key];
|
||||||
|
if ($item["ttl"] > time() && $item["value"] !== null) {
|
||||||
|
return $item["value"];
|
||||||
|
} else {
|
||||||
|
$this->delete($key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set($key, $value, $ttl = null)
|
||||||
|
{
|
||||||
|
if (!$this->checkKey($key)) {
|
||||||
|
throw new InvalidArgumentException("Invalid Key");
|
||||||
|
}
|
||||||
|
$_SESSION[$key] = array(
|
||||||
|
"value" => $value,
|
||||||
|
"ttl" => time() + ($ttl != null ? $this->getTTL($ttl) : $this->ttl)
|
||||||
|
);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function delete($key)
|
||||||
|
{
|
||||||
|
if (!$this->checkKey($key)) {
|
||||||
|
throw new InvalidArgumentException("Invalid Key");
|
||||||
|
}
|
||||||
|
$_SESSION[$key] = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function clear()
|
||||||
|
{
|
||||||
|
return session_reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function has($key)
|
||||||
|
{
|
||||||
|
if (!$this->checkKey($key)) {
|
||||||
|
throw new InvalidArgumentException("Invalid Key");
|
||||||
|
}
|
||||||
|
return isset($_SESSION[$key]) && !empty($_SESSION[$key]);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user