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