• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aplus-framework / cache / 16306742406

15 Jul 2025 11:44PM UTC coverage: 95.194%. Remained the same
16306742406

push

github

natanfelles
Add since tag

515 of 541 relevant lines covered (95.19%)

55.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/RedisCache.php
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 Framework\Log\Logger;
13
use Override;
14
use Redis;
15
use SensitiveParameter;
16

17
/**
18
 * Class RedisCache.
19
 *
20
 * @package cache
21
 */
22
class RedisCache extends Cache
23
{
24
    protected Redis $redis;
25
    /**
26
     * Redis Cache handler configurations.
27
     *
28
     * @var array<string,mixed>
29
     */
30
    protected array $configs = [
31
        'host' => '127.0.0.1',
32
        'port' => 6379,
33
        'timeout' => 0.0,
34
        'password' => null,
35
        'database' => null,
36
    ];
37

38
    /**
39
     * RedisCache constructor.
40
     *
41
     * @param Redis|array<string,mixed>|null $configs Driver specific
42
     * configurations. Set null to not initialize or a custom Redis object.
43
     * @param string|null $prefix Keys prefix
44
     * @param Serializer|string $serializer Data serializer
45
     * @param Logger|null $logger Logger instance
46
     */
47
    public function __construct(
48
        #[SensitiveParameter]
49
        Redis | array | null $configs = [],
50
        ?string $prefix = null,
51
        Serializer | string $serializer = Serializer::PHP,
52
        ?Logger $logger = null
53
    ) {
54
        parent::__construct($configs, $prefix, $serializer, $logger);
100✔
55
        if ($configs instanceof Redis) {
100✔
56
            $this->setRedis($configs);
5✔
57
            $this->setAutoClose(false);
5✔
58
        }
59
    }
60

61
    protected function initialize() : void
62
    {
63
        $this->connect();
100✔
64
    }
65

66
    protected function connect() : void
67
    {
68
        $this->redis = new Redis();
100✔
69
        $this->redis->connect(
100✔
70
            $this->configs['host'],
100✔
71
            $this->configs['port'],
100✔
72
            $this->configs['timeout']
100✔
73
        );
100✔
74
        if (isset($this->configs['password'])) {
100✔
75
            $this->redis->auth($this->configs['password']);
5✔
76
        }
77
        if (isset($this->configs['database'])) {
100✔
78
            $this->redis->select($this->configs['database']);
5✔
79
        }
80
    }
81

82
    /**
83
     * Set custom Redis instance.
84
     *
85
     * @since 3.2
86
     *
87
     * @param Redis $redis
88
     *
89
     * @return static
90
     */
91
    public function setRedis(Redis $redis) : static
92
    {
93
        $this->redis = $redis;
10✔
94
        return $this;
10✔
95
    }
96

97
    /**
98
     * Get Redis instance or null.
99
     *
100
     * @since 3.2
101
     *
102
     * @return Redis|null
103
     */
104
    public function getRedis() : ?Redis
105
    {
106
        return $this->redis ?? null;
10✔
107
    }
108

109
    public function get(string $key) : mixed
110
    {
111
        if (isset($this->debugCollector)) {
55✔
112
            $start = \microtime(true);
10✔
113
            return $this->addDebugGet(
10✔
114
                $key,
10✔
115
                $start,
10✔
116
                $this->getValue($key)
10✔
117
            );
10✔
118
        }
119
        return $this->getValue($key);
45✔
120
    }
121

122
    protected function getValue(string $key) : mixed
123
    {
124
        $value = $this->redis->get($this->renderKey($key));
55✔
125
        if ($value === false) {
55✔
126
            return null;
55✔
127
        }
128
        return $this->unserialize($value);
45✔
129
    }
130

131
    public function set(string $key, mixed $value, ?int $ttl = null) : bool
132
    {
133
        if (isset($this->debugCollector)) {
50✔
134
            $start = \microtime(true);
5✔
135
            return $this->addDebugSet(
5✔
136
                $key,
5✔
137
                $ttl,
5✔
138
                $start,
5✔
139
                $value,
5✔
140
                $this->redis->set(
5✔
141
                    $this->renderKey($key),
5✔
142
                    $this->serialize($value),
5✔
143
                    $this->makeTtl($ttl)
5✔
144
                )
5✔
145
            );
5✔
146
        }
147
        return $this->redis->set(
45✔
148
            $this->renderKey($key),
45✔
149
            $this->serialize($value),
45✔
150
            $this->makeTtl($ttl)
45✔
151
        );
45✔
152
    }
153

154
    public function delete(string $key) : bool
155
    {
156
        if (isset($this->debugCollector)) {
15✔
157
            $start = \microtime(true);
5✔
158
            return $this->addDebugDelete(
5✔
159
                $key,
5✔
160
                $start,
5✔
161
                (bool) $this->redis->del($this->renderKey($key))
5✔
162
            );
5✔
163
        }
164
        return (bool) $this->redis->del($this->renderKey($key));
10✔
165
    }
166

167
    public function flush() : bool
168
    {
169
        if (isset($this->debugCollector)) {
100✔
170
            $start = \microtime(true);
20✔
171
            return $this->addDebugFlush(
20✔
172
                $start,
20✔
173
                $this->redis->flushAll()
20✔
174
            );
20✔
175
        }
176
        return $this->redis->flushAll();
80✔
177
    }
178

179
    #[Override]
180
    public function close() : bool
181
    {
182
        return $this->redis->close();
100✔
183
    }
184
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc