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\cache\driver; |
14
|
|
|
|
15
|
|
|
use think\cache\Driver; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Redis缓存驱动,适合单机部署、有前端代理实现高可用的场景,性能最好 |
19
|
|
|
* 有需要在业务层实现读写分离、或者使用RedisCluster的需求,请使用Redisd驱动 |
20
|
|
|
* |
21
|
|
|
* 要求安装phpredis扩展:https://github.com/nicolasff/phpredis |
22
|
|
|
* @author 尘缘 <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class Redis extends Driver |
25
|
|
|
{ |
26
|
|
|
/** @var \Predis\Client|\Redis */ |
|
|
|
|
27
|
|
|
protected $handler; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* 配置参数 |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $options = [ |
34
|
|
|
'host' => '127.0.0.1', |
35
|
|
|
'port' => 6379, |
36
|
|
|
'password' => '', |
37
|
|
|
'select' => 0, |
38
|
|
|
'timeout' => 0, |
39
|
|
|
'expire' => 0, |
40
|
|
|
'persistent' => false, |
41
|
|
|
'prefix' => '', |
42
|
|
|
'tag_prefix' => 'tag:', |
43
|
|
|
'serialize' => [], |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* 架构函数 |
48
|
|
|
* @access public |
49
|
|
|
* @param array $options 缓存参数 |
50
|
|
|
*/ |
51
|
|
|
public function __construct(array $options = []) |
52
|
|
|
{ |
53
|
|
|
if (!empty($options)) { |
54
|
|
|
$this->options = array_merge($this->options, $options); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (extension_loaded('redis')) { |
58
|
|
|
$this->handler = new \Redis; |
59
|
|
|
|
60
|
|
|
if ($this->options['persistent']) { |
61
|
|
|
$this->handler->pconnect($this->options['host'], (int) $this->options['port'], $this->options['timeout'], 'persistent_id_' . $this->options['select']); |
62
|
|
|
} else { |
63
|
|
|
$this->handler->connect($this->options['host'], (int) $this->options['port'], $this->options['timeout']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ('' != $this->options['password']) { |
67
|
|
|
$this->handler->auth($this->options['password']); |
68
|
|
|
} |
69
|
|
|
} elseif (class_exists('\Predis\Client')) { |
70
|
|
|
$params = []; |
71
|
|
|
foreach ($this->options as $key => $val) { |
72
|
|
|
if (in_array($key, ['aggregate', 'cluster', 'connections', 'exceptions', 'prefix', 'profile', 'replication', 'parameters'])) { |
73
|
|
|
$params[$key] = $val; |
74
|
|
|
unset($this->options[$key]); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ('' == $this->options['password']) { |
79
|
|
|
unset($this->options['password']); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->handler = new \Predis\Client($this->options, $params); |
83
|
|
|
|
84
|
|
|
$this->options['prefix'] = ''; |
85
|
|
|
} else { |
86
|
|
|
throw new \BadFunctionCallException('not support: redis'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (0 != $this->options['select']) { |
90
|
|
|
$this->handler->select($this->options['select']); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* 判断缓存 |
96
|
|
|
* @access public |
97
|
|
|
* @param string $name 缓存变量名 |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public function has($name): bool |
101
|
|
|
{ |
102
|
|
|
return $this->handler->exists($this->getCacheKey($name)) ? true : false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* 读取缓存 |
107
|
|
|
* @access public |
108
|
|
|
* @param string $name 缓存变量名 |
109
|
|
|
* @param mixed $default 默认值 |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
public function get($name, $default = null) |
113
|
|
|
{ |
114
|
|
|
$this->readTimes++; |
115
|
|
|
|
116
|
|
|
$value = $this->handler->get($this->getCacheKey($name)); |
117
|
|
|
|
118
|
|
|
if (false === $value) { |
119
|
|
|
return $default; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->unserialize($value); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* 写入缓存 |
127
|
|
|
* @access public |
128
|
|
|
* @param string $name 缓存变量名 |
129
|
|
|
* @param mixed $value 存储数据 |
130
|
|
|
* @param integer|\DateTime $expire 有效时间(秒) |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public function set($name, $value, $expire = null): bool |
134
|
|
|
{ |
135
|
|
|
$this->writeTimes++; |
136
|
|
|
|
137
|
|
|
if (is_null($expire)) { |
138
|
|
|
$expire = $this->options['expire']; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$key = $this->getCacheKey($name); |
142
|
|
|
$expire = $this->getExpireTime($expire); |
143
|
|
|
$value = $this->serialize($value); |
144
|
|
|
|
145
|
|
|
if ($expire) { |
146
|
|
|
$this->handler->setex($key, $expire, $value); |
147
|
|
|
} else { |
148
|
|
|
$this->handler->set($key, $value); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return true; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* 自增缓存(针对数值缓存) |
156
|
|
|
* @access public |
157
|
|
|
* @param string $name 缓存变量名 |
158
|
|
|
* @param int $step 步长 |
159
|
|
|
* @return false|int |
160
|
|
|
*/ |
161
|
|
|
public function inc(string $name, int $step = 1) |
162
|
|
|
{ |
163
|
|
|
$this->writeTimes++; |
164
|
|
|
|
165
|
|
|
$key = $this->getCacheKey($name); |
166
|
|
|
|
167
|
|
|
return $this->handler->incrby($key, $step); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* 自减缓存(针对数值缓存) |
172
|
|
|
* @access public |
173
|
|
|
* @param string $name 缓存变量名 |
174
|
|
|
* @param int $step 步长 |
175
|
|
|
* @return false|int |
176
|
|
|
*/ |
177
|
|
|
public function dec(string $name, int $step = 1) |
178
|
|
|
{ |
179
|
|
|
$this->writeTimes++; |
180
|
|
|
|
181
|
|
|
$key = $this->getCacheKey($name); |
182
|
|
|
|
183
|
|
|
return $this->handler->decrby($key, $step); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* 删除缓存 |
188
|
|
|
* @access public |
189
|
|
|
* @param string $name 缓存变量名 |
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
|
|
public function delete($name): bool |
193
|
|
|
{ |
194
|
|
|
$this->writeTimes++; |
195
|
|
|
|
196
|
|
|
$result = $this->handler->del($this->getCacheKey($name)); |
197
|
|
|
return $result > 0; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* 清除缓存 |
202
|
|
|
* @access public |
203
|
|
|
* @return bool |
204
|
|
|
*/ |
205
|
|
|
public function clear(): bool |
206
|
|
|
{ |
207
|
|
|
$this->writeTimes++; |
208
|
|
|
|
209
|
|
|
$this->handler->flushDB(); |
210
|
|
|
return true; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* 删除缓存标签 |
215
|
|
|
* @access public |
216
|
|
|
* @param array $keys 缓存标识列表 |
217
|
|
|
* @return void |
218
|
|
|
*/ |
219
|
|
|
public function clearTag(array $keys): void |
220
|
|
|
{ |
221
|
|
|
// 指定标签清除 |
222
|
|
|
$this->handler->del($keys); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* 追加(数组)缓存数据 |
227
|
|
|
* @access public |
228
|
|
|
* @param string $name 缓存标识 |
229
|
|
|
* @param mixed $value 数据 |
230
|
|
|
* @return void |
231
|
|
|
*/ |
232
|
|
|
public function push(string $name, $value): void |
233
|
|
|
{ |
234
|
|
|
$this->handler->sAdd($name, $value); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* 获取标签包含的缓存标识 |
239
|
|
|
* @access public |
240
|
|
|
* @param string $tag 缓存标签 |
241
|
|
|
* @return array |
242
|
|
|
*/ |
243
|
|
|
public function getTagItems(string $tag): array |
244
|
|
|
{ |
245
|
|
|
return $this->handler->sMembers($tag); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
} |
249
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths