|
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 think\cache\CacheItem; |
|
18
|
|
|
use think\cache\Driver; |
|
19
|
|
|
use think\exception\InvalidArgumentException; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* 缓存管理类 |
|
23
|
|
|
* @mixin Driver |
|
24
|
|
|
*/ |
|
|
|
|
|
|
25
|
|
|
class Cache implements CacheItemPoolInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* 缓存队列 |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $data = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* 延期保存的缓存队列 |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $deferred = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* 缓存实例 |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $instance = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* 配置参数 |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $config = []; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* 架构函数 |
|
53
|
|
|
* @access public |
|
54
|
|
|
* @param array $config 配置参数 |
|
|
|
|
|
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct(array $config = []) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->config = $config; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public static function __make(Config $config) |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
return new static($config->get('cache')); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* 连接或者切换缓存 |
|
69
|
|
|
* @access public |
|
70
|
|
|
* @param string $name 连接配置名 |
|
71
|
|
|
* @param bool $force 强制重新连接 |
|
72
|
|
|
* @return Driver |
|
73
|
|
|
*/ |
|
74
|
|
|
public function store(string $name = '', bool $force = false): Driver |
|
75
|
|
|
{ |
|
76
|
|
|
if ('' == $name) { |
|
77
|
|
|
$name = $this->config['default'] ?? 'file'; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$options = $this->config['stores'][$name]; |
|
81
|
|
|
|
|
82
|
|
|
if ($force || !isset($this->instance[$name])) { |
|
83
|
|
|
$type = !empty($options['type']) ? $options['type'] : 'File'; |
|
84
|
|
|
|
|
85
|
|
|
$this->instance[$name] = App::factory($type, '\\think\\cache\\driver\\', $options); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $this->instance[$name]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* 设置配置 |
|
93
|
|
|
* @access public |
|
94
|
|
|
* @param array $config 配置参数 |
|
95
|
|
|
* @return void |
|
96
|
|
|
*/ |
|
97
|
|
|
public function config(array $config): void |
|
98
|
|
|
{ |
|
99
|
|
|
$this->config = array_merge($this->config, $config); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* 返回「键」对应的一个缓存项。 |
|
104
|
|
|
* @access public |
|
105
|
|
|
* @param string $key 缓存标识 |
|
106
|
|
|
* @return CacheItemInterface |
|
107
|
|
|
* @throws InvalidArgumentException |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getItem($key): CacheItem |
|
110
|
|
|
{ |
|
111
|
|
|
if (isset($this->data[$key])) { |
|
112
|
|
|
return $this->data[$key]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$cacheItem = new CacheItem($key); |
|
116
|
|
|
|
|
117
|
|
|
if ($this->has($key)) { |
|
|
|
|
|
|
118
|
|
|
$cacheItem->set($this->get($key)); |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$this->data[$key] = $cacheItem; |
|
122
|
|
|
|
|
123
|
|
|
return $cacheItem; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* 返回一个可供遍历的缓存项集合。 |
|
128
|
|
|
* @access public |
|
129
|
|
|
* @param array $keys |
|
|
|
|
|
|
130
|
|
|
* @return array|\Traversable |
|
131
|
|
|
* @throws InvalidArgumentException |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getItems(array $keys = []): array |
|
134
|
|
|
{ |
|
135
|
|
|
$result = []; |
|
136
|
|
|
foreach ($keys as $key) { |
|
137
|
|
|
$result[] = $this->getItem($key); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $result; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* 检查缓存系统中是否有「键」对应的缓存项。 |
|
145
|
|
|
* @access public |
|
146
|
|
|
* @param string $key |
|
|
|
|
|
|
147
|
|
|
* @return bool |
|
148
|
|
|
* @throws InvalidArgumentException |
|
149
|
|
|
*/ |
|
150
|
|
|
public function hasItem($key): bool |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->store()->has($key); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* 清空缓冲池 |
|
157
|
|
|
* @access public |
|
158
|
|
|
* @return bool |
|
159
|
|
|
*/ |
|
160
|
|
|
public function clear(): bool |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->store()->clear(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* 从缓冲池里移除某个缓存项 |
|
167
|
|
|
* @access public |
|
168
|
|
|
* @param string $key |
|
|
|
|
|
|
169
|
|
|
* @return bool |
|
170
|
|
|
* @throws InvalidArgumentException |
|
171
|
|
|
*/ |
|
172
|
|
|
public function deleteItem($key): bool |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->store()->delete($key); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* 从缓冲池里移除多个缓存项 |
|
179
|
|
|
* @access public |
|
180
|
|
|
* @param array $keys |
|
|
|
|
|
|
181
|
|
|
* @return bool |
|
182
|
|
|
* @throws InvalidArgumentException |
|
183
|
|
|
*/ |
|
184
|
|
|
public function deleteItems(array $keys): bool |
|
185
|
|
|
{ |
|
186
|
|
|
foreach ($keys as $key) { |
|
187
|
|
|
$this->store()->delete($key); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return true; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* 立刻为「CacheItemInterface」对象做数据持久化。 |
|
195
|
|
|
* @access public |
|
196
|
|
|
* @param CacheItemInterface $item |
|
|
|
|
|
|
197
|
|
|
* @return bool |
|
198
|
|
|
*/ |
|
199
|
|
|
public function save(CacheItemInterface $item): bool |
|
200
|
|
|
{ |
|
201
|
|
|
if ($item->getKey()) { |
|
202
|
|
|
return $this->store()->set($item->getKey(), $item->get(), $item->getExpire()); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return false; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* 稍后为「CacheItemInterface」对象做数据持久化。 |
|
210
|
|
|
* @access public |
|
211
|
|
|
* @param CacheItemInterface $item |
|
|
|
|
|
|
212
|
|
|
* @return bool |
|
213
|
|
|
*/ |
|
214
|
|
|
public function saveDeferred(CacheItemInterface $item): bool |
|
215
|
|
|
{ |
|
216
|
|
|
$this->deferred[$item->getKey()] = $item; |
|
217
|
|
|
return true; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* 提交所有的正在队列里等待的请求到数据持久层,配合 `saveDeferred()` 使用 |
|
222
|
|
|
* @access public |
|
223
|
|
|
* @return bool |
|
224
|
|
|
*/ |
|
225
|
|
|
public function commit(): bool |
|
226
|
|
|
{ |
|
227
|
|
|
foreach ($this->deferred as $key => $item) { |
|
228
|
|
|
$result = $this->save($item); |
|
229
|
|
|
unset($this->deferred[$key]); |
|
230
|
|
|
|
|
231
|
|
|
if (false === $result) { |
|
232
|
|
|
return false; |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
return true; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
public function __call($method, $args) |
|
|
|
|
|
|
239
|
|
|
{ |
|
240
|
|
|
return call_user_func_array([$this->store(), $method], $args); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
public function __destruct() |
|
|
|
|
|
|
244
|
|
|
{ |
|
245
|
|
|
if (!empty($this->deferred)) { |
|
246
|
|
|
$this->commit(); |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
} |
|
251
|
|
|
|