Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
16 / 16 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
RedisDriver | |
100.00% |
16 / 16 |
|
100.00% |
7 / 7 |
12 | |
100.00% |
1 / 1 |
__construct | n/a |
0 / 0 |
n/a |
0 / 0 |
2 | |||||
__destruct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
exists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
get | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
set | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
delete | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
deleteMultiple | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
flush | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
expiresAt | |
100.00% |
1 / 1 |
|
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\Driver; |
17 | |
18 | use Aviat\Banker\Exception\CacheException; |
19 | use Predis\Client; |
20 | |
21 | use DateInterval; |
22 | |
23 | /** |
24 | * Redis cache backend |
25 | */ |
26 | class RedisDriver extends AbstractDriver { |
27 | |
28 | /** |
29 | * The object encapsulating the connection to the Redis server |
30 | * |
31 | * @var Client |
32 | */ |
33 | protected Client $conn; |
34 | |
35 | /** |
36 | * RedisDriver constructor. |
37 | * |
38 | * @codeCoverageIgnore |
39 | * @param array $config |
40 | * @param array $options - Predis library connection options |
41 | * @throws CacheException |
42 | */ |
43 | public function __construct(array $config = [], array $options = []) |
44 | { |
45 | if ( ! class_exists(Client::class)) |
46 | { |
47 | throw new CacheException('The redis driver requires the predis/predis composer package to be installed.'); |
48 | } |
49 | |
50 | $this->conn = new Client($config, $options); |
51 | } |
52 | |
53 | /** |
54 | * Disconnect from redis server |
55 | * @codeCoverageIgnore |
56 | */ |
57 | public function __destruct() |
58 | { |
59 | $this->conn->quit(); |
60 | } |
61 | |
62 | /** |
63 | * See if a key currently exists in the cache |
64 | * |
65 | * @param string $key |
66 | * @return bool |
67 | */ |
68 | public function exists(string $key): bool |
69 | { |
70 | return (bool) $this->conn->exists($key); |
71 | } |
72 | |
73 | /** |
74 | * Get the value for the selected cache key |
75 | * |
76 | * @param string $key |
77 | * @return mixed |
78 | */ |
79 | public function get(string $key): mixed |
80 | { |
81 | $raw = $this->conn->get($key) ?? ''; |
82 | $parsed = @unserialize($raw); |
83 | $hasError = is_array(error_get_last()); |
84 | |
85 | return ($hasError) ? NULL : $parsed; |
86 | } |
87 | |
88 | /** |
89 | * Set a cached value |
90 | * |
91 | * @param string $key |
92 | * @param mixed $value |
93 | * @param DateInterval|int|null $expires |
94 | * @return bool |
95 | */ |
96 | public function set(string $key, mixed $value, DateInterval|int|null $expires = NULL): bool |
97 | { |
98 | $value = serialize($value); |
99 | |
100 | $status = ($expires !== NULL) |
101 | ? $this->conn->set($key, $value, 'EX', $expires) |
102 | : $this->conn->set($key, $value); |
103 | |
104 | return (string)$status === 'OK'; |
105 | } |
106 | |
107 | /** |
108 | * Remove an item from the cache |
109 | * |
110 | * @param string $key |
111 | * @return boolean |
112 | */ |
113 | public function delete(string $key): bool |
114 | { |
115 | // This call returns the number of keys deleted |
116 | return $this->conn->del([$key]) === 1; |
117 | } |
118 | |
119 | /** |
120 | * Remove multiple items from the cache |
121 | * |
122 | * @param string[] $keys |
123 | * @return boolean |
124 | */ |
125 | public function deleteMultiple(array $keys = []): bool |
126 | { |
127 | $this->validateKeys($keys); |
128 | |
129 | $res = $this->conn->del(...array_values($keys)); |
130 | return $res === count($keys); |
131 | } |
132 | |
133 | /** |
134 | * Empty the cache |
135 | * |
136 | * @return boolean |
137 | */ |
138 | public function flush(): bool |
139 | { |
140 | return (bool) $this->conn->flushdb(); |
141 | } |
142 | |
143 | /** |
144 | * Set the expiration timestamp of a key |
145 | * |
146 | * @param string $key |
147 | * @param int $expires |
148 | * @return boolean |
149 | */ |
150 | public function expiresAt(string $key, int $expires): bool |
151 | { |
152 | return (bool) $this->conn->expireat($key, $expires); |
153 | } |
154 | } |