Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
13 / 13 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
AbstractDriver | |
100.00% |
13 / 13 |
|
100.00% |
2 / 2 |
7 | |
100.00% |
1 / 1 |
__construct | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getMultiple | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
setMultiple | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 |
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\Driver; |
17 | |
18 | use Aviat\Banker\LoggerTrait; |
19 | use Aviat\Banker\KeyValidateTrait; |
20 | use DateInterval; |
21 | use Psr\Log\LoggerAwareInterface; |
22 | |
23 | /** |
24 | * Base class for cache backends |
25 | */ |
26 | abstract class AbstractDriver implements DriverInterface, LoggerAwareInterface { |
27 | |
28 | use KeyValidateTrait; |
29 | use LoggerTrait; |
30 | |
31 | /** |
32 | * Data to be stored later |
33 | * |
34 | * @var array |
35 | */ |
36 | protected array $deferred = []; |
37 | |
38 | /** |
39 | * Common constructor interface for driver classes |
40 | */ |
41 | abstract public function __construct(); |
42 | |
43 | /** |
44 | * Retrieve a set of values by their cache key |
45 | * |
46 | * @param string[] $keys |
47 | * @return array |
48 | */ |
49 | public function getMultiple(array $keys = []): array |
50 | { |
51 | $this->validateKeys($keys); |
52 | |
53 | $output = []; |
54 | |
55 | foreach ($keys as $key) |
56 | { |
57 | if ($this->exists($key)) |
58 | { |
59 | $output[$key] = $this->get($key); |
60 | } |
61 | } |
62 | |
63 | return $output; |
64 | } |
65 | |
66 | /** |
67 | * Set multiple cache values |
68 | * |
69 | * @param array $items |
70 | * @param DateInterval|int|null $expires |
71 | * @return bool |
72 | */ |
73 | public function setMultiple(array $items, DateInterval|int|null $expires = NULL): bool |
74 | { |
75 | $this->validateKeys($items, TRUE); |
76 | |
77 | $setResults = []; |
78 | foreach ($items as $k => $v) |
79 | { |
80 | $setResults[] = ($expires === NULL) |
81 | ? $this->set($k, $v) |
82 | : $this->set($k, $v, $expires); |
83 | } |
84 | |
85 | // Only return true if all the results are true |
86 | return array_reduce($setResults, fn ($carry, $item) => $item && $carry, TRUE); |
87 | } |
88 | } |