1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpWinTools\WmiScripting\Support\Cache; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Psr\SimpleCache\CacheInterface; |
7
|
|
|
use PhpWinTools\WmiScripting\Configuration\Config; |
8
|
|
|
use PhpWinTools\WmiScripting\Exceptions\CacheInvalidArgumentException; |
9
|
|
|
|
10
|
|
|
abstract class CacheDriver implements CacheInterface |
11
|
|
|
{ |
12
|
|
|
/** @var Config */ |
13
|
|
|
protected $config; |
14
|
|
|
|
15
|
|
|
/** @var mixed */ |
16
|
|
|
protected $store; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
protected $name = null; |
20
|
|
|
|
21
|
|
|
public function __construct(Config $config = null, string $name = null) |
22
|
|
|
{ |
23
|
|
|
$this->config = $config ?? Config::instance(); |
24
|
|
|
$this->name = $name ?? get_called_class(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $key |
29
|
|
|
* @param null|Closure|mixed $default |
30
|
|
|
* |
31
|
|
|
* @throws CacheInvalidArgumentException |
32
|
|
|
* |
33
|
|
|
* @return mixed |
34
|
|
|
*/ |
35
|
|
|
abstract public function get($key, $default = null); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param iterable $keys |
39
|
|
|
* @param null|Closure|mixed $default |
40
|
|
|
* |
41
|
|
|
* @throws CacheInvalidArgumentException |
42
|
|
|
* |
43
|
|
|
* @return iterable |
44
|
|
|
*/ |
45
|
|
|
abstract public function getMultiple($keys, $default = null); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $key |
49
|
|
|
* @param mixed $value |
50
|
|
|
* @param null|int|mixed $ttl |
51
|
|
|
* |
52
|
|
|
* @throws CacheInvalidArgumentException |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
abstract public function set($key, $value, $ttl = null); |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param iterable $values |
60
|
|
|
* @param null|int|mixed $ttl |
61
|
|
|
* |
62
|
|
|
* @throws CacheInvalidArgumentException |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
abstract public function setMultiple($values, $ttl = null); |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $key |
70
|
|
|
* |
71
|
|
|
* @throws CacheInvalidArgumentException |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
abstract public function delete($key); |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param iterable $keys |
79
|
|
|
* |
80
|
|
|
* @throws CacheInvalidArgumentException |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
abstract public function deleteMultiple($keys); |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $key |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
* |
92
|
|
|
* @throws CacheInvalidArgumentException |
93
|
|
|
*/ |
94
|
|
|
abstract public function has($key); |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $key |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
abstract public function expired($key): bool; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $key |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
public function notExpired($key) |
109
|
|
|
{ |
110
|
|
|
return $this->expired($key) === false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
abstract public function empty(): bool; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function notEmpty(): bool |
122
|
|
|
{ |
123
|
|
|
return $this->empty() === false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public function name(): string |
130
|
|
|
{ |
131
|
|
|
return $this->name; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $key |
136
|
|
|
* |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
public function canGet($key): bool |
140
|
|
|
{ |
141
|
|
|
return $this->isValidKey($key) && $this->has($key) && $this->notExpired($key); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $key |
146
|
|
|
* @param mixed $value |
147
|
|
|
* @param null|int|mixed $ttl |
148
|
|
|
* |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
|
|
public function canSet($key, $value, $ttl = null): bool |
152
|
|
|
{ |
153
|
|
|
return $this->isValidKey($key) && $this->isValidValue($value) && $this->isValidTtl($ttl); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $key |
158
|
|
|
* |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
public function canDelete($key): bool |
162
|
|
|
{ |
163
|
|
|
return $this->isValidKey($key) && $this->has($key); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $key |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
|
|
protected function isValidKey($key): bool |
172
|
|
|
{ |
173
|
|
|
return is_string($key); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param mixed $value |
178
|
|
|
* |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
|
|
protected function isValidValue($value): bool |
182
|
|
|
{ |
183
|
|
|
return $value === $value; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param null|int|mixed $ttl |
188
|
|
|
* |
189
|
|
|
* @return bool |
190
|
|
|
*/ |
191
|
|
|
protected function isValidTtl($ttl): bool |
192
|
|
|
{ |
193
|
|
|
return is_null($ttl) || is_int($ttl); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param mixed $key |
198
|
|
|
* |
199
|
|
|
* @throws CacheInvalidArgumentException |
200
|
|
|
* |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
|
|
protected function validateKey($key) |
204
|
|
|
{ |
205
|
|
|
if ($this->isValidKey($key)) { |
206
|
|
|
return $key; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
throw new CacheInvalidArgumentException("'{$this->coerceToString($key)}' is not a valid key."); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param mixed $value |
215
|
|
|
* |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
|
|
protected function coerceToString($value): string |
219
|
|
|
{ |
220
|
|
|
if (is_object($value)) { |
221
|
|
|
$value = get_class($value); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
if (is_array($value)) { |
225
|
|
|
$value = 'array'; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return (string) $value; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|