Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
28 / 28 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
Teller | |
100.00% |
28 / 28 |
|
100.00% |
9 / 9 |
14 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
clear | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
get | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
set | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
delete | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getMultiple | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
setMultiple | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
deleteMultiple | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
has | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
1 | <?php declare(strict_types=1); |
2 | /** |
3 | * Banker |
4 | * |
5 | * A Caching library implementing psr/cache (PSR 6) and psr/simple-cache (PSR 16) |
6 | * |
7 | * PHP version 8+ |
8 | * |
9 | * @package Banker |
10 | * @author Timothy J. Warren <tim@timshomepage.net> |
11 | * @copyright 2016 - 2023 Timothy J. Warren |
12 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
13 | * @version 4.1.0 |
14 | * @link https://git.timshomepage.net/timw4mail/banker |
15 | */ |
16 | namespace Aviat\Banker; |
17 | |
18 | use DateInterval; |
19 | use Psr\Log\{LoggerInterface, LoggerAwareInterface}; |
20 | use Psr\SimpleCache; |
21 | |
22 | /** |
23 | * Implements PSR-16 (SimpleCache) |
24 | */ |
25 | class Teller implements LoggerAwareInterface, SimpleCache\CacheInterface { |
26 | use _Driver; |
27 | use KeyValidateTrait; |
28 | use LoggerTrait; |
29 | |
30 | /** |
31 | * Set up the cache backend |
32 | * |
33 | * @param array $config |
34 | * @param LoggerInterface|null $logger |
35 | */ |
36 | public function __construct(array $config, ?LoggerInterface $logger = NULL) |
37 | { |
38 | $this->driver = $this->loadDriver($config); |
39 | |
40 | if ($logger !== NULL) |
41 | { |
42 | $this->setLogger($logger); |
43 | } |
44 | } |
45 | |
46 | /** |
47 | * Wipes clean the entire cache's keys. |
48 | * |
49 | * @return bool True on success and false on failure. |
50 | */ |
51 | public function clear(): bool |
52 | { |
53 | return $this->driver->flush(); |
54 | } |
55 | |
56 | /** |
57 | * Fetches a value from the cache. |
58 | * |
59 | * @param string $key The unique key of this item in the cache. |
60 | * @param mixed $default Default value to return if the key does not exist. |
61 | * |
62 | * @return mixed The value of the item from the cache, or $default in case of cache miss. |
63 | * |
64 | * @throws SimpleCache\InvalidArgumentException |
65 | * MUST be thrown if the $key string is not a legal value. |
66 | */ |
67 | public function get(string $key, mixed $default = null): mixed |
68 | { |
69 | $this->validateKey($key); |
70 | |
71 | return ($this->driver->exists($key)) ? $this->driver->get($key) : $default; |
72 | } |
73 | |
74 | /** |
75 | * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
76 | * |
77 | * @param string $key The key of the item to store. |
78 | * @param mixed $value The value of the item to store, must be serializable. |
79 | * @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
80 | * the driver supports TTL then the library may set a default value |
81 | * for it or let the driver take care of that. |
82 | * |
83 | * @return bool True on success and false on failure. |
84 | * |
85 | * @throws SimpleCache\InvalidArgumentException |
86 | * MUST be thrown if the $key string is not a legal value. |
87 | */ |
88 | public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool |
89 | { |
90 | $this->validateKey($key); |
91 | |
92 | return $this->driver->set($key, $value, $ttl); |
93 | } |
94 | |
95 | /** |
96 | * Delete an item from the cache by its unique key. |
97 | * |
98 | * @param string $key The unique cache key of the item to delete. |
99 | * |
100 | * @return bool True if the item was successfully removed. False if there was an error. |
101 | * |
102 | * @throws SimpleCache\InvalidArgumentException |
103 | * MUST be thrown if the $key string is not a legal value. |
104 | */ |
105 | public function delete(string $key): bool |
106 | { |
107 | $this->validateKey($key); |
108 | |
109 | return $this->driver->delete($key); |
110 | } |
111 | |
112 | /** |
113 | * Obtains multiple cache items by their unique keys. |
114 | * |
115 | * @param iterable $keys A list of keys that can obtained in a single operation. |
116 | * @param mixed $default Default value to return for keys that do not exist. |
117 | * |
118 | * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value. |
119 | * |
120 | * @throws SimpleCache\InvalidArgumentException |
121 | * MUST be thrown if $keys is neither an array nor a Traversable, |
122 | * or if any of the $keys are not a legal value. |
123 | */ |
124 | public function getMultiple(iterable $keys, mixed $default = null): iterable |
125 | { |
126 | $keys = (array)$keys; |
127 | $this->validateKeys($keys); |
128 | $foundValues = $this->driver->getMultiple($keys); |
129 | $foundKeys = array_keys($foundValues); |
130 | |
131 | // If all the values are found, just return them |
132 | if ($keys === $foundKeys) |
133 | { |
134 | return $foundValues; |
135 | } |
136 | |
137 | // Otherwise, return a default value for missing keys |
138 | $result = $foundValues; |
139 | foreach (array_diff($keys, $foundKeys) as $key) |
140 | { |
141 | $result[$key] = $default; |
142 | } |
143 | |
144 | return $result; |
145 | } |
146 | |
147 | /** |
148 | * Persists a set of key => value pairs in the cache, with an optional TTL. |
149 | * |
150 | * @param iterable $values A list of key => value pairs for a multiple-set operation. |
151 | * @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
152 | * the driver supports TTL then the library may set a default value |
153 | * for it or let the driver take care of that. |
154 | * |
155 | * @return bool True on success and false on failure. |
156 | * |
157 | * @throws SimpleCache\InvalidArgumentException |
158 | * MUST be thrown if $values is neither an array nor a Traversable, |
159 | * or if any of the $values are not a legal value. |
160 | */ |
161 | public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool |
162 | { |
163 | $this->validateKeys($values, TRUE); |
164 | |
165 | return ($ttl === NULL) |
166 | ? $this->driver->setMultiple((array)$values) |
167 | : $this->driver->setMultiple((array)$values, $ttl); |
168 | } |
169 | |
170 | /** |
171 | * Deletes multiple cache items in a single operation. |
172 | * |
173 | * @param iterable $keys A list of string-based keys to be deleted. |
174 | * |
175 | * @return bool True if the items were successfully removed. False if there was an error. |
176 | * |
177 | * @throws SimpleCache\InvalidArgumentException |
178 | * MUST be thrown if $keys is neither an array nor a Traversable, |
179 | * or if any of the $keys are not a legal value. |
180 | */ |
181 | public function deleteMultiple(iterable $keys): bool |
182 | { |
183 | $this->validateKeys($keys); |
184 | |
185 | return $this->driver->deleteMultiple((array)$keys); |
186 | } |
187 | |
188 | /** |
189 | * Determines whether an item is present in the cache. |
190 | * |
191 | * NOTE: It is recommended that has() is only to be used for cache warming type purposes |
192 | * and not to be used within your live applications operations for get/set, as this method |
193 | * is subject to a race condition where your has() will return true and immediately after, |
194 | * another script can remove it making the state of your app out of date. |
195 | * |
196 | * @param string $key The cache item key. |
197 | * |
198 | * @return bool |
199 | * |
200 | * @throws SimpleCache\InvalidArgumentException |
201 | * MUST be thrown if the $key string is not a legal value. |
202 | */ |
203 | public function has(string $key): bool |
204 | { |
205 | $this->validateKey($key); |
206 | |
207 | return $this->driver->exists($key); |
208 | } |
209 | } |