|
1
|
|
|
<?php |
|
2
|
|
|
// +---------------------------------------------------------------------- |
|
|
|
|
|
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
|
4
|
|
|
// +---------------------------------------------------------------------- |
|
5
|
|
|
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. |
|
6
|
|
|
// +---------------------------------------------------------------------- |
|
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
|
8
|
|
|
// +---------------------------------------------------------------------- |
|
9
|
|
|
// | Author: liu21st <[email protected]> |
|
10
|
|
|
// +---------------------------------------------------------------------- |
|
11
|
|
|
declare (strict_types = 1); |
|
12
|
|
|
|
|
13
|
|
|
namespace think\session\driver; |
|
14
|
|
|
|
|
15
|
|
|
use think\Exception; |
|
16
|
|
|
use think\session\SessionHandler; |
|
17
|
|
|
|
|
18
|
|
|
class Redis implements SessionHandler |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
/** @var \Redis */ |
|
|
|
|
|
|
21
|
|
|
protected $handler = null; |
|
22
|
|
|
protected $config = [ |
|
23
|
|
|
'host' => '127.0.0.1', // redis主机 |
|
24
|
|
|
'port' => 6379, // redis端口 |
|
25
|
|
|
'password' => '', // 密码 |
|
26
|
|
|
'select' => 0, // 操作库 |
|
27
|
|
|
'expire' => 3600, // 有效期(秒) |
|
28
|
|
|
'timeout' => 0, // 超时时间(秒) |
|
29
|
|
|
'persistent' => true, // 是否长连接 |
|
30
|
|
|
'name' => '', // session key前缀 |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(array $config = []) |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
$this->config = array_merge($this->config, $config); |
|
36
|
|
|
|
|
37
|
|
|
$this->init(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* 打开Session |
|
42
|
|
|
* @access public |
|
43
|
|
|
* @return bool |
|
44
|
|
|
* @throws Exception |
|
45
|
|
|
*/ |
|
46
|
|
|
public function init(): bool |
|
47
|
|
|
{ |
|
48
|
|
|
if (extension_loaded('redis')) { |
|
49
|
|
|
$this->handler = new \Redis; |
|
50
|
|
|
|
|
51
|
|
|
// 建立连接 |
|
52
|
|
|
$func = $this->config['persistent'] ? 'pconnect' : 'connect'; |
|
53
|
|
|
$this->handler->$func($this->config['host'], $this->config['port'], $this->config['timeout']); |
|
54
|
|
|
|
|
55
|
|
|
if ('' != $this->config['password']) { |
|
56
|
|
|
$this->handler->auth($this->config['password']); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (0 != $this->config['select']) { |
|
60
|
|
|
$this->handler->select($this->config['select']); |
|
61
|
|
|
} |
|
62
|
|
|
} elseif (class_exists('\Predis\Client')) { |
|
63
|
|
|
$params = []; |
|
64
|
|
|
foreach ($this->config as $key => $val) { |
|
65
|
|
|
if (in_array($key, ['aggregate', 'cluster', 'connections', 'exceptions', 'prefix', 'profile', 'replication'])) { |
|
66
|
|
|
$params[$key] = $val; |
|
67
|
|
|
unset($this->config[$key]); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
$this->handler = new \Predis\Client($this->config, $params); |
|
|
|
|
|
|
71
|
|
|
} else { |
|
72
|
|
|
throw new \BadFunctionCallException('not support: redis'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* 读取Session |
|
80
|
|
|
* @access public |
|
81
|
|
|
* @param string $sessID |
|
|
|
|
|
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
public function read(string $sessID): array |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->handler->get($this->config['prefix'] . $sessID); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* 写入Session |
|
91
|
|
|
* @access public |
|
92
|
|
|
* @param string $sessID |
|
|
|
|
|
|
93
|
|
|
* @param array $data |
|
|
|
|
|
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
|
|
public function write(string $sessID, array $data): bool |
|
97
|
|
|
{ |
|
98
|
|
|
if ($this->config['expire'] > 0) { |
|
99
|
|
|
$result = $this->handler->setex($this->config['prefix'] . $sessID, $this->config['expire'], $data); |
|
|
|
|
|
|
100
|
|
|
} else { |
|
101
|
|
|
$result = $this->handler->set($this->config['prefix'] . $sessID, $data); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $result ? true : false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* 删除Session |
|
109
|
|
|
* @access public |
|
110
|
|
|
* @param string $sessID |
|
|
|
|
|
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
|
|
public function delete(string $sessID): bool |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->handler->delete($this->config['prefix'] . $sessID) > 0; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|