1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
namespace Xervice\Redis\Business; |
6
|
|
|
|
7
|
|
|
use Xervice\Core\Business\Model\Facade\AbstractFacade; |
8
|
|
|
use Xervice\DataProvider\Business\Model\DataProvider\AbstractDataProvider; |
9
|
|
|
use Xervice\DataProvider\Business\Model\DataProvider\DataProviderInterface; |
10
|
|
|
use Xervice\Redis\Business\Exception\RedisException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @method \Xervice\Redis\Business\RedisBusinessFactory getFactory() |
14
|
|
|
* @method \Xervice\Redis\RedisConfig getConfig() |
15
|
|
|
*/ |
16
|
|
|
class RedisFacade extends AbstractFacade |
17
|
|
|
{ |
18
|
8 |
|
public function init(): void |
19
|
|
|
{ |
20
|
8 |
|
$this->getFactory()->createCommandProvider()->provideCommands(); |
21
|
8 |
|
} |
22
|
|
|
|
23
|
8 |
|
public function flushAll(): void |
24
|
|
|
{ |
25
|
8 |
|
$this->getFactory()->getRedisClient()->flushall(); |
26
|
8 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Clear old transactions |
30
|
|
|
*/ |
31
|
1 |
|
public function startTransaction(): void |
32
|
|
|
{ |
33
|
1 |
|
$this->getFactory()->getTransactionHandler()->clearCollection(); |
34
|
1 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $key |
38
|
|
|
* @param \Xervice\DataProvider\Business\Model\DataProvider\AbstractDataProvider $dataProvider |
39
|
|
|
*/ |
40
|
1 |
|
public function addTransaction(string $key, AbstractDataProvider $dataProvider): void |
41
|
|
|
{ |
42
|
1 |
|
$this->getFactory()->getTransactionHandler()->addToCollection( |
43
|
1 |
|
$this->getFactory()->createTransaction( |
44
|
1 |
|
$key, |
45
|
1 |
|
$dataProvider |
46
|
|
|
) |
47
|
|
|
); |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $key |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
4 |
|
public function exists(string $key): bool |
56
|
|
|
{ |
57
|
4 |
|
return $this->getFactory()->getRedisClient()->exists($key) !== 0; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
1 |
|
public function persistTransaction() |
64
|
|
|
{ |
65
|
1 |
|
return $this->mset( |
66
|
1 |
|
$this->getFactory()->getTransactionHandler()->getTransactionArray() |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $key |
72
|
|
|
* @param \Xervice\DataProvider\Business\Model\DataProvider\AbstractDataProvider $dataProvider |
73
|
|
|
* @param string $expireResolution |
74
|
|
|
* @param int $expireTTL |
75
|
|
|
* |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
|
|
public function setEx( |
79
|
|
|
string $key, |
80
|
|
|
AbstractDataProvider $dataProvider, |
81
|
|
|
string $expireResolution, |
82
|
|
|
int $expireTTL |
83
|
|
|
) { |
84
|
|
|
return $this->getFactory()->getRedisClient()->set( |
85
|
|
|
$key, |
86
|
|
|
$this->getFactory()->createConverter()->convertTo($dataProvider), |
87
|
|
|
$expireResolution, |
88
|
|
|
$expireTTL |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $key |
94
|
|
|
* @param \Xervice\DataProvider\Business\Model\DataProvider\AbstractDataProvider $dataProvider |
95
|
|
|
* |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
3 |
|
public function set(string $key, AbstractDataProvider $dataProvider) |
99
|
|
|
{ |
100
|
3 |
|
return $this->getFactory()->getRedisClient()->set( |
101
|
3 |
|
$key, |
102
|
3 |
|
$this->getFactory()->createConverter()->convertTo($dataProvider) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param array $list |
108
|
|
|
* |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
3 |
|
public function mset(array $list) |
112
|
|
|
{ |
113
|
3 |
|
return $this->getFactory()->getRedisClient()->mset( |
114
|
3 |
|
$this->getFactory()->createListConverter()->convertToList($list) |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $key |
120
|
|
|
* |
121
|
|
|
* @return \Xervice\DataProvider\Business\Model\DataProvider\DataProviderInterface |
122
|
|
|
* @throws \Xervice\Redis\Business\Exception\RedisException |
123
|
|
|
*/ |
124
|
4 |
|
public function get(string $key): DataProviderInterface |
125
|
|
|
{ |
126
|
4 |
|
$result = $this->getFactory()->getRedisClient()->get($key); |
127
|
|
|
|
128
|
4 |
|
if (!$result) { |
129
|
3 |
|
throw new RedisException('No value found for key ' . $key); |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
return $this->getFactory()->createConverter()->convertFrom( |
133
|
1 |
|
$result |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $search |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
public function mgetByKeys(string $search): array |
143
|
|
|
{ |
144
|
|
|
return $this->mget( |
145
|
|
|
$this->keys($search) |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $search |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
public function keys(string $search): array |
155
|
|
|
{ |
156
|
|
|
return $this->getFactory()->getRedisClient()->keys($search); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param array $keys |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
1 |
|
public function mget(array $keys): array |
165
|
|
|
{ |
166
|
1 |
|
$result = $this->getFactory()->getRedisClient()->mget($keys); |
167
|
|
|
|
168
|
1 |
|
return $this->getFactory()->createListConverter()->convertFromList( |
169
|
1 |
|
$result |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string $key |
175
|
|
|
* @param int $seconds |
176
|
|
|
*/ |
177
|
|
|
public function expire(string $key, int $seconds): void |
178
|
|
|
{ |
179
|
|
|
$this->getFactory()->getRedisClient()->expire($key, $seconds); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $key |
184
|
|
|
*/ |
185
|
1 |
|
public function delete(string $key): void |
186
|
|
|
{ |
187
|
1 |
|
$this->bulkDelete([$key]); |
188
|
1 |
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param array $keys |
192
|
|
|
*/ |
193
|
1 |
|
public function bulkDelete(array $keys): void |
194
|
|
|
{ |
195
|
1 |
|
$this->getFactory()->getRedisClient()->del($keys); |
196
|
1 |
|
} |
197
|
|
|
} |
198
|
|
|
|