From ee266e719f0cf028365379c652746c7cb7517c34 Mon Sep 17 00:00:00 2001 From: Avior Date: Wed, 17 Apr 2019 21:15:04 +0200 Subject: [PATCH] Added session cache manager --- src/AdminPanel/Cache/SessionCache.php | 71 +++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/AdminPanel/Cache/SessionCache.php diff --git a/src/AdminPanel/Cache/SessionCache.php b/src/AdminPanel/Cache/SessionCache.php new file mode 100644 index 0000000..f2e451b --- /dev/null +++ b/src/AdminPanel/Cache/SessionCache.php @@ -0,0 +1,71 @@ +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]); + } +}