1 | <?php |
||||
2 | declare(strict_types=1); |
||||
3 | |||||
4 | namespace MySQLReplication\Cache; |
||||
5 | |||||
6 | use Composer\InstalledVersions; |
||||
7 | use Psr\SimpleCache\CacheInterface; |
||||
8 | |||||
9 | // phpcs:ignoreFile |
||||
10 | 1 | if (class_exists(InstalledVersions::class) && version_compare(InstalledVersions::getVersion("psr/simple-cache"), '3.0.0', ">=")) { |
|||
11 | class ArrayCache implements CacheInterface |
||||
12 | { |
||||
13 | use ArrayCacheTrait; |
||||
14 | |||||
15 | /** |
||||
16 | * @inheritDoc |
||||
17 | */ |
||||
18 | public function get(string $key, $default = null): mixed |
||||
19 | { |
||||
20 | return $this->has($key) ? $this->tableMapCache[$key] : $default; |
||||
21 | } |
||||
22 | |||||
23 | /** |
||||
24 | * @inheritDoc |
||||
25 | */ |
||||
26 | public function getMultiple(iterable $keys, $default = null): iterable |
||||
27 | { |
||||
28 | $data = []; |
||||
29 | foreach ($keys as $key) { |
||||
30 | if ($this->has($key)) { |
||||
31 | $data[$key] = $this->tableMapCache[$key]; |
||||
32 | } |
||||
33 | } |
||||
34 | |||||
35 | return [] !== $data ? $data : $default; |
||||
36 | } |
||||
37 | } |
||||
38 | } else { |
||||
39 | class ArrayCache implements CacheInterface |
||||
40 | { |
||||
41 | use ArrayCacheTrait; |
||||
42 | |||||
43 | /** |
||||
44 | * @inheritDoc |
||||
45 | */ |
||||
46 | 58 | public function get($key, $default = null) |
|||
47 | { |
||||
48 | 58 | return $this->has($key) ? $this->tableMapCache[$key] : $default; |
|||
49 | } |
||||
50 | |||||
51 | /** |
||||
52 | * @inheritDoc |
||||
53 | */ |
||||
54 | 3 | public function getMultiple($keys, $default = null) |
|||
55 | { |
||||
56 | 3 | $data = []; |
|||
57 | 3 | foreach ($keys as $key) { |
|||
58 | 3 | if ($this->has($key)) { |
|||
59 | 3 | $data[$key] = $this->tableMapCache[$key]; |
|||
60 | } |
||||
61 | } |
||||
62 | |||||
63 | 3 | return [] !== $data ? $data : $default; |
|||
64 | } |
||||
65 | } |
||||
66 | } |
||||
67 | |||||
68 | trait ArrayCacheTrait |
||||
69 | { |
||||
70 | private $tableMapCache = []; |
||||
71 | |||||
72 | /** |
||||
73 | * @var int |
||||
74 | */ |
||||
75 | private $tableCacheSize; |
||||
76 | |||||
77 | 67 | public function __construct(int $tableCacheSize = 128) |
|||
78 | { |
||||
79 | 67 | $this->tableCacheSize = $tableCacheSize; |
|||
80 | } |
||||
81 | |||||
82 | |||||
83 | /** |
||||
84 | * @inheritDoc |
||||
85 | */ |
||||
86 | 63 | public function has($key): bool |
|||
87 | { |
||||
88 | 63 | return isset($this->tableMapCache[$key]); |
|||
89 | } |
||||
90 | |||||
91 | /** |
||||
92 | * @inheritDoc |
||||
93 | */ |
||||
94 | 2 | public function clear(): bool |
|||
95 | { |
||||
96 | 2 | $this->tableMapCache = []; |
|||
97 | |||||
98 | 2 | return true; |
|||
99 | } |
||||
100 | |||||
101 | /** |
||||
102 | * @inheritDoc |
||||
103 | */ |
||||
104 | 3 | public function setMultiple($values, $ttl = null): bool |
|||
0 ignored issues
–
show
|
|||||
105 | { |
||||
106 | 3 | foreach ($values as $key => $value) { |
|||
107 | 3 | $this->set($key, $value); |
|||
108 | } |
||||
109 | |||||
110 | 3 | return true; |
|||
111 | } |
||||
112 | |||||
113 | /** |
||||
114 | * @inheritDoc |
||||
115 | */ |
||||
116 | 63 | public function set($key, $value, $ttl = null): bool |
|||
0 ignored issues
–
show
The parameter
$ttl is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
117 | { |
||||
118 | // automatically clear table cache to save memory |
||||
119 | 63 | if (count($this->tableMapCache) > $this->tableCacheSize) { |
|||
120 | $this->tableMapCache = array_slice( |
||||
121 | $this->tableMapCache, |
||||
122 | (int)($this->tableCacheSize / 2), |
||||
123 | null, |
||||
124 | true |
||||
125 | ); |
||||
126 | } |
||||
127 | |||||
128 | 63 | $this->tableMapCache[$key] = $value; |
|||
129 | |||||
130 | 63 | return true; |
|||
131 | } |
||||
132 | |||||
133 | /** |
||||
134 | * @inheritDoc |
||||
135 | */ |
||||
136 | 1 | public function deleteMultiple($keys): bool |
|||
137 | { |
||||
138 | 1 | foreach ($keys as $key) { |
|||
139 | 1 | $this->delete($key); |
|||
140 | } |
||||
141 | |||||
142 | 1 | return true; |
|||
143 | } |
||||
144 | |||||
145 | /** |
||||
146 | * @inheritDoc |
||||
147 | */ |
||||
148 | 2 | public function delete($key): bool |
|||
149 | { |
||||
150 | 2 | unset($this->tableMapCache[$key]); |
|||
151 | |||||
152 | 2 | return true; |
|||
153 | } |
||||
154 | } |
||||
155 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.