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 Memcached implements SessionHandler |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
protected $handler = null; |
21
|
|
|
protected $config = [ |
22
|
|
|
'host' => '127.0.0.1', // memcache主机 |
23
|
|
|
'port' => 11211, // memcache端口 |
24
|
|
|
'expire' => 3600, // session有效期 |
25
|
|
|
'timeout' => 0, // 连接超时时间(单位:毫秒) |
26
|
|
|
'prefix' => '', // session name (memcache key前缀) |
27
|
|
|
'username' => '', //账号 |
28
|
|
|
'password' => '', //密码 |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
public function __construct(array $config = []) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$this->config = array_merge($this->config, $config); |
34
|
|
|
|
35
|
|
|
$this->init(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Session初始化 |
40
|
|
|
* @access protected |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
|
|
protected function init(): bool |
44
|
|
|
{ |
45
|
|
|
// 检测php环境 |
46
|
|
|
if (!extension_loaded('memcached')) { |
47
|
|
|
throw new Exception('not support:memcached'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->handler = new \Memcached; |
51
|
|
|
|
52
|
|
|
// 设置连接超时时间(单位:毫秒) |
53
|
|
|
if ($this->config['timeout'] > 0) { |
54
|
|
|
$this->handler->setOption(\Memcached::OPT_CONNECT_TIMEOUT, $this->config['timeout']); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// 支持集群 |
58
|
|
|
$hosts = (array) $this->config['host']; |
59
|
|
|
$ports = (array) $this->config['port']; |
60
|
|
|
|
61
|
|
|
if (empty($ports[0])) { |
62
|
|
|
$ports[0] = 11211; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// 建立连接 |
66
|
|
|
$servers = []; |
67
|
|
|
foreach ($hosts as $i => $host) { |
68
|
|
|
$servers[] = [$host, $ports[$i] ?? $ports[0], 1]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->handler->addServers($servers); |
72
|
|
|
|
73
|
|
|
if ('' != $this->config['username']) { |
74
|
|
|
$this->handler->setOption(\Memcached::OPT_BINARY_PROTOCOL, true); |
75
|
|
|
$this->handler->setSaslAuthData($this->config['username'], $this->config['password']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* 读取Session |
83
|
|
|
* @access public |
84
|
|
|
* @param string $sessID |
|
|
|
|
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function read(string $sessID): string |
88
|
|
|
{ |
89
|
|
|
return (string) $this->handler->get($this->config['prefix'] . $sessID); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* 写入Session |
94
|
|
|
* @access public |
95
|
|
|
* @param string $sessID |
|
|
|
|
96
|
|
|
* @param array $data |
|
|
|
|
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function write(string $sessID, string $data): bool |
100
|
|
|
{ |
101
|
|
|
return $this->handler->set($this->config['prefix'] . $sessID, $data, $this->config['expire']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* 删除Session |
106
|
|
|
* @access public |
107
|
|
|
* @param string $sessID |
|
|
|
|
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public function delete(string $sessID): bool |
111
|
|
|
{ |
112
|
|
|
return $this->handler->delete($this->config['prefix'] . $sessID); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|