| Total Complexity | 11 |
| Total Lines | 72 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 8 | class ApcuStore extends AbstractCache |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * {@inheritDoc} |
||
| 12 | */ |
||
| 13 | 7 | public function get(string $key, bool $withExpired = false) |
|
| 14 | { |
||
| 15 | 7 | $contents = apcu_fetch($this->getActualCacheKey($key)); |
|
| 16 | |||
| 17 | 7 | if ( ! $contents) { |
|
| 18 | 3 | return null; |
|
| 19 | } |
||
| 20 | |||
| 21 | 6 | if ($withExpired) { |
|
| 22 | 1 | return $contents ?: null; |
|
| 23 | } |
||
| 24 | |||
| 25 | 6 | $isExpired = Carbon::parse($contents['expires_at'])->lt(Carbon::now()); |
|
| 26 | |||
| 27 | 6 | return $isExpired ? null : $contents; |
|
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * {@inheritDoc} |
||
| 32 | */ |
||
| 33 | 4 | public function set(string $key, $value) |
|
| 34 | { |
||
| 35 | 4 | $contents = $this->get($key) ?? []; |
|
| 36 | |||
| 37 | 4 | if (\is_array($value)) { |
|
| 38 | 4 | $contents = $value + $contents; |
|
| 39 | } else { |
||
| 40 | 1 | $contents[] = $value; |
|
| 41 | } |
||
| 42 | |||
| 43 | 4 | return apcu_store($this->getActualCacheKey($key), $contents, $this->getTtl()); |
|
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritDoc} |
||
| 48 | */ |
||
| 49 | 2 | public function delete(string $key): bool |
|
| 50 | { |
||
| 51 | 2 | return true === apcu_delete($this->getActualCacheKey($key)); |
|
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritDoc} |
||
| 56 | */ |
||
| 57 | 1 | public function keys(): array |
|
| 58 | { |
||
| 59 | 1 | $iterator = new APCUIterator('/^' . preg_quote($this->getPrefix()) . '.*$/', APC_ITER_KEY); |
|
| 60 | |||
| 61 | 1 | return array_column(iterator_to_array($iterator, false), 'key'); |
|
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get actual cache key with prefix. |
||
| 66 | * |
||
| 67 | * @param string $key |
||
| 68 | * |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | 7 | protected function getActualCacheKey(string $key): string |
|
| 80 | } |
||
| 81 | } |
||
| 82 |