1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ackintosh\Ganesha\Storage\Adapter; |
4
|
|
|
|
5
|
|
|
use Ackintosh\Ganesha\Exception\StorageException; |
6
|
|
|
use Exception; |
7
|
|
|
|
8
|
|
|
class RedisStore |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var \Predis\Client|\Redis|\RedisArray|\RedisCluster |
12
|
|
|
*/ |
13
|
|
|
private $redis; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param \Redis|\RedisArray|\RedisCluster|\Predis\Client $redisClient |
17
|
|
|
* |
18
|
|
|
* @throws \InvalidArgumentException if redis client is not supported |
19
|
|
|
*/ |
20
|
|
|
public function __construct($redisClient) |
21
|
|
|
{ |
22
|
|
|
if ( |
23
|
|
|
!$redisClient instanceof \Redis |
24
|
|
|
&& !$redisClient instanceof \RedisArray |
25
|
|
|
&& !$redisClient instanceof \RedisCluster |
26
|
|
|
&& !$redisClient instanceof \Predis\Client |
|
|
|
|
27
|
|
|
) { |
28
|
|
|
throw new \InvalidArgumentException(sprintf( |
29
|
|
|
'%s() expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\Client, %s given', |
30
|
|
|
__METHOD__, |
31
|
|
|
\is_object($redisClient) ? \get_class($redisClient) : \gettype($redisClient) |
32
|
|
|
)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->redis = $redisClient; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Deletes the elements of the sorted set stored at the specified key which have scores in the range [start,end]. |
40
|
|
|
* |
41
|
|
|
* @param string $key |
42
|
|
|
* @param float|string $start double or "+inf" or "-inf" string |
43
|
|
|
* @param float|string $end double or "+inf" or "-inf" string |
44
|
|
|
* |
45
|
|
|
* @return int|false The number of values deleted from the sorted set |
46
|
|
|
* |
47
|
|
|
* @throws StorageException |
48
|
|
|
*/ |
49
|
|
|
public function zRemRangeByScore(string $key, $start, $end) |
50
|
|
|
{ |
51
|
|
|
try { |
52
|
|
|
return $this->redis->zRemRangeByScore($key, $start, $end); |
|
|
|
|
53
|
|
|
} catch (Exception $exception) { |
54
|
|
|
throw new StorageException($exception->getMessage(), $exception->getCode(), $exception); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns the cardinality of an ordered set. |
60
|
|
|
* |
61
|
|
|
* @param string $key |
62
|
|
|
* |
63
|
|
|
* @return int the set's cardinality |
64
|
|
|
* |
65
|
|
|
* @throws StorageException |
66
|
|
|
*/ |
67
|
|
|
public function zCard(string $key): int |
68
|
|
|
{ |
69
|
|
|
try { |
70
|
|
|
$r = $this->redis->zCard($key); |
|
|
|
|
71
|
|
|
} catch (Exception $exception) { |
72
|
|
|
throw new StorageException($exception->getMessage(), $exception->getCode(), $exception); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($r === false) { |
76
|
|
|
throw new StorageException(sprintf( |
77
|
|
|
"Failed to execute zCard command. key: %s", |
78
|
|
|
$key |
79
|
|
|
)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $r; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Adds the specified member with a given score to the sorted set stored at key. |
87
|
|
|
* |
88
|
|
|
* @param string $key Required key |
89
|
|
|
* @param float $score1 Required score |
90
|
|
|
* @param string $value1 Required value |
91
|
|
|
* |
92
|
|
|
* @return int Number of values added |
93
|
|
|
* |
94
|
|
|
* @throws StorageException |
95
|
|
|
*/ |
96
|
|
|
public function zAdd(string $key, float $score1, string $value1): int |
97
|
|
|
{ |
98
|
|
|
try { |
99
|
|
|
$r = $this->redis->zAdd($key, $score1, $value1); |
|
|
|
|
100
|
|
|
} catch (Exception $exception) { |
101
|
|
|
throw new StorageException($exception->getMessage(), $exception->getCode(), $exception); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($r === false) { |
105
|
|
|
throw new StorageException(sprintf( |
106
|
|
|
"Failed to execute zAdd command. key: %s, score1: %s, value1: %s", |
107
|
|
|
$key, |
108
|
|
|
$score1, |
109
|
|
|
$value1 |
110
|
|
|
)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $r; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns a range of elements from the ordered set stored at the specified key, |
118
|
|
|
* with values in the range [start, end]. start and stop are interpreted as zero-based indices: |
119
|
|
|
* 0 the first element, |
120
|
|
|
* 1 the second ... |
121
|
|
|
* -1 the last element, |
122
|
|
|
* -2 the penultimate ... |
123
|
|
|
* |
124
|
|
|
* @param string $key |
125
|
|
|
* @param int $start |
126
|
|
|
* @param int $end |
127
|
|
|
* |
128
|
|
|
* @return array Array containing the values in specified range. |
129
|
|
|
* |
130
|
|
|
* @throws StorageException |
131
|
|
|
*/ |
132
|
|
|
public function zRange(string $key, int $start, int $end): array |
133
|
|
|
{ |
134
|
|
|
try { |
135
|
|
|
$elements = $this->redis->zRange($key, $start, $end); |
|
|
|
|
136
|
|
|
return !$elements ? [] : $elements; |
137
|
|
|
} catch (Exception $exception) { |
138
|
|
|
throw new StorageException($exception->getMessage(), $exception->getCode(), $exception); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set the string value in argument as value of the key. |
144
|
|
|
* |
145
|
|
|
* @param string $key |
146
|
|
|
* @param string $value |
147
|
|
|
* |
148
|
|
|
* @return bool TRUE if the command is successful. |
149
|
|
|
* |
150
|
|
|
* @throws StorageException |
151
|
|
|
*/ |
152
|
|
|
public function set(string $key, string $value): bool |
153
|
|
|
{ |
154
|
|
|
try { |
155
|
|
|
$r = $this->redis->set($key, $value); |
|
|
|
|
156
|
|
|
if (is_bool($r)) { |
157
|
|
|
return $r; |
158
|
|
|
} elseif ($r instanceof \Predis\Response\Status) { |
159
|
|
|
return $r->getPayload() === 'OK'; |
160
|
|
|
} else { |
161
|
|
|
throw new \LogicException("Could not handle the response: " . serialize($r)); |
162
|
|
|
} |
163
|
|
|
} catch (Exception $exception) { |
164
|
|
|
throw new StorageException($exception->getMessage(), $exception->getCode(), $exception); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get the value related to the specified key |
170
|
|
|
* |
171
|
|
|
* @param string $key |
172
|
|
|
* |
173
|
|
|
* @return string|false If key didn't exist, FALSE is returned. Otherwise, the value related to this key is returned. |
174
|
|
|
* |
175
|
|
|
* @throws StorageException |
176
|
|
|
*/ |
177
|
|
|
public function get(string $key) |
178
|
|
|
{ |
179
|
|
|
try { |
180
|
|
|
$result = $this->redis->get($key); |
|
|
|
|
181
|
|
|
if ($this->redis instanceof \Predis\Client && $result === null) { |
182
|
|
|
return false; |
183
|
|
|
} |
184
|
|
|
return $result; |
185
|
|
|
} catch (Exception $exception) { |
186
|
|
|
throw new StorageException($exception->getMessage(), $exception->getCode(), $exception); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|