1 | <?php |
||||||
2 | /* |
||||||
3 | * This file is part of the Koded package. |
||||||
4 | * |
||||||
5 | * (c) Mihail Binev <[email protected]> |
||||||
6 | * |
||||||
7 | * Please view the LICENSE distributed with this source code |
||||||
8 | * for the full copyright and license information. |
||||||
9 | */ |
||||||
10 | |||||||
11 | namespace Koded\Caching\Client; |
||||||
12 | |||||||
13 | use function array_keys; |
||||||
14 | use function Koded\Caching\filter_keys; |
||||||
15 | use function Koded\Caching\normalize_ttl; |
||||||
16 | |||||||
17 | /** |
||||||
18 | * Trait MultiplesTrait implements all "multi" operations |
||||||
19 | * separated for easy override in the specific cache classes. |
||||||
20 | * |
||||||
21 | */ |
||||||
22 | trait MultiplesTrait |
||||||
23 | { |
||||||
24 | 231 | public function getMultiple(iterable $keys, mixed $default = null): iterable |
|||||
25 | { |
||||||
26 | 231 | $filtered = filter_keys($keys, false); |
|||||
27 | 95 | return $this->internalMultiGet($filtered, $default); |
|||||
28 | } |
||||||
29 | |||||||
30 | 119 | public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool |
|||||
31 | { |
||||||
32 | 119 | $filtered = filter_keys($values, true); |
|||||
33 | 119 | $ttl = normalize_ttl($ttl ?? $this->ttl); |
|||||
34 | 119 | if ($ttl !== null && $ttl < 1) { |
|||||
35 | // All items are considered expired and must be deleted |
||||||
36 | 8 | return $this->deleteMultiple(array_keys($filtered)); |
|||||
37 | } |
||||||
38 | 111 | return $this->internalMultiSet($filtered, $ttl); |
|||||
39 | } |
||||||
40 | |||||||
41 | 31 | public function deleteMultiple(iterable $keys): bool |
|||||
42 | { |
||||||
43 | 31 | $filtered = filter_keys($keys, false); |
|||||
44 | 31 | if (empty($filtered)) { |
|||||
45 | 8 | return true; |
|||||
46 | } |
||||||
47 | 31 | return $this->internalMultiDelete($filtered); |
|||||
48 | } |
||||||
49 | |||||||
50 | /* |
||||||
51 | * |
||||||
52 | * Override in specific cache classes. |
||||||
53 | * |
||||||
54 | */ |
||||||
55 | |||||||
56 | 83 | private function internalMultiGet(array $keys, mixed $default): iterable |
|||||
57 | { |
||||||
58 | 83 | $cached = []; |
|||||
59 | 83 | foreach ($keys as $key) { |
|||||
60 | 83 | $cached[$key] = $this->get($key, $default); |
|||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
61 | } |
||||||
62 | 83 | return $cached; |
|||||
63 | } |
||||||
64 | |||||||
65 | 97 | private function internalMultiSet(array $values, ?int $ttl): bool |
|||||
66 | { |
||||||
67 | 97 | $cached = $count = 0; |
|||||
68 | 97 | foreach ($values as $key => $value) { |
|||||
69 | 97 | $count++; |
|||||
70 | 97 | $this->set($key, $value, $ttl) && ++$cached; |
|||||
0 ignored issues
–
show
It seems like
set() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
71 | } |
||||||
72 | 97 | return $count === $cached; |
|||||
73 | } |
||||||
74 | |||||||
75 | 27 | private function internalMultiDelete(array $keys): bool |
|||||
76 | { |
||||||
77 | 27 | $deleted = $count = 0; |
|||||
78 | 27 | foreach ($keys as $key) { |
|||||
79 | 27 | $count++; |
|||||
80 | 27 | $this->delete($key) && ++$deleted; |
|||||
0 ignored issues
–
show
The method
delete() does not exist on Koded\Caching\Client\MultiplesTrait . Did you maybe mean deleteMultiple() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
81 | } |
||||||
82 | 27 | return $count === $deleted; |
|||||
83 | } |
||||||
84 | } |
||||||
85 |