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