Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
53 / 53 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
RedisCache | |
100.00% |
53 / 53 |
|
100.00% |
8 / 8 |
13 | |
100.00% |
1 / 1 |
__destruct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
initialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
connect | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
get | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
getValue | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
set | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
2 | |||
delete | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
flush | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 |
1 | <?php declare(strict_types=1); |
2 | /* |
3 | * This file is part of Aplus Framework Cache Library. |
4 | * |
5 | * (c) Natan Felles <natanfelles@gmail.com> |
6 | * |
7 | * For the full copyright and license information, please view the LICENSE |
8 | * file that was distributed with this source code. |
9 | */ |
10 | namespace Framework\Cache; |
11 | |
12 | use Redis; |
13 | |
14 | /** |
15 | * Class RedisCache. |
16 | * |
17 | * @package cache |
18 | */ |
19 | class RedisCache extends Cache |
20 | { |
21 | protected Redis $redis; |
22 | /** |
23 | * Redis Cache handler configurations. |
24 | * |
25 | * @var array<string,mixed> |
26 | */ |
27 | protected array $configs = [ |
28 | 'host' => '127.0.0.1', |
29 | 'port' => 6379, |
30 | 'timeout' => 0.0, |
31 | ]; |
32 | |
33 | public function __destruct() |
34 | { |
35 | $this->redis->close(); |
36 | } |
37 | |
38 | protected function initialize() : void |
39 | { |
40 | $this->connect(); |
41 | } |
42 | |
43 | protected function connect() : void |
44 | { |
45 | $this->redis = new Redis(); |
46 | $this->redis->connect( |
47 | $this->configs['host'], |
48 | $this->configs['port'], |
49 | $this->configs['timeout'] |
50 | ); |
51 | } |
52 | |
53 | public function get(string $key) : mixed |
54 | { |
55 | if (isset($this->debugCollector)) { |
56 | $start = \microtime(true); |
57 | return $this->addDebugGet( |
58 | $key, |
59 | $start, |
60 | $this->getValue($key) |
61 | ); |
62 | } |
63 | return $this->getValue($key); |
64 | } |
65 | |
66 | protected function getValue(string $key) : mixed |
67 | { |
68 | $value = $this->redis->get($this->renderKey($key)); |
69 | if ($value === false) { |
70 | return null; |
71 | } |
72 | return $this->unserialize($value); |
73 | } |
74 | |
75 | public function set(string $key, mixed $value, int $ttl = null) : bool |
76 | { |
77 | if (isset($this->debugCollector)) { |
78 | $start = \microtime(true); |
79 | return $this->addDebugSet( |
80 | $key, |
81 | $ttl, |
82 | $start, |
83 | $value, |
84 | $this->redis->set( |
85 | $this->renderKey($key), |
86 | $this->serialize($value), |
87 | $this->makeTtl($ttl) |
88 | ) |
89 | ); |
90 | } |
91 | return $this->redis->set( |
92 | $this->renderKey($key), |
93 | $this->serialize($value), |
94 | $this->makeTtl($ttl) |
95 | ); |
96 | } |
97 | |
98 | public function delete(string $key) : bool |
99 | { |
100 | if (isset($this->debugCollector)) { |
101 | $start = \microtime(true); |
102 | return $this->addDebugDelete( |
103 | $key, |
104 | $start, |
105 | (bool) $this->redis->del($this->renderKey($key)) |
106 | ); |
107 | } |
108 | return (bool) $this->redis->del($this->renderKey($key)); |
109 | } |
110 | |
111 | public function flush() : bool |
112 | { |
113 | if (isset($this->debugCollector)) { |
114 | $start = \microtime(true); |
115 | return $this->addDebugFlush( |
116 | $start, |
117 | $this->redis->flushAll() |
118 | ); |
119 | } |
120 | return $this->redis->flushAll(); |
121 | } |
122 | } |