1
|
|
|
<?php |
2
|
|
|
// +---------------------------------------------------------------------- |
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
4
|
|
|
// +---------------------------------------------------------------------- |
5
|
|
|
// | Copyright (c) 2006~2021 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'], (int) $this->options['timeout'], 'persistent_id_' . $this->options['select']); |
62
|
|
|
} else { |
63
|
|
|
$this->handler->connect($this->options['host'], (int) $this->options['port'], (int) $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((int) $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
|
|
|
$key = $this->getCacheKey($name); |
116
|
|
|
$value = $this->handler->get($key); |
117
|
|
|
|
118
|
|
|
if (false === $value || is_null($value)) { |
119
|
|
|
return $default; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->unserialize($value); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* 模糊查询 |
127
|
|
|
* @access public |
128
|
|
|
* @param string $name 缓存变量名 |
129
|
|
|
* @return mixed |
130
|
|
|
*/ |
131
|
|
|
public function keys($name) |
132
|
|
|
{ |
133
|
|
|
$this->readTimes++; |
134
|
|
|
$value = $this->handler->keys($name); |
135
|
|
|
|
136
|
|
|
if (false === $value || is_null($value)) { |
137
|
|
|
return null; |
138
|
|
|
} |
139
|
|
|
return $value; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* 写入缓存 |
144
|
|
|
* @access public |
145
|
|
|
* @param string $name 缓存变量名 |
146
|
|
|
* @param mixed $value 存储数据 |
147
|
|
|
* @param integer|\DateTime $expire 有效时间(秒) |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function set($name, $value, $expire = null): bool |
151
|
|
|
{ |
152
|
|
|
$this->writeTimes++; |
153
|
|
|
|
154
|
|
|
if (is_null($expire)) { |
155
|
|
|
$expire = $this->options['expire']; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$key = $this->getCacheKey($name); |
159
|
|
|
$expire = $this->getExpireTime($expire); |
160
|
|
|
$value = $this->serialize($value); |
161
|
|
|
|
162
|
|
|
if ($expire) { |
163
|
|
|
$this->handler->setex($key, $expire, $value); |
164
|
|
|
} else { |
165
|
|
|
$this->handler->set($key, $value); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return true; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* 自增缓存(针对数值缓存) |
173
|
|
|
* @access public |
174
|
|
|
* @param string $name 缓存变量名 |
175
|
|
|
* @param int $step 步长 |
176
|
|
|
* @return false|int |
177
|
|
|
*/ |
178
|
|
|
public function inc(string $name, int $step = 1) |
179
|
|
|
{ |
180
|
|
|
$this->writeTimes++; |
181
|
|
|
$key = $this->getCacheKey($name); |
182
|
|
|
|
183
|
|
|
return $this->handler->incrby($key, $step); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* 自减缓存(针对数值缓存) |
188
|
|
|
* @access public |
189
|
|
|
* @param string $name 缓存变量名 |
190
|
|
|
* @param int $step 步长 |
191
|
|
|
* @return false|int |
192
|
|
|
*/ |
193
|
|
|
public function dec(string $name, int $step = 1) |
194
|
|
|
{ |
195
|
|
|
$this->writeTimes++; |
196
|
|
|
$key = $this->getCacheKey($name); |
197
|
|
|
|
198
|
|
|
return $this->handler->decrby($key, $step); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* 删除缓存 |
203
|
|
|
* @access public |
204
|
|
|
* @param string $name 缓存变量名 |
205
|
|
|
* @return bool |
206
|
|
|
*/ |
207
|
|
|
public function delete($name): bool |
208
|
|
|
{ |
209
|
|
|
$this->writeTimes++; |
210
|
|
|
|
211
|
|
|
$key = $this->getCacheKey($name); |
212
|
|
|
$result = $this->handler->del($key); |
213
|
|
|
return $result > 0; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* 清除缓存 |
218
|
|
|
* @access public |
219
|
|
|
* @return bool |
220
|
|
|
*/ |
221
|
|
|
public function clear(): bool |
222
|
|
|
{ |
223
|
|
|
$this->writeTimes++; |
224
|
|
|
$this->handler->flushDB(); |
225
|
|
|
return true; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* 删除缓存标签 |
230
|
|
|
* @access public |
231
|
|
|
* @param array $keys 缓存标识列表 |
232
|
|
|
* @return void |
233
|
|
|
*/ |
234
|
|
|
public function clearTag(array $keys): void |
235
|
|
|
{ |
236
|
|
|
// 指定标签清除 |
237
|
|
|
$this->handler->del($keys); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* 追加TagSet数据 |
242
|
|
|
* @access public |
243
|
|
|
* @param string $name 缓存标识 |
244
|
|
|
* @param mixed $value 数据 |
245
|
|
|
* @return void |
246
|
|
|
*/ |
247
|
|
|
public function append(string $name, $value): void |
248
|
|
|
{ |
249
|
|
|
$key = $this->getCacheKey($name); |
250
|
|
|
$this->handler->sAdd($key, $value); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* 获取标签包含的缓存标识 |
255
|
|
|
* @access public |
256
|
|
|
* @param string $tag 缓存标签 |
257
|
|
|
* @return array |
258
|
|
|
*/ |
259
|
|
|
public function getTagItems(string $tag): array |
260
|
|
|
{ |
261
|
|
|
$name = $this->getTagKey($tag); |
262
|
|
|
$key = $this->getCacheKey($name); |
263
|
|
|
return $this->handler->sMembers($key); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
} |
267
|
|
|
|
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