Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
34 / 34 |
|
100.00% |
22 / 22 |
CRAP | |
100.00% |
1 / 1 |
Cache | |
100.00% |
34 / 34 |
|
100.00% |
22 / 22 |
27 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAvailableAdapters | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
isAvailable | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
adapter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getTtl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getItemTtl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
saveItem | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
saveItems | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
getItem | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasItem | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
deleteItem | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
deleteItems | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
clear | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
destroy | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__set | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__isset | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__unset | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
offsetExists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
offsetGet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
offsetSet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
offsetUnset | |
100.00% |
1 / 1 |
|
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; |
15 | |
16 | use Pop\Cache\Adapter\AdapterInterface; |
17 | |
18 | /** |
19 | * Cache class |
20 | * |
21 | * @category Pop |
22 | * @package Pop\Cache |
23 | * @author Nick Sagona, III <dev@nolainteractive.com> |
24 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
25 | * @license http://www.popphp.org/license New BSD License |
26 | * @version 4.0.0 |
27 | */ |
28 | class Cache implements \ArrayAccess |
29 | { |
30 | |
31 | /** |
32 | * Cache adapter |
33 | * @var ?Adapter\AdapterInterface |
34 | */ |
35 | protected ?Adapter\AdapterInterface $adapter = null; |
36 | |
37 | /** |
38 | * Constructor |
39 | * |
40 | * Instantiate the cache object |
41 | * |
42 | * @param Adapter\AdapterInterface $adapter |
43 | */ |
44 | public function __construct(Adapter\AdapterInterface $adapter) |
45 | { |
46 | $this->adapter = $adapter; |
47 | } |
48 | |
49 | /** |
50 | * Determine available adapters |
51 | * |
52 | * @return array |
53 | */ |
54 | public static function getAvailableAdapters(): array |
55 | { |
56 | $pdoDrivers = (class_exists('Pdo', false)) ? \PDO::getAvailableDrivers() : []; |
57 | |
58 | return [ |
59 | 'apc' => (function_exists('apc_cache_info')), |
60 | 'file' => true, |
61 | 'memcached' => (class_exists('Memcache', false)), |
62 | 'redis' => (class_exists('Redis', false)), |
63 | 'session' => (function_exists('session_start')), |
64 | 'sqlite' => (class_exists('Sqlite3') || in_array('sqlite', $pdoDrivers)) |
65 | ]; |
66 | } |
67 | |
68 | /** |
69 | * Determine if an adapter is available |
70 | * |
71 | * @param string $adapter |
72 | * @return bool |
73 | */ |
74 | public static function isAvailable(string $adapter): bool |
75 | { |
76 | $adapter = strtolower($adapter); |
77 | $adapters = self::getAvailableAdapters(); |
78 | return (isset($adapters[$adapter]) && ($adapters[$adapter])); |
79 | } |
80 | |
81 | /** |
82 | * Get the adapter |
83 | * |
84 | * @return ?Adapter\AdapterInterface |
85 | */ |
86 | public function adapter(): ?Adapter\AdapterInterface |
87 | { |
88 | return $this->adapter; |
89 | } |
90 | |
91 | /** |
92 | * Get global cache TTL |
93 | * |
94 | * @return int |
95 | */ |
96 | public function getTtl(): int |
97 | { |
98 | return $this->adapter->getTtl(); |
99 | } |
100 | |
101 | /** |
102 | * Get item cache TTL |
103 | * |
104 | * @param string $id |
105 | * @return int |
106 | */ |
107 | public function getItemTtl(string $id): int |
108 | { |
109 | return $this->adapter->getItemTtl($id); |
110 | } |
111 | |
112 | /** |
113 | * Save an item to cache |
114 | * |
115 | * @param string $id |
116 | * @param mixed $value |
117 | * @param ?int $ttl |
118 | * @return void |
119 | */ |
120 | public function saveItem(string $id, mixed $value, ?int $ttl = null): void |
121 | { |
122 | $this->adapter->saveItem($id, $value, $ttl); |
123 | } |
124 | |
125 | /** |
126 | * Save items to cache |
127 | * |
128 | * @param array $items |
129 | * @return void |
130 | */ |
131 | public function saveItems(array $items): void |
132 | { |
133 | foreach ($items as $id => $value) { |
134 | $this->adapter->saveItem($id, $value); |
135 | } |
136 | } |
137 | |
138 | /** |
139 | * Get an item from cache |
140 | * |
141 | * @param string $id |
142 | * @return mixed |
143 | */ |
144 | public function getItem(string $id): mixed |
145 | { |
146 | return $this->adapter->getItem($id); |
147 | } |
148 | |
149 | /** |
150 | * Determine if the item is in cache |
151 | * |
152 | * @param string $id |
153 | * @return bool |
154 | */ |
155 | public function hasItem(string $id): bool |
156 | { |
157 | return $this->adapter->hasItem($id); |
158 | } |
159 | |
160 | /** |
161 | * Delete an item in cache |
162 | * |
163 | * @param string $id |
164 | * @return void |
165 | */ |
166 | public function deleteItem(string $id): void |
167 | { |
168 | $this->adapter->deleteItem($id); |
169 | } |
170 | |
171 | /** |
172 | * Delete items in cache |
173 | * |
174 | * @param array $ids |
175 | * @return void |
176 | */ |
177 | public function deleteItems(array $ids): void |
178 | { |
179 | foreach ($ids as $id) { |
180 | $this->adapter->deleteItem($id); |
181 | } |
182 | } |
183 | |
184 | /** |
185 | * Clear all stored values from cache |
186 | * |
187 | * @return void |
188 | */ |
189 | public function clear(): void |
190 | { |
191 | $this->adapter->clear(); |
192 | } |
193 | |
194 | /** |
195 | * Destroy cache resource |
196 | * |
197 | * @return void |
198 | */ |
199 | public function destroy(): void |
200 | { |
201 | $this->adapter->destroy(); |
202 | } |
203 | |
204 | /** |
205 | * Magic get method to return an item from cache |
206 | * |
207 | * @param string $name |
208 | * @return mixed |
209 | */ |
210 | public function __get(string $name): mixed |
211 | { |
212 | return $this->adapter->getItem($name); |
213 | } |
214 | |
215 | /** |
216 | * Magic set method to save an item in the cache |
217 | * |
218 | * @param string $name |
219 | * @param mixed $value |
220 | * @return void |
221 | */ |
222 | public function __set(string $name, mixed $value): void |
223 | { |
224 | $this->adapter->saveItem($name, $value); |
225 | } |
226 | |
227 | /** |
228 | * Determine if the item is in cache |
229 | * |
230 | * @param string $name |
231 | * @return bool |
232 | */ |
233 | public function __isset(string $name): bool |
234 | { |
235 | return $this->adapter->hasItem($name); |
236 | } |
237 | |
238 | /** |
239 | * Delete value from cache |
240 | * |
241 | * @param string $name |
242 | * @return void |
243 | */ |
244 | public function __unset(string $name): void |
245 | { |
246 | $this->adapter->deleteItem($name); |
247 | } |
248 | |
249 | /** |
250 | * ArrayAccess offsetExists |
251 | * |
252 | * @param mixed $offset |
253 | * @return bool |
254 | */ |
255 | public function offsetExists(mixed $offset): bool |
256 | { |
257 | return $this->__isset($offset); |
258 | } |
259 | |
260 | /** |
261 | * ArrayAccess offsetGet |
262 | * |
263 | * @param mixed $offset |
264 | * @return mixed |
265 | */ |
266 | public function offsetGet(mixed $offset): mixed |
267 | { |
268 | return $this->__get($offset); |
269 | } |
270 | |
271 | /** |
272 | * ArrayAccess offsetSet |
273 | * |
274 | * @param mixed $offset |
275 | * @param mixed $value |
276 | * @return void |
277 | */ |
278 | public function offsetSet(mixed $offset, mixed $value): void |
279 | { |
280 | $this->__set($offset, $value); |
281 | } |
282 | |
283 | /** |
284 | * ArrayAccess offsetUnset |
285 | * |
286 | * @param mixed $offset |
287 | * @return void |
288 | */ |
289 | public function offsetUnset(mixed $offset): void |
290 | { |
291 | $this->__unset($offset); |
292 | } |
293 | |
294 | } |