1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class SessionCache |
4
|
|
|
* |
5
|
|
|
* @filesource SessionCache.php |
6
|
|
|
* @created 27.05.2017 |
7
|
|
|
* @package chillerlan\SimpleCache |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2017 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\SimpleCache; |
14
|
|
|
|
15
|
|
|
use chillerlan\Settings\SettingsContainerInterface; |
16
|
|
|
use Psr\Log\LoggerInterface; |
17
|
|
|
|
18
|
|
|
use function time; |
19
|
|
|
|
20
|
|
|
class SessionCache extends CacheDriverAbstract{ |
21
|
|
|
|
22
|
|
|
protected string $key; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* SessionCache constructor. |
26
|
|
|
* |
27
|
|
|
* @throws \Psr\SimpleCache\CacheException |
28
|
|
|
*/ |
29
|
|
|
public function __construct(SettingsContainerInterface $options = null, LoggerInterface $logger = null){ |
30
|
|
|
parent::__construct($options, $logger); |
31
|
|
|
|
32
|
|
|
$this->key = $this->options->cacheSessionkey; |
33
|
|
|
|
34
|
|
|
if(!is_string($this->key) || empty($this->key)){ |
|
|
|
|
35
|
|
|
throw new CacheException('invalid session cache key'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
$_SESSION[$this->key] = []; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** @inheritdoc */ |
43
|
|
|
public function get($key, $default = null){ |
44
|
|
|
$key = $this->checkKey($key); |
45
|
|
|
|
46
|
|
|
if(isset($_SESSION[$this->key][$key])){ |
47
|
|
|
|
48
|
|
|
if($_SESSION[$this->key][$key]['ttl'] === null || $_SESSION[$this->key][$key]['ttl'] > time()){ |
49
|
|
|
return $_SESSION[$this->key][$key]['content']; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
unset($_SESSION[$this->key][$key]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $default; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** @inheritdoc */ |
59
|
|
|
public function set($key, $value, $ttl = null):bool{ |
60
|
|
|
$ttl = $this->getTTL($ttl); |
61
|
|
|
|
62
|
|
|
$_SESSION[$this->key][$this->checkKey($key)] = [ |
63
|
|
|
'ttl' => $ttl ? time() + $ttl : null, |
64
|
|
|
'content' => $value, |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @inheritdoc */ |
71
|
|
|
public function delete($key):bool{ |
72
|
|
|
unset($_SESSION[$this->key][$this->checkKey($key)]); |
73
|
|
|
|
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** @inheritdoc */ |
78
|
|
|
public function clear():bool{ |
79
|
|
|
$_SESSION[$this->key] = []; |
80
|
|
|
|
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|