1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Valkyrja Framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Valkyrja\Cache\Tagger; |
15
|
|
|
|
16
|
|
|
use JsonException; |
17
|
|
|
use Override; |
|
|
|
|
18
|
|
|
use Valkyrja\Cache\Contract\Cache; |
19
|
|
|
use Valkyrja\Cache\Tagger\Contract\Tagger as Contract; |
20
|
|
|
use Valkyrja\Type\BuiltIn\Support\Arr; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Cache. |
24
|
|
|
* |
25
|
|
|
* @author Melech Mizrachi |
26
|
|
|
*/ |
27
|
|
|
class Tagger implements Contract |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* The tags. |
31
|
|
|
* |
32
|
|
|
* @var string[] |
33
|
|
|
*/ |
34
|
|
|
protected array $tags; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Tag constructor. |
38
|
|
|
* |
39
|
|
|
* @param Cache $adapter |
40
|
|
|
* @param string ...$tags |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
protected Cache $adapter, |
44
|
|
|
string ...$tags |
45
|
|
|
) { |
46
|
|
|
$this->tags = $tags; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritDoc |
51
|
|
|
*/ |
52
|
|
|
#[Override] |
53
|
|
|
public static function make(Cache $store, string ...$tags): static |
54
|
|
|
{ |
55
|
|
|
return new static($store, ...$tags); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritDoc |
60
|
|
|
* |
61
|
|
|
* @throws JsonException |
62
|
|
|
*/ |
63
|
|
|
#[Override] |
64
|
|
|
public function has(string $key): bool |
65
|
|
|
{ |
66
|
|
|
foreach ($this->tags as $tag) { |
67
|
|
|
if (isset($this->getKeys($tag)[$key]) && $this->adapter->has($key)) { |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritDoc |
77
|
|
|
* |
78
|
|
|
* @throws JsonException |
79
|
|
|
*/ |
80
|
|
|
#[Override] |
81
|
|
|
public function get(string $key): string|null |
82
|
|
|
{ |
83
|
|
|
foreach ($this->tags as $tag) { |
84
|
|
|
if (isset($this->getKeys($tag)[$key])) { |
85
|
|
|
return $this->adapter->get($key); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @inheritDoc |
94
|
|
|
* |
95
|
|
|
* @throws JsonException |
96
|
|
|
*/ |
97
|
|
|
#[Override] |
98
|
|
|
public function many(string ...$keys): array |
99
|
|
|
{ |
100
|
|
|
$items = []; |
101
|
|
|
|
102
|
|
|
foreach ($this->tags as $tag) { |
103
|
|
|
$cachedKeys = $this->getKeys($tag); |
104
|
|
|
|
105
|
|
|
foreach ($keys as $key) { |
106
|
|
|
if (isset($cachedKeys[$key])) { |
107
|
|
|
$value = $this->adapter->get($key); |
108
|
|
|
|
109
|
|
|
if ($value === null) { |
110
|
|
|
continue; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$items[] = $value; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $items; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @inheritDoc |
123
|
|
|
* |
124
|
|
|
* @throws JsonException |
125
|
|
|
*/ |
126
|
|
|
#[Override] |
127
|
|
|
public function put(string $key, string $value, int $minutes): void |
128
|
|
|
{ |
129
|
|
|
$this->tag($key); |
130
|
|
|
|
131
|
|
|
$this->adapter->put($key, $value, $minutes); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @inheritDoc |
136
|
|
|
* |
137
|
|
|
* @throws JsonException |
138
|
|
|
*/ |
139
|
|
|
#[Override] |
140
|
|
|
public function putMany(array $values, int $minutes): void |
141
|
|
|
{ |
142
|
|
|
foreach ($values as $key => $value) { |
143
|
|
|
$this->put($key, $value, $minutes); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @inheritDoc |
149
|
|
|
* |
150
|
|
|
* @throws JsonException |
151
|
|
|
*/ |
152
|
|
|
#[Override] |
153
|
|
|
public function increment(string $key, int $value = 1): int |
154
|
|
|
{ |
155
|
|
|
$this->tag($key); |
156
|
|
|
|
157
|
|
|
return $this->adapter->increment($key, $value); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @inheritDoc |
162
|
|
|
* |
163
|
|
|
* @throws JsonException |
164
|
|
|
*/ |
165
|
|
|
#[Override] |
166
|
|
|
public function decrement(string $key, int $value = 1): int |
167
|
|
|
{ |
168
|
|
|
$this->tag($key); |
169
|
|
|
|
170
|
|
|
return $this->adapter->decrement($key, $value); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @inheritDoc |
175
|
|
|
* |
176
|
|
|
* @throws JsonException |
177
|
|
|
*/ |
178
|
|
|
#[Override] |
179
|
|
|
public function forever(string $key, string $value): void |
180
|
|
|
{ |
181
|
|
|
$this->tag($key); |
182
|
|
|
|
183
|
|
|
$this->adapter->forever($key, $value); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @inheritDoc |
188
|
|
|
* |
189
|
|
|
* @throws JsonException |
190
|
|
|
*/ |
191
|
|
|
#[Override] |
192
|
|
|
public function forget(string $key): bool |
193
|
|
|
{ |
194
|
|
|
$this->untag($key); |
195
|
|
|
|
196
|
|
|
return $this->adapter->forget($key); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @inheritDoc |
201
|
|
|
* |
202
|
|
|
* @throws JsonException |
203
|
|
|
*/ |
204
|
|
|
#[Override] |
205
|
|
|
public function flush(): bool |
206
|
|
|
{ |
207
|
|
|
foreach ($this->tags as $tag) { |
208
|
|
|
foreach ($this->getKeys($tag) as $key) { |
209
|
|
|
$this->adapter->forget($key); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return true; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @inheritDoc |
218
|
|
|
* |
219
|
|
|
* @throws JsonException |
220
|
|
|
*/ |
221
|
|
|
#[Override] |
222
|
|
|
public function tag(string $key): static |
223
|
|
|
{ |
224
|
|
|
foreach ($this->tags as $tag) { |
225
|
|
|
$keys = $this->getKeys($tag); |
226
|
|
|
|
227
|
|
|
$keys[$key] = $key; |
228
|
|
|
|
229
|
|
|
$this->putKeys($tag, $keys); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return $this; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @inheritDoc |
237
|
|
|
* |
238
|
|
|
* @throws JsonException |
239
|
|
|
*/ |
240
|
|
|
#[Override] |
241
|
|
|
public function untag(string $key): static |
242
|
|
|
{ |
243
|
|
|
foreach ($this->tags as $tag) { |
244
|
|
|
$keys = $this->getKeys($tag); |
245
|
|
|
|
246
|
|
|
unset($keys[$key]); |
247
|
|
|
|
248
|
|
|
$this->putKeys($tag, $keys); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return $this; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @inheritDoc |
256
|
|
|
* |
257
|
|
|
* @throws JsonException |
258
|
|
|
*/ |
259
|
|
|
#[Override] |
260
|
|
|
public function tagMany(string ...$keys): static |
261
|
|
|
{ |
262
|
|
|
foreach ($keys as $key) { |
263
|
|
|
$this->tag($key); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return $this; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @inheritDoc |
271
|
|
|
* |
272
|
|
|
* @throws JsonException |
273
|
|
|
*/ |
274
|
|
|
#[Override] |
275
|
|
|
public function untagMany(string ...$keys): static |
276
|
|
|
{ |
277
|
|
|
foreach ($keys as $key) { |
278
|
|
|
$this->untag($key); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
return $this; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Get a tag. |
286
|
|
|
* |
287
|
|
|
* @param string $tag |
288
|
|
|
* |
289
|
|
|
* @throws JsonException |
290
|
|
|
* |
291
|
|
|
* @return string[] |
292
|
|
|
*/ |
293
|
|
|
protected function getKeys(string $tag): array |
294
|
|
|
{ |
295
|
|
|
$keysFromCache = $this->adapter->get($tag); |
296
|
|
|
|
297
|
|
|
if ($keysFromCache !== null && $keysFromCache !== '') { |
298
|
|
|
/** @var string[] $keys */ |
299
|
|
|
$keys = Arr::fromString($keysFromCache); |
300
|
|
|
|
301
|
|
|
return $keys; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
return []; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Put a tag. |
309
|
|
|
* |
310
|
|
|
* @param string $tag |
311
|
|
|
* @param string[] $keys |
312
|
|
|
* |
313
|
|
|
* @throws JsonException |
314
|
|
|
* |
315
|
|
|
* @return void |
316
|
|
|
*/ |
317
|
|
|
protected function putKeys(string $tag, array $keys): void |
318
|
|
|
{ |
319
|
|
|
$this->adapter->forever($tag, Arr::toString($keys)); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
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