Issues (1251)

src/Valkyrja/Cache/RedisCache.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Cache;
15
16
use Override;
0 ignored issues
show
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Predis\Client;
18
use Valkyrja\Cache\Contract\Cache as Contract;
19
use Valkyrja\Cache\Tagger\Contract\Tagger;
20
use Valkyrja\Cache\Tagger\Tagger as TagClass;
21
22
/**
23
 * Class RedisCache.
24
 *
25
 * @author Melech Mizrachi
26
 */
27
class RedisCache implements Contract
28
{
29
    /**
30
     * RedisCache constructor.
31
     *
32
     * @param Client $client The predis client
33
     * @param string $prefix The prefix
34
     */
35
    public function __construct(
36
        protected Client $client,
37
        protected string $prefix = ''
38
    ) {
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    #[Override]
45
    public function has(string $key): bool
46
    {
47
        return (bool) $this->client->exists($this->getKey($key));
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53
    #[Override]
54
    public function get(string $key): string|null
55
    {
56
        return $this->client->get($this->getKey($key));
57
    }
58
59
    /**
60
     * @inheritDoc
61
     *
62
     * @psalm-suppress MixedReturnTypeCoercion
63
     */
64
    #[Override]
65
    public function many(string ...$keys): array
66
    {
67
        $prefixedKeys = [];
68
69
        foreach ($keys as $key) {
70
            $prefixedKeys[] = $this->getKey($key);
71
        }
72
73
        return $this->client->mget($prefixedKeys);
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    #[Override]
80
    public function put(string $key, string $value, int $minutes): void
81
    {
82
        $this->client->setex($this->getKey($key), $minutes * 60, $value);
83
    }
84
85
    /**
86
     * @inheritDoc
87
     */
88
    #[Override]
89
    public function putMany(array $values, int $minutes): void
90
    {
91
        $seconds = $minutes * 60;
92
93
        $this->client->transaction(
94
            function (Client $client) use ($values, $seconds): void {
95
                foreach ($values as $key => $value) {
96
                    $client->setex($this->getKey($key), $seconds, $value);
97
                }
98
            }
99
        );
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105
    #[Override]
106
    public function increment(string $key, int $value = 1): int
107
    {
108
        return $this->client->incrby($this->getKey($key), $value);
109
    }
110
111
    /**
112
     * @inheritDoc
113
     */
114
    #[Override]
115
    public function decrement(string $key, int $value = 1): int
116
    {
117
        return $this->client->decrby($this->getKey($key), $value);
118
    }
119
120
    /**
121
     * @inheritDoc
122
     */
123
    #[Override]
124
    public function forever(string $key, $value): void
125
    {
126
        $this->client->set($this->getKey($key), $value);
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132
    #[Override]
133
    public function forget(string $key): bool
134
    {
135
        return (bool) $this->client->del([$this->getKey($key)]);
136
    }
137
138
    /**
139
     * @inheritDoc
140
     */
141
    #[Override]
142
    public function flush(): bool
143
    {
144
        return (bool) $this->client->flushdb();
145
    }
146
147
    /**
148
     * @inheritDoc
149
     */
150
    #[Override]
151
    public function getPrefix(): string
152
    {
153
        return $this->prefix;
154
    }
155
156
    /**
157
     * @inheritDoc
158
     */
159
    #[Override]
160
    public function getTagger(string ...$tags): Tagger
161
    {
162
        return TagClass::make($this, ...$tags);
163
    }
164
165
    /**
166
     * Get key.
167
     *
168
     * @param string $key
169
     *
170
     * @return string
171
     */
172
    protected function getKey(string $key): string
173
    {
174
        return $this->getPrefix() . $key;
175
    }
176
}
177