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