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