1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the ICanBoogie package. |
||
5 | * |
||
6 | * (c) Olivier Laviale <[email protected]> |
||
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 ICanBoogie\Storage; |
||
13 | |||
14 | /** |
||
15 | * A storage using APC. |
||
16 | */ |
||
17 | class APCStorage implements Storage, \ArrayAccess |
||
18 | { |
||
19 | use Storage\ArrayAccess; |
||
20 | |||
21 | /** |
||
22 | * Whether the APC feature is available. |
||
23 | * |
||
24 | * @codeCoverageIgnore |
||
25 | */ |
||
26 | static public function is_available(): bool |
||
27 | { |
||
28 | return (extension_loaded('apc') || extension_loaded('apcu')) && ini_get('apc.enabled'); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $prefix; |
||
35 | |||
36 | public function __construct(string $prefix = null) |
||
37 | { |
||
38 | $this->prefix = $prefix ?: substr(sha1($_SERVER['DOCUMENT_ROOT']), 0, 8) . ':'; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @inheritdoc |
||
43 | */ |
||
44 | public function exists(string $key): bool |
||
45 | { |
||
46 | return apcu_exists($this->prefix . $key); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @inheritdoc |
||
51 | */ |
||
52 | public function retrieve(string $key) |
||
53 | { |
||
54 | $rc = apcu_fetch($this->prefix . $key, $success); |
||
55 | |||
56 | return $success ? $rc : null; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @inheritdoc |
||
61 | */ |
||
62 | public function store(string $key, $data, int $ttl = null): void |
||
63 | { |
||
64 | apcu_store($this->prefix . $key, $data, $ttl ?: 0); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @inheritdoc |
||
69 | */ |
||
70 | public function eliminate(string $key): void |
||
71 | { |
||
72 | apcu_delete($this->prefix . $key); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @inheritdoc |
||
77 | */ |
||
78 | public function clear(): void |
||
79 | { |
||
80 | apcu_delete($this->create_internal_iterator()); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @inheritdoc |
||
85 | */ |
||
86 | public function getIterator(): iterable |
||
87 | { |
||
88 | $prefix_length = strlen($this->prefix); |
||
89 | |||
90 | foreach ($this->create_internal_iterator() as $key => $dummy) |
||
91 | { |
||
92 | yield substr($key, $prefix_length); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Creates internal iterator. |
||
98 | */ |
||
99 | private function create_internal_iterator(): \APCUIterator |
||
100 | { |
||
101 | return new \APCUIterator('/^' . preg_quote($this->prefix) . '/', APC_ITER_NONE); |
||
102 | } |
||
103 | } |
||
104 |