|
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; |
|
14
|
|
|
|
|
15
|
|
|
use Psr\Cache\CacheItemInterface; |
|
16
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
17
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
18
|
|
|
use think\cache\CacheItem; |
|
19
|
|
|
use think\cache\Driver; |
|
20
|
|
|
use think\Container; |
|
21
|
|
|
use think\exception\InvalidArgumentException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* 缓存管理类 |
|
25
|
|
|
* @mixin Driver |
|
26
|
|
|
*/ |
|
27
|
|
|
class Cache implements CacheItemPoolInterface, CacheInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* 缓存队列 |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $data = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* 延期保存的缓存队列 |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $deferred = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* 缓存实例 |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $instance = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* 配置参数 |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $config = []; |
|
52
|
|
|
|
|
53
|
|
|
public static function __make(Config $config) |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
$cache = new static(); |
|
56
|
|
|
$cache->config($config->get('cache')); |
|
|
|
|
|
|
57
|
|
|
return $cache; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* 连接或者切换缓存 |
|
62
|
|
|
* @access public |
|
63
|
|
|
* @param string $name 连接配置名 |
|
64
|
|
|
* @param bool $force 强制重新连接 |
|
65
|
|
|
* @return Driver |
|
66
|
|
|
*/ |
|
67
|
|
|
public function store(string $name = '', bool $force = false): Driver |
|
68
|
|
|
{ |
|
69
|
|
|
if ('' == $name) { |
|
70
|
|
|
$name = $this->config['default'] ?? 'file'; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if ($force || !isset($this->instance[$name])) { |
|
74
|
|
|
if (!isset($this->config['stores'][$name])) { |
|
75
|
|
|
throw new InvalidArgumentException('Undefined cache config:' . $name); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$options = $this->config['stores'][$name]; |
|
79
|
|
|
|
|
80
|
|
|
$this->instance[$name] = $this->connect($options); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->instance[$name]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* 连接缓存 |
|
88
|
|
|
* @access public |
|
89
|
|
|
* @param array $options 连接参数 |
|
90
|
|
|
* @param string $name 连接配置名 |
|
|
|
|
|
|
91
|
|
|
* @return Driver |
|
92
|
|
|
*/ |
|
93
|
|
|
public function connect(array $options, string $name = ''): Driver |
|
94
|
|
|
{ |
|
95
|
|
|
if ($name && isset($this->instance[$name])) { |
|
96
|
|
|
return $this->instance[$name]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$type = !empty($options['type']) ? $options['type'] : 'File'; |
|
100
|
|
|
|
|
101
|
|
|
$handler = Container::factory($type, '\\think\\cache\\driver\\', $options); |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
if ($name) { |
|
104
|
|
|
$this->instance[$name] = $handler; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $handler; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* 设置配置 |
|
112
|
|
|
* @access public |
|
113
|
|
|
* @param array $config 配置参数 |
|
114
|
|
|
* @return void |
|
115
|
|
|
*/ |
|
116
|
|
|
public function config(array $config): void |
|
117
|
|
|
{ |
|
118
|
|
|
$this->config = array_merge($this->config, $config); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* 返回「键」对应的一个缓存项。 |
|
123
|
|
|
* @access public |
|
124
|
|
|
* @param string $key 缓存标识 |
|
125
|
|
|
* @return CacheItemInterface |
|
126
|
|
|
* @throws InvalidArgumentException |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getItem($key): CacheItem |
|
129
|
|
|
{ |
|
130
|
|
|
if (isset($this->data[$key])) { |
|
131
|
|
|
return $this->data[$key]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$cacheItem = new CacheItem($key); |
|
135
|
|
|
|
|
136
|
|
|
if ($this->has($key)) { |
|
137
|
|
|
$cacheItem->set($this->get($key)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$this->data[$key] = $cacheItem; |
|
141
|
|
|
|
|
142
|
|
|
return $cacheItem; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* 返回一个可供遍历的缓存项集合。 |
|
147
|
|
|
* @access public |
|
148
|
|
|
* @param array $keys |
|
|
|
|
|
|
149
|
|
|
* @return array|\Traversable |
|
150
|
|
|
* @throws InvalidArgumentException |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getItems(array $keys = []): array |
|
153
|
|
|
{ |
|
154
|
|
|
$result = []; |
|
155
|
|
|
foreach ($keys as $key) { |
|
156
|
|
|
$result[] = $this->getItem($key); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $result; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* 检查缓存系统中是否有「键」对应的缓存项。 |
|
164
|
|
|
* @access public |
|
165
|
|
|
* @param string $key |
|
|
|
|
|
|
166
|
|
|
* @return bool |
|
167
|
|
|
* @throws InvalidArgumentException |
|
168
|
|
|
*/ |
|
169
|
|
|
public function hasItem($key): bool |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->store()->has($key); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* 清空缓冲池 |
|
176
|
|
|
* @access public |
|
177
|
|
|
* @return bool |
|
178
|
|
|
*/ |
|
179
|
|
|
public function clear(): bool |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->store()->clear(); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* 从缓冲池里移除某个缓存项 |
|
186
|
|
|
* @access public |
|
187
|
|
|
* @param string $key |
|
|
|
|
|
|
188
|
|
|
* @return bool |
|
189
|
|
|
* @throws InvalidArgumentException |
|
190
|
|
|
*/ |
|
191
|
|
|
public function deleteItem($key): bool |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->store()->delete($key); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* 从缓冲池里移除多个缓存项 |
|
198
|
|
|
* @access public |
|
199
|
|
|
* @param array $keys |
|
|
|
|
|
|
200
|
|
|
* @return bool |
|
201
|
|
|
* @throws InvalidArgumentException |
|
202
|
|
|
*/ |
|
203
|
|
|
public function deleteItems(array $keys): bool |
|
204
|
|
|
{ |
|
205
|
|
|
foreach ($keys as $key) { |
|
206
|
|
|
$this->store()->delete($key); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
return true; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* 立刻为「CacheItemInterface」对象做数据持久化。 |
|
214
|
|
|
* @access public |
|
215
|
|
|
* @param CacheItemInterface $item |
|
|
|
|
|
|
216
|
|
|
* @return bool |
|
217
|
|
|
*/ |
|
218
|
|
|
public function save(CacheItemInterface $item): bool |
|
219
|
|
|
{ |
|
220
|
|
|
if ($item->getKey()) { |
|
221
|
|
|
return $this->store()->set($item->getKey(), $item->get(), $item->getExpire()); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
return false; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* 稍后为「CacheItemInterface」对象做数据持久化。 |
|
229
|
|
|
* @access public |
|
230
|
|
|
* @param CacheItemInterface $item |
|
|
|
|
|
|
231
|
|
|
* @return bool |
|
232
|
|
|
*/ |
|
233
|
|
|
public function saveDeferred(CacheItemInterface $item): bool |
|
234
|
|
|
{ |
|
235
|
|
|
$this->deferred[$item->getKey()] = $item; |
|
236
|
|
|
return true; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* 提交所有的正在队列里等待的请求到数据持久层,配合 `saveDeferred()` 使用 |
|
241
|
|
|
* @access public |
|
242
|
|
|
* @return bool |
|
243
|
|
|
*/ |
|
244
|
|
|
public function commit(): bool |
|
245
|
|
|
{ |
|
246
|
|
|
foreach ($this->deferred as $key => $item) { |
|
247
|
|
|
$result = $this->save($item); |
|
248
|
|
|
unset($this->deferred[$key]); |
|
249
|
|
|
|
|
250
|
|
|
if (false === $result) { |
|
251
|
|
|
return false; |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
return true; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
|
|
|
|
|
258
|
|
|
* 读取缓存 |
|
259
|
|
|
* @access public |
|
260
|
|
|
* @param string $name 缓存变量名 |
|
|
|
|
|
|
261
|
|
|
* @param mixed $default 默认值 |
|
262
|
|
|
* @return mixed |
|
263
|
|
|
*/ |
|
264
|
|
|
public function get($key, $default = false) |
|
265
|
|
|
{ |
|
266
|
|
|
return $this->store()->get($key, $default); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
|
|
|
|
|
270
|
|
|
* 写入缓存 |
|
271
|
|
|
* @access public |
|
272
|
|
|
* @param string $name 缓存变量名 |
|
|
|
|
|
|
273
|
|
|
* @param mixed $value 存储数据 |
|
274
|
|
|
* @param int|\DateTime $expire 有效时间 0为永久 |
|
|
|
|
|
|
275
|
|
|
* @return bool |
|
276
|
|
|
*/ |
|
277
|
|
|
public function set($key, $value, $ttl = null): bool |
|
278
|
|
|
{ |
|
279
|
|
|
return $this->store()->set($key, $value, $ttl); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
|
|
|
|
|
283
|
|
|
* 删除缓存 |
|
284
|
|
|
* @access public |
|
285
|
|
|
* @param string $name 缓存变量名 |
|
|
|
|
|
|
286
|
|
|
* @return bool |
|
287
|
|
|
*/ |
|
288
|
|
|
public function delete($key): bool |
|
289
|
|
|
{ |
|
290
|
|
|
return $this->store()->delete($key); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* 读取缓存 |
|
295
|
|
|
* @access public |
|
296
|
|
|
* @param iterable $keys 缓存变量名 |
|
|
|
|
|
|
297
|
|
|
* @param mixed $default 默认值 |
|
298
|
|
|
* @return iterable |
|
299
|
|
|
* @throws InvalidArgumentException |
|
300
|
|
|
*/ |
|
301
|
|
|
public function getMultiple($keys, $default = null): iterable |
|
302
|
|
|
{ |
|
303
|
|
|
return $this->store()->getMultiple($key, $default); |
|
|
|
|
|
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* 写入缓存 |
|
308
|
|
|
* @access public |
|
309
|
|
|
* @param iterable $values 缓存数据 |
|
310
|
|
|
* @param null|int|\DateInterval $ttl 有效时间 0为永久 |
|
311
|
|
|
* @return bool |
|
312
|
|
|
*/ |
|
313
|
|
|
public function setMultiple($values, $ttl = null): bool |
|
314
|
|
|
{ |
|
315
|
|
|
return $this->store()->setMultiple($values, $ttl); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* 删除缓存 |
|
320
|
|
|
* @access public |
|
321
|
|
|
* @param iterable $keys 缓存变量名 |
|
322
|
|
|
* @return bool |
|
323
|
|
|
* @throws InvalidArgumentException |
|
324
|
|
|
*/ |
|
325
|
|
|
public function deleteMultiple($keys): bool |
|
326
|
|
|
{ |
|
327
|
|
|
return $this->store()->deleteMultiple($keys); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
/** |
|
|
|
|
|
|
331
|
|
|
* 判断缓存是否存在 |
|
332
|
|
|
* @access public |
|
333
|
|
|
* @param string $name 缓存变量名 |
|
|
|
|
|
|
334
|
|
|
* @return bool |
|
335
|
|
|
*/ |
|
336
|
|
|
public function has($key): bool |
|
337
|
|
|
{ |
|
338
|
|
|
return $this->store()->has($key); |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
public function __call($method, $args) |
|
|
|
|
|
|
342
|
|
|
{ |
|
343
|
|
|
return call_user_func_array([$this->store(), $method], $args); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
public function __destruct() |
|
|
|
|
|
|
347
|
|
|
{ |
|
348
|
|
|
if (!empty($this->deferred)) { |
|
349
|
|
|
$this->commit(); |
|
350
|
|
|
} |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
} |
|
354
|
|
|
|