Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.67% |
29 / 30 |
|
88.89% |
8 / 9 |
CRAP | |
0.00% |
0 / 1 |
Apc | |
96.67% |
29 / 30 |
|
88.89% |
8 / 9 |
15 | |
0.00% |
0 / 1 |
__construct | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
getInfo | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getItemTtl | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
saveItem | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
getItem | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
hasItem | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
deleteItem | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
clear | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
destroy | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Cache\Adapter; |
15 | |
16 | /** |
17 | * APC cache adapter class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Cache |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 4.0.0 |
25 | */ |
26 | class Apc extends AbstractAdapter |
27 | { |
28 | |
29 | /** |
30 | * Constructor |
31 | * |
32 | * Instantiate the APC cache object |
33 | * |
34 | * @param int $ttl |
35 | * @throws Exception |
36 | */ |
37 | public function __construct(int $ttl = 0) |
38 | { |
39 | parent::__construct($ttl); |
40 | if (!function_exists('apcu_cache_info')) { |
41 | throw new Exception('Error: APCu is not available.'); |
42 | } |
43 | } |
44 | |
45 | /** |
46 | * Method to get the current APC info. |
47 | * |
48 | * @return array |
49 | */ |
50 | public function getInfo(): array |
51 | { |
52 | return apcu_cache_info(); |
53 | } |
54 | |
55 | /** |
56 | * Get the time-to-live for an item in cache |
57 | * |
58 | * @param string $id |
59 | * @return int |
60 | */ |
61 | public function getItemTtl(string $id): int |
62 | { |
63 | $cacheValue = apcu_fetch($id); |
64 | $ttl = 0; |
65 | |
66 | if ($cacheValue !== false) { |
67 | $ttl = $cacheValue['ttl']; |
68 | } |
69 | |
70 | return $ttl; |
71 | } |
72 | |
73 | /** |
74 | * Save an item to cache |
75 | * |
76 | * @param string $id |
77 | * @param mixed $value |
78 | * @param ?int $ttl |
79 | * @return Apc |
80 | */ |
81 | public function saveItem(string $id, mixed $value, ?int $ttl = null): Apc |
82 | { |
83 | $cacheValue = [ |
84 | 'start' => time(), |
85 | 'ttl' => ($ttl !== null) ? $ttl : $this->ttl, |
86 | 'value' => $value |
87 | ]; |
88 | |
89 | apcu_store($id, $cacheValue, $cacheValue['ttl']); |
90 | |
91 | return $this; |
92 | } |
93 | |
94 | /** |
95 | * Get an item from cache |
96 | * |
97 | * @param string $id |
98 | * @return mixed |
99 | */ |
100 | public function getItem(string $id): mixed |
101 | { |
102 | $cacheValue = apcu_fetch($id); |
103 | $value = false; |
104 | |
105 | if (($cacheValue !== false) && |
106 | (($cacheValue['ttl'] == 0) || ((time() - $cacheValue['start']) <= $cacheValue['ttl']))) { |
107 | $value = $cacheValue['value']; |
108 | } else { |
109 | $this->deleteItem($id); |
110 | } |
111 | |
112 | return $value; |
113 | } |
114 | |
115 | /** |
116 | * Determine if the item exist in cache |
117 | * |
118 | * @param string $id |
119 | * @return bool |
120 | */ |
121 | public function hasItem(string $id): bool |
122 | { |
123 | return ($this->getItem($id) !== false); |
124 | } |
125 | |
126 | /** |
127 | * Delete a value in cache |
128 | * |
129 | * @param string $id |
130 | * @return Apc |
131 | */ |
132 | public function deleteItem(string $id): Apc |
133 | { |
134 | apcu_delete($id); |
135 | return $this; |
136 | } |
137 | |
138 | /** |
139 | * Clear all stored values from cache |
140 | * |
141 | * @return Apc |
142 | */ |
143 | public function clear(): Apc |
144 | { |
145 | apcu_clear_cache(); |
146 | return $this; |
147 | } |
148 | |
149 | /** |
150 | * Destroy cache resource |
151 | * |
152 | * @return Apc |
153 | */ |
154 | public function destroy(): Apc |
155 | { |
156 | $this->clear(); |
157 | return $this; |
158 | } |
159 | |
160 | } |