-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisSessionManager.php
More file actions
90 lines (80 loc) · 2.15 KB
/
RedisSessionManager.php
File metadata and controls
90 lines (80 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
require_once 'BaseSessionManager.php';
/**
* Class for accessing to redis session storage
*/
class RedisSessionManager extends BaseSessionManager
{
private $host;
private $port;
private $conn;
public function connect(array $params)
{
if (empty($params['host'])) {
throw new InvalidArgumentException('host not found');
}
if (empty($params['port'])) {
throw new InvalidArgumentException('port not found');
}
$this->host = $params['host'];
$this->port = $params['port'];
$this->conn = new Redis();
$result = $this->conn->connect($this->host, $this->port);
if (!$result) {
throw new Exception('error connect to redis server');
}
}
public function disconnect()
{
unset($this->conn);
}
public function get($key)
{
$key = strval($key);
if (empty($key)) {
throw new InvalidArgumentException('key is empty');
}
$full_key = $this->getPrefix() . $key;
return $this->conn->get($full_key);
}
public function set($key, $value)
{
$key = strval($key);
if (empty($key)) {
throw new InvalidArgumentException('key is empty');
}
$full_key = $this->getPrefix() . $key;
$this->conn->set($full_key, $value);
}
public function delete($key)
{
$key = strval($key);
if (empty($key)) {
throw new InvalidArgumentException('key is empty');
}
$full_key = $this->getPrefix() . $key;
$this->conn->delete($full_key);
}
public function deleteAll()
{
$keys = $this->getAllKeys();
$count = 0;
foreach ($keys as $key) {
$this->delete($key);
$count++;
}
return $count;
}
public function getAllKeys()
{
$keys = $this->conn->getKeys($this->getPrefix() . '*');
if (!$keys) {
return array();
}
$ret = array();
foreach ($keys as $k) {
$ret[] = str_replace($this->getPrefix(), '', $k);
}
return $ret;
}
}