1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Soupmix\Cache; |
||
6 | |||
7 | use DateInterval; |
||
8 | use DateTime; |
||
9 | use Psr\SimpleCache\CacheInterface; |
||
10 | |||
11 | use function apcu_clear_cache; |
||
12 | use function apcu_delete; |
||
13 | use function apcu_exists; |
||
14 | use function apcu_fetch; |
||
15 | use function apcu_store; |
||
16 | use function time; |
||
17 | |||
18 | class APCUCache extends Common implements CacheInterface |
||
19 | { |
||
20 | /** |
||
21 | 2 | * {@inheritDoc} |
|
22 | */ |
||
23 | 2 | public function get($key, $default = null) |
|
24 | 2 | { |
|
25 | 2 | $this->checkReservedCharacters($key); |
|
26 | $value = apcu_fetch($key); |
||
27 | |||
28 | return $value ?: $default; |
||
29 | } |
||
30 | |||
31 | 5 | /** |
|
32 | * {@inheritDoc} |
||
33 | 5 | */ |
|
34 | 3 | public function set($key, $value, $ttl = null): bool |
|
35 | 1 | { |
|
36 | $this->checkReservedCharacters($key); |
||
37 | 3 | if ($ttl instanceof DateInterval) { |
|
38 | $ttl = (new DateTime('now'))->add($ttl)->getTimeStamp() - time(); |
||
39 | } |
||
40 | |||
41 | return apcu_store($key, $value, (int) $ttl); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
42 | } |
||
43 | 2 | ||
44 | /** |
||
45 | 2 | * {@inheritDoc} |
|
46 | 2 | */ |
|
47 | public function delete($key): bool |
||
48 | { |
||
49 | $this->checkReservedCharacters($key); |
||
50 | |||
51 | return (bool) apcu_delete($key); |
||
52 | 6 | } |
|
53 | |||
54 | 6 | public function clear(): bool |
|
55 | { |
||
56 | return apcu_clear_cache(); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | 1 | * {@inheritDoc} |
|
61 | */ |
||
62 | 1 | public function has($key) |
|
63 | 1 | { |
|
64 | $this->checkReservedCharacters($key); |
||
65 | |||
66 | return apcu_exists($key); |
||
0 ignored issues
–
show
|
|||
67 | } |
||
68 | } |
||
69 |