diff options
Diffstat (limited to 'vendor/symfony/cache/Adapter/ArrayAdapter.php')
-rw-r--r-- | vendor/symfony/cache/Adapter/ArrayAdapter.php | 359 |
1 files changed, 359 insertions, 0 deletions
diff --git a/vendor/symfony/cache/Adapter/ArrayAdapter.php b/vendor/symfony/cache/Adapter/ArrayAdapter.php new file mode 100644 index 0000000..0f1c49d --- /dev/null +++ b/vendor/symfony/cache/Adapter/ArrayAdapter.php | |||
@@ -0,0 +1,359 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
7 | * | ||
8 | * For the full copyright and license information, please view the LICENSE | ||
9 | * file that was distributed with this source code. | ||
10 | */ | ||
11 | |||
12 | namespace Symfony\Component\Cache\Adapter; | ||
13 | |||
14 | use Psr\Cache\CacheItemInterface; | ||
15 | use Psr\Log\LoggerAwareInterface; | ||
16 | use Psr\Log\LoggerAwareTrait; | ||
17 | use Symfony\Component\Cache\CacheItem; | ||
18 | use Symfony\Component\Cache\Exception\InvalidArgumentException; | ||
19 | use Symfony\Component\Cache\ResettableInterface; | ||
20 | use Symfony\Contracts\Cache\CacheInterface; | ||
21 | |||
22 | /** | ||
23 | * An in-memory cache storage. | ||
24 | * | ||
25 | * Acts as a least-recently-used (LRU) storage when configured with a maximum number of items. | ||
26 | * | ||
27 | * @author Nicolas Grekas <p@tchwork.com> | ||
28 | */ | ||
29 | class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface | ||
30 | { | ||
31 | use LoggerAwareTrait; | ||
32 | |||
33 | private array $values = []; | ||
34 | private array $tags = []; | ||
35 | private array $expiries = []; | ||
36 | |||
37 | private static \Closure $createCacheItem; | ||
38 | |||
39 | /** | ||
40 | * @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise | ||
41 | */ | ||
42 | public function __construct( | ||
43 | private int $defaultLifetime = 0, | ||
44 | private bool $storeSerialized = true, | ||
45 | private float $maxLifetime = 0, | ||
46 | private int $maxItems = 0, | ||
47 | ) { | ||
48 | if (0 > $maxLifetime) { | ||
49 | throw new InvalidArgumentException(sprintf('Argument $maxLifetime must be positive, %F passed.', $maxLifetime)); | ||
50 | } | ||
51 | |||
52 | if (0 > $maxItems) { | ||
53 | throw new InvalidArgumentException(sprintf('Argument $maxItems must be a positive integer, %d passed.', $maxItems)); | ||
54 | } | ||
55 | |||
56 | self::$createCacheItem ??= \Closure::bind( | ||
57 | static function ($key, $value, $isHit, $tags) { | ||
58 | $item = new CacheItem(); | ||
59 | $item->key = $key; | ||
60 | $item->value = $value; | ||
61 | $item->isHit = $isHit; | ||
62 | if (null !== $tags) { | ||
63 | $item->metadata[CacheItem::METADATA_TAGS] = $tags; | ||
64 | } | ||
65 | |||
66 | return $item; | ||
67 | }, | ||
68 | null, | ||
69 | CacheItem::class | ||
70 | ); | ||
71 | } | ||
72 | |||
73 | public function get(string $key, callable $callback, ?float $beta = null, ?array &$metadata = null): mixed | ||
74 | { | ||
75 | $item = $this->getItem($key); | ||
76 | $metadata = $item->getMetadata(); | ||
77 | |||
78 | // ArrayAdapter works in memory, we don't care about stampede protection | ||
79 | if (\INF === $beta || !$item->isHit()) { | ||
80 | $save = true; | ||
81 | $item->set($callback($item, $save)); | ||
82 | if ($save) { | ||
83 | $this->save($item); | ||
84 | } | ||
85 | } | ||
86 | |||
87 | return $item->get(); | ||
88 | } | ||
89 | |||
90 | public function delete(string $key): bool | ||
91 | { | ||
92 | return $this->deleteItem($key); | ||
93 | } | ||
94 | |||
95 | public function hasItem(mixed $key): bool | ||
96 | { | ||
97 | if (\is_string($key) && isset($this->expiries[$key]) && $this->expiries[$key] > microtime(true)) { | ||
98 | if ($this->maxItems) { | ||
99 | // Move the item last in the storage | ||
100 | $value = $this->values[$key]; | ||
101 | unset($this->values[$key]); | ||
102 | $this->values[$key] = $value; | ||
103 | } | ||
104 | |||
105 | return true; | ||
106 | } | ||
107 | \assert('' !== CacheItem::validateKey($key)); | ||
108 | |||
109 | return isset($this->expiries[$key]) && !$this->deleteItem($key); | ||
110 | } | ||
111 | |||
112 | public function getItem(mixed $key): CacheItem | ||
113 | { | ||
114 | if (!$isHit = $this->hasItem($key)) { | ||
115 | $value = null; | ||
116 | |||
117 | if (!$this->maxItems) { | ||
118 | // Track misses in non-LRU mode only | ||
119 | $this->values[$key] = null; | ||
120 | } | ||
121 | } else { | ||
122 | $value = $this->storeSerialized ? $this->unfreeze($key, $isHit) : $this->values[$key]; | ||
123 | } | ||
124 | |||
125 | return (self::$createCacheItem)($key, $value, $isHit, $this->tags[$key] ?? null); | ||
126 | } | ||
127 | |||
128 | public function getItems(array $keys = []): iterable | ||
129 | { | ||
130 | \assert(self::validateKeys($keys)); | ||
131 | |||
132 | return $this->generateItems($keys, microtime(true), self::$createCacheItem); | ||
133 | } | ||
134 | |||
135 | public function deleteItem(mixed $key): bool | ||
136 | { | ||
137 | \assert('' !== CacheItem::validateKey($key)); | ||
138 | unset($this->values[$key], $this->tags[$key], $this->expiries[$key]); | ||
139 | |||
140 | return true; | ||
141 | } | ||
142 | |||
143 | public function deleteItems(array $keys): bool | ||
144 | { | ||
145 | foreach ($keys as $key) { | ||
146 | $this->deleteItem($key); | ||
147 | } | ||
148 | |||
149 | return true; | ||
150 | } | ||
151 | |||
152 | public function save(CacheItemInterface $item): bool | ||
153 | { | ||
154 | if (!$item instanceof CacheItem) { | ||
155 | return false; | ||
156 | } | ||
157 | $item = (array) $item; | ||
158 | $key = $item["\0*\0key"]; | ||
159 | $value = $item["\0*\0value"]; | ||
160 | $expiry = $item["\0*\0expiry"]; | ||
161 | |||
162 | $now = microtime(true); | ||
163 | |||
164 | if (null !== $expiry) { | ||
165 | if (!$expiry) { | ||
166 | $expiry = \PHP_INT_MAX; | ||
167 | } elseif ($expiry <= $now) { | ||
168 | $this->deleteItem($key); | ||
169 | |||
170 | return true; | ||
171 | } | ||
172 | } | ||
173 | if ($this->storeSerialized && null === $value = $this->freeze($value, $key)) { | ||
174 | return false; | ||
175 | } | ||
176 | if (null === $expiry && 0 < $this->defaultLifetime) { | ||
177 | $expiry = $this->defaultLifetime; | ||
178 | $expiry = $now + ($expiry > ($this->maxLifetime ?: $expiry) ? $this->maxLifetime : $expiry); | ||
179 | } elseif ($this->maxLifetime && (null === $expiry || $expiry > $now + $this->maxLifetime)) { | ||
180 | $expiry = $now + $this->maxLifetime; | ||
181 | } | ||
182 | |||
183 | if ($this->maxItems) { | ||
184 | unset($this->values[$key], $this->tags[$key]); | ||
185 | |||
186 | // Iterate items and vacuum expired ones while we are at it | ||
187 | foreach ($this->values as $k => $v) { | ||
188 | if ($this->expiries[$k] > $now && \count($this->values) < $this->maxItems) { | ||
189 | break; | ||
190 | } | ||
191 | |||
192 | unset($this->values[$k], $this->tags[$k], $this->expiries[$k]); | ||
193 | } | ||
194 | } | ||
195 | |||
196 | $this->values[$key] = $value; | ||
197 | $this->expiries[$key] = $expiry ?? \PHP_INT_MAX; | ||
198 | |||
199 | if (null === $this->tags[$key] = $item["\0*\0newMetadata"][CacheItem::METADATA_TAGS] ?? null) { | ||
200 | unset($this->tags[$key]); | ||
201 | } | ||
202 | |||
203 | return true; | ||
204 | } | ||
205 | |||
206 | public function saveDeferred(CacheItemInterface $item): bool | ||
207 | { | ||
208 | return $this->save($item); | ||
209 | } | ||
210 | |||
211 | public function commit(): bool | ||
212 | { | ||
213 | return true; | ||
214 | } | ||
215 | |||
216 | public function clear(string $prefix = ''): bool | ||
217 | { | ||
218 | if ('' !== $prefix) { | ||
219 | $now = microtime(true); | ||
220 | |||
221 | foreach ($this->values as $key => $value) { | ||
222 | if (!isset($this->expiries[$key]) || $this->expiries[$key] <= $now || str_starts_with($key, $prefix)) { | ||
223 | unset($this->values[$key], $this->tags[$key], $this->expiries[$key]); | ||
224 | } | ||
225 | } | ||
226 | |||
227 | if ($this->values) { | ||
228 | return true; | ||
229 | } | ||
230 | } | ||
231 | |||
232 | $this->values = $this->tags = $this->expiries = []; | ||
233 | |||
234 | return true; | ||
235 | } | ||
236 | |||
237 | /** | ||
238 | * Returns all cached values, with cache miss as null. | ||
239 | */ | ||
240 | public function getValues(): array | ||
241 | { | ||
242 | if (!$this->storeSerialized) { | ||
243 | return $this->values; | ||
244 | } | ||
245 | |||
246 | $values = $this->values; | ||
247 | foreach ($values as $k => $v) { | ||
248 | if (null === $v || 'N;' === $v) { | ||
249 | continue; | ||
250 | } | ||
251 | if (!\is_string($v) || !isset($v[2]) || ':' !== $v[1]) { | ||
252 | $values[$k] = serialize($v); | ||
253 | } | ||
254 | } | ||
255 | |||
256 | return $values; | ||
257 | } | ||
258 | |||
259 | public function reset(): void | ||
260 | { | ||
261 | $this->clear(); | ||
262 | } | ||
263 | |||
264 | private function generateItems(array $keys, float $now, \Closure $f): \Generator | ||
265 | { | ||
266 | foreach ($keys as $i => $key) { | ||
267 | if (!$isHit = isset($this->expiries[$key]) && ($this->expiries[$key] > $now || !$this->deleteItem($key))) { | ||
268 | $value = null; | ||
269 | |||
270 | if (!$this->maxItems) { | ||
271 | // Track misses in non-LRU mode only | ||
272 | $this->values[$key] = null; | ||
273 | } | ||
274 | } else { | ||
275 | if ($this->maxItems) { | ||
276 | // Move the item last in the storage | ||
277 | $value = $this->values[$key]; | ||
278 | unset($this->values[$key]); | ||
279 | $this->values[$key] = $value; | ||
280 | } | ||
281 | |||
282 | $value = $this->storeSerialized ? $this->unfreeze($key, $isHit) : $this->values[$key]; | ||
283 | } | ||
284 | unset($keys[$i]); | ||
285 | |||
286 | yield $key => $f($key, $value, $isHit, $this->tags[$key] ?? null); | ||
287 | } | ||
288 | |||
289 | foreach ($keys as $key) { | ||
290 | yield $key => $f($key, null, false); | ||
291 | } | ||
292 | } | ||
293 | |||
294 | private function freeze($value, string $key): string|int|float|bool|array|\UnitEnum|null | ||
295 | { | ||
296 | if (null === $value) { | ||
297 | return 'N;'; | ||
298 | } | ||
299 | if (\is_string($value)) { | ||
300 | // Serialize strings if they could be confused with serialized objects or arrays | ||
301 | if ('N;' === $value || (isset($value[2]) && ':' === $value[1])) { | ||
302 | return serialize($value); | ||
303 | } | ||
304 | } elseif (!\is_scalar($value)) { | ||
305 | try { | ||
306 | $serialized = serialize($value); | ||
307 | } catch (\Exception $e) { | ||
308 | unset($this->values[$key], $this->tags[$key]); | ||
309 | $type = get_debug_type($value); | ||
310 | $message = sprintf('Failed to save key "{key}" of type %s: %s', $type, $e->getMessage()); | ||
311 | CacheItem::log($this->logger, $message, ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]); | ||
312 | |||
313 | return null; | ||
314 | } | ||
315 | // Keep value serialized if it contains any objects or any internal references | ||
316 | if ('C' === $serialized[0] || 'O' === $serialized[0] || preg_match('/;[OCRr]:[1-9]/', $serialized)) { | ||
317 | return $serialized; | ||
318 | } | ||
319 | } | ||
320 | |||
321 | return $value; | ||
322 | } | ||
323 | |||
324 | private function unfreeze(string $key, bool &$isHit): mixed | ||
325 | { | ||
326 | if ('N;' === $value = $this->values[$key]) { | ||
327 | return null; | ||
328 | } | ||
329 | if (\is_string($value) && isset($value[2]) && ':' === $value[1]) { | ||
330 | try { | ||
331 | $value = unserialize($value); | ||
332 | } catch (\Exception $e) { | ||
333 | CacheItem::log($this->logger, 'Failed to unserialize key "{key}": '.$e->getMessage(), ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]); | ||
334 | $value = false; | ||
335 | } | ||
336 | if (false === $value) { | ||
337 | $value = null; | ||
338 | $isHit = false; | ||
339 | |||
340 | if (!$this->maxItems) { | ||
341 | $this->values[$key] = null; | ||
342 | } | ||
343 | } | ||
344 | } | ||
345 | |||
346 | return $value; | ||
347 | } | ||
348 | |||
349 | private function validateKeys(array $keys): bool | ||
350 | { | ||
351 | foreach ($keys as $key) { | ||
352 | if (!\is_string($key) || !isset($this->expiries[$key])) { | ||
353 | CacheItem::validateKey($key); | ||
354 | } | ||
355 | } | ||
356 | |||
357 | return true; | ||
358 | } | ||
359 | } | ||