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\SimpleCache\CacheInterface; |
16
|
|
|
use think\cache\Driver; |
17
|
|
|
use think\exception\InvalidArgumentException; |
18
|
|
|
use think\helper\Arr; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* 缓存管理类 |
22
|
|
|
* @mixin Driver |
23
|
|
|
* @mixin \think\cache\driver\File |
24
|
|
|
*/ |
25
|
|
|
class Cache extends Manager implements CacheInterface |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
protected $namespace = '\\think\\cache\\driver\\'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* 默认驱动 |
32
|
|
|
* @return array|string |
33
|
|
|
*/ |
34
|
2 |
|
public function getDefaultDriver(): string |
35
|
|
|
{ |
36
|
2 |
|
return $this->getConfig('default'); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* 获取缓存配置 |
41
|
|
|
* @access public |
42
|
|
|
* @param null|string $name 名称 |
43
|
|
|
* @param mixed $default 默认值 |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
4 |
|
public function getConfig(string $name = null, $default = null) |
47
|
|
|
{ |
48
|
4 |
|
if (!is_null($name)) { |
49
|
4 |
|
return $this->app->config->get('cache.' . $name, $default); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
return $this->app->config->get('cache'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* 获取驱动配置 |
57
|
|
|
* @param string $store |
|
|
|
|
58
|
|
|
* @param string $name |
|
|
|
|
59
|
|
|
* @param null $default |
|
|
|
|
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
4 |
|
public function getStoreConfig(string $store, string $name = null, $default = null) |
63
|
|
|
{ |
64
|
4 |
|
if ($config = $this->getConfig("stores.{$store}")) { |
65
|
3 |
|
return Arr::get($config, $name, $default); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
throw new \InvalidArgumentException("Store [$store] not found."); |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
protected function resolveType(string $name) |
|
|
|
|
72
|
|
|
{ |
73
|
3 |
|
return $this->getStoreConfig($name, 'type', 'file'); |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
protected function resolveConfig(string $name) |
|
|
|
|
77
|
|
|
{ |
78
|
3 |
|
return $this->getStoreConfig($name); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* 连接或者切换缓存 |
83
|
|
|
* @access public |
84
|
|
|
* @param string $name 连接配置名 |
85
|
|
|
* @return Driver |
86
|
|
|
*/ |
87
|
3 |
|
public function store(string $name = null): Driver |
88
|
|
|
{ |
89
|
3 |
|
return $this->driver($name); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* 清空缓冲池 |
94
|
|
|
* @access public |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
2 |
|
public function clear(): bool |
98
|
|
|
{ |
99
|
2 |
|
return $this->store()->clear(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* 读取缓存 |
104
|
|
|
* @access public |
105
|
|
|
* @param string $key 缓存变量名 |
106
|
|
|
* @param mixed $default 默认值 |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
2 |
|
public function get($key, $default = null) |
110
|
|
|
{ |
111
|
2 |
|
return $this->store()->get($key, $default); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* 写入缓存 |
116
|
|
|
* @access public |
117
|
|
|
* @param string $key 缓存变量名 |
118
|
|
|
* @param mixed $value 存储数据 |
119
|
|
|
* @param int|\DateTime $ttl 有效时间 0为永久 |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
2 |
|
public function set($key, $value, $ttl = null): bool |
123
|
|
|
{ |
124
|
2 |
|
return $this->store()->set($key, $value, $ttl); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* 删除缓存 |
129
|
|
|
* @access public |
130
|
|
|
* @param string $key 缓存变量名 |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
2 |
|
public function delete($key): bool |
134
|
|
|
{ |
135
|
2 |
|
return $this->store()->delete($key); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* 读取缓存 |
140
|
|
|
* @access public |
141
|
|
|
* @param iterable $keys 缓存变量名 |
142
|
|
|
* @param mixed $default 默认值 |
143
|
|
|
* @return iterable |
144
|
|
|
* @throws InvalidArgumentException |
145
|
|
|
*/ |
146
|
|
|
public function getMultiple($keys, $default = null): iterable |
147
|
|
|
{ |
148
|
|
|
return $this->store()->getMultiple($keys, $default); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* 写入缓存 |
153
|
|
|
* @access public |
154
|
|
|
* @param iterable $values 缓存数据 |
155
|
|
|
* @param null|int|\DateInterval $ttl 有效时间 0为永久 |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
public function setMultiple($values, $ttl = null): bool |
159
|
|
|
{ |
160
|
|
|
return $this->store()->setMultiple($values, $ttl); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* 删除缓存 |
165
|
|
|
* @access public |
166
|
|
|
* @param iterable $keys 缓存变量名 |
167
|
|
|
* @return bool |
168
|
|
|
* @throws InvalidArgumentException |
169
|
|
|
*/ |
170
|
|
|
public function deleteMultiple($keys): bool |
171
|
|
|
{ |
172
|
|
|
return $this->store()->deleteMultiple($keys); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* 判断缓存是否存在 |
177
|
|
|
* @access public |
178
|
|
|
* @param string $key 缓存变量名 |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
1 |
|
public function has($key): bool |
182
|
|
|
{ |
183
|
1 |
|
return $this->store()->has($key); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
} |
187
|
|
|
|