1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is part of Blitz PHP framework. |
||
5 | * |
||
6 | * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view |
||
9 | * the LICENSE file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace BlitzPHP\Cache\Handlers; |
||
13 | |||
14 | use APCUIterator; |
||
15 | use DateInterval; |
||
16 | use RuntimeException; |
||
17 | |||
18 | /** |
||
19 | * Moteur de stockage APCu pour le cache |
||
20 | */ |
||
21 | class Apcu extends BaseHandler |
||
22 | { |
||
23 | /** |
||
24 | * Contient les noms de groupe compilés |
||
25 | * (préfixé par le préfixe de configuration global) |
||
26 | * |
||
27 | * @var string[] |
||
28 | */ |
||
29 | protected array $_compiledGroupNames = []; |
||
30 | |||
31 | /** |
||
32 | * {@inheritDoc} |
||
33 | */ |
||
34 | public function init(array $config = []): bool |
||
35 | { |
||
36 | if (! extension_loaded('apcu')) { |
||
37 | throw new RuntimeException('L\'extension `apcu` doit être activée pour utiliser ApcuHandler.'); |
||
38 | } |
||
39 | |||
40 | return parent::init($config); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * {@inheritDoc} |
||
45 | */ |
||
46 | public function set(string $key, mixed $value, null|DateInterval|int $ttl = null): bool |
||
47 | { |
||
48 | $key = $this->_key($key); |
||
49 | $duration = $this->duration($ttl); |
||
50 | |||
51 | return apcu_store($key, $value, $duration); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
52 | } |
||
53 | |||
54 | /** |
||
55 | * {@inheritDoc} |
||
56 | * |
||
57 | * @see https://secure.php.net/manual/en/function.apcu-fetch.php |
||
58 | */ |
||
59 | public function get(string $key, mixed $default = null): mixed |
||
60 | { |
||
61 | $value = apcu_fetch($this->_key($key), $success); |
||
62 | if ($success === false) { |
||
63 | return $default; |
||
64 | } |
||
65 | |||
66 | return $value; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * {@inheritDoc} |
||
71 | * |
||
72 | * @see https://secure.php.net/manual/en/function.apcu-inc.php |
||
73 | */ |
||
74 | public function increment(string $key, int $offset = 1) |
||
75 | { |
||
76 | $key = $this->_key($key); |
||
77 | |||
78 | return apcu_inc($key, $offset); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * {@inheritDoc} |
||
83 | * |
||
84 | * @see https://secure.php.net/manual/en/function.apcu-dec.php |
||
85 | */ |
||
86 | public function decrement(string $key, int $offset = 1) |
||
87 | { |
||
88 | $key = $this->_key($key); |
||
89 | |||
90 | return apcu_dec($key, $offset); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * {@inheritDoc} |
||
95 | * |
||
96 | * @see https://secure.php.net/manual/en/function.apcu-delete.php |
||
97 | */ |
||
98 | public function delete(string $key): bool |
||
99 | { |
||
100 | $key = $this->_key($key); |
||
101 | |||
102 | return apcu_delete($key); |
||
0 ignored issues
–
show
|
|||
103 | } |
||
104 | |||
105 | /** |
||
106 | * {@inheritDoc} |
||
107 | * |
||
108 | * @see https://secure.php.net/manual/en/function.apcu-cache-info.php |
||
109 | * @see https://secure.php.net/manual/en/function.apcu-delete.php |
||
110 | */ |
||
111 | public function clear(): bool |
||
112 | { |
||
113 | if (class_exists(APCUIterator::class, false)) { |
||
114 | $iterator = new APCUIterator( |
||
115 | '/^' . preg_quote($this->_config['prefix'], '/') . '/', |
||
116 | APC_ITER_NONE |
||
117 | ); |
||
118 | apcu_delete($iterator); |
||
119 | |||
120 | return true; |
||
121 | } |
||
122 | |||
123 | $cache = apcu_cache_info(); // Déclenche déjà un avertissement par lui-même |
||
124 | |||
125 | foreach ($cache['cache_list'] as $key) { |
||
126 | if (str_starts_with($key['info'], $this->_config['prefix'])) { |
||
127 | apcu_delete($key['info']); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | return true; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * {@inheritDoc} |
||
136 | * |
||
137 | * @see https://secure.php.net/manual/en/function.apcu-add.php |
||
138 | */ |
||
139 | public function add(string $key, mixed $value): bool |
||
140 | { |
||
141 | $key = $this->_key($key); |
||
142 | $duration = $this->_config['duration']; |
||
143 | |||
144 | return apcu_add($key, $value, $duration); |
||
0 ignored issues
–
show
|
|||
145 | } |
||
146 | |||
147 | /** |
||
148 | * {@inheritDoc} |
||
149 | * |
||
150 | * @see https://secure.php.net/manual/en/function.apcu-fetch.php |
||
151 | * @see https://secure.php.net/manual/en/function.apcu-store.php |
||
152 | */ |
||
153 | public function groups(): array |
||
154 | { |
||
155 | if (empty($this->_compiledGroupNames)) { |
||
156 | foreach ($this->_config['groups'] as $group) { |
||
157 | $this->_compiledGroupNames[] = $this->_config['prefix'] . $group; |
||
158 | } |
||
159 | } |
||
160 | |||
161 | $success = false; |
||
162 | $groups = apcu_fetch($this->_compiledGroupNames, $success); |
||
163 | if ($success && count($groups) !== count($this->_config['groups'])) { |
||
164 | foreach ($this->_compiledGroupNames as $group) { |
||
165 | if (! isset($groups[$group])) { |
||
166 | $value = 1; |
||
167 | if (apcu_store($group, $value) === false) { |
||
168 | $this->warning( |
||
169 | sprintf('Impossible de stocker la clé "%s" avec la valeur "%s" dans le cache APCu.', $group, $value) |
||
170 | ); |
||
171 | } |
||
172 | $groups[$group] = $value; |
||
173 | } |
||
174 | } |
||
175 | ksort($groups); |
||
176 | } |
||
177 | |||
178 | $result = []; |
||
179 | $groups = array_values($groups); |
||
180 | |||
181 | foreach ($this->_config['groups'] as $i => $group) { |
||
182 | $result[] = $group . $groups[$i]; |
||
183 | } |
||
184 | |||
185 | return $result; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * {@inheritDoc} |
||
190 | * |
||
191 | * @see https://secure.php.net/manual/en/function.apcu-inc.php |
||
192 | */ |
||
193 | public function clearGroup(string $group): bool |
||
194 | { |
||
195 | $success = false; |
||
196 | apcu_inc($this->_config['prefix'] . $group, 1, $success); |
||
197 | |||
198 | return $success; |
||
199 | } |
||
200 | } |
||
201 |