1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Webdcg\Redis\Traits; |
4
|
|
|
|
5
|
|
|
use Webdcg\Redis\Exceptions\SetOperationException; |
6
|
|
|
|
7
|
|
|
trait SortedSets |
8
|
|
|
{ |
9
|
|
|
/* |
10
|
|
|
* Available Set Operations |
11
|
|
|
*/ |
12
|
|
|
protected $SET_OPERATIONS = ['SUM', 'MIN', 'MAX']; |
13
|
|
|
|
14
|
|
|
public function bzPop(): bool |
15
|
|
|
{ |
16
|
|
|
return false; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Add one or more members to a sorted set or update its score if it |
21
|
|
|
* already exists. |
22
|
|
|
* |
23
|
|
|
* See: https://redis.io/commands/zadd. |
24
|
|
|
* |
25
|
|
|
* @param string $key |
26
|
|
|
* @param float $score |
27
|
|
|
* @param mixed] $member |
|
|
|
|
28
|
|
|
* |
29
|
|
|
* @return int 1 if the element is added. 0 otherwise. |
30
|
|
|
*/ |
31
|
|
|
public function zAdd(string $key, float $score, $member): int |
32
|
|
|
{ |
33
|
|
|
return $this->redis->zAdd($key, $score, $member); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get the number of members in a sorted set. |
38
|
|
|
* See: https://redis.io/commands/zcard. |
39
|
|
|
* |
40
|
|
|
* @param string $key |
41
|
|
|
* |
42
|
|
|
* @return int The Set's cardinality |
43
|
|
|
*/ |
44
|
|
|
public function zCard(string $key): int |
45
|
|
|
{ |
46
|
|
|
return $this->redis->zCard($key); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the number of members in a sorted set. |
51
|
|
|
* |
52
|
|
|
* Note: zSize is an alias for zCard and will be removed in future |
53
|
|
|
* versions of phpredis. |
54
|
|
|
* |
55
|
|
|
* See: https://redis.io/commands/zcard. |
56
|
|
|
* |
57
|
|
|
* @param string $key |
58
|
|
|
* |
59
|
|
|
* @return int The Set's cardinality |
60
|
|
|
*/ |
61
|
|
|
public function zSize(string $key): int |
62
|
|
|
{ |
63
|
|
|
return $this->redis->zCard($key); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns the number of elements of the sorted set stored at the specified |
68
|
|
|
* key which have scores in the range [start, end]. Adding a parenthesis |
69
|
|
|
* before start or end excludes it from the range. +inf and -inf are also |
70
|
|
|
* valid limits. |
71
|
|
|
* |
72
|
|
|
* See: https://redis.io/commands/zcount. |
73
|
|
|
* |
74
|
|
|
* @param string $key |
75
|
|
|
* @param mixed|int|string $start |
76
|
|
|
* @param mixed|int|string $end |
77
|
|
|
* |
78
|
|
|
* @return int the size of a corresponding zRangeByScore. |
79
|
|
|
*/ |
80
|
|
|
public function zCount(string $key, $start, $end): int |
81
|
|
|
{ |
82
|
|
|
return $this->redis->zCount($key, $start, $end); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Increments the score of a member from a sorted set by a given amount. |
87
|
|
|
* |
88
|
|
|
* See: https://redis.io/commands/zincrby. |
89
|
|
|
* |
90
|
|
|
* @param string $key |
91
|
|
|
* @param float $value (double) value that will be added to the |
92
|
|
|
* member's score). |
93
|
|
|
* @param string $member |
94
|
|
|
* |
95
|
|
|
* @return float the new value |
96
|
|
|
*/ |
97
|
|
|
public function zIncrBy(string $key, float $value, $member): float |
98
|
|
|
{ |
99
|
|
|
return $this->redis->zIncrBy($key, $value, $member); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Creates an intersection of sorted sets given in second argument. |
104
|
|
|
* The result of the union will be stored in the sorted set defined by the |
105
|
|
|
* first argument. |
106
|
|
|
* |
107
|
|
|
* The third optional argument defines weights to apply to the sorted sets |
108
|
|
|
* in input. In this case, the weights will be multiplied by the score of |
109
|
|
|
* each element in the sorted set before applying the aggregation. The |
110
|
|
|
* forth argument defines the AGGREGATE option which specify how the |
111
|
|
|
* results of the union are aggregated. |
112
|
|
|
* |
113
|
|
|
* See: https://redis.io/commands/zinterstore. |
114
|
|
|
* |
115
|
|
|
* @param string $keyOutput |
116
|
|
|
* @param array $arrayZSetKeys |
117
|
|
|
* @param array $arrayWeights |
118
|
|
|
* @param string $aggregateFunction Either "SUM", "MIN", or "MAX": |
119
|
|
|
* defines the behaviour to use on |
120
|
|
|
* duplicate entries during the |
121
|
|
|
* zInterStore. |
122
|
|
|
* |
123
|
|
|
* @return int The number of values in the new |
124
|
|
|
* sorted set. |
125
|
|
|
*/ |
126
|
|
|
public function zInterStore( |
127
|
|
|
string $keyOutput, |
128
|
|
|
array $arrayZSetKeys, |
129
|
|
|
?array $arrayWeights = null, |
130
|
|
|
?string $aggregateFunction = null |
131
|
|
|
): int { |
132
|
|
|
// Validate Aggregate Function |
133
|
|
|
if (!is_null($aggregateFunction) && !is_null($arrayWeights)) { |
134
|
|
|
$operation = strtoupper($aggregateFunction); |
135
|
|
|
|
136
|
|
|
if (!in_array($operation, $this->SET_OPERATIONS)) { |
137
|
|
|
throw new SetOperationException('Operation not supported', 1); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this->redis->zInterStore($keyOutput, $arrayZSetKeys, $arrayWeights, $operation); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// Call using Weights |
144
|
|
|
if (!is_null($arrayWeights)) { |
145
|
|
|
return $this->redis->zInterStore($keyOutput, $arrayZSetKeys, $arrayWeights); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// Make simplest call just with the required params |
149
|
|
|
return $this->redis->zInterStore($keyOutput, $arrayZSetKeys); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Creates an intersection of sorted sets given in second argument. |
154
|
|
|
* The result of the union will be stored in the sorted set defined by the |
155
|
|
|
* first argument. |
156
|
|
|
* |
157
|
|
|
* The third optional argument defines weights to apply to the sorted sets |
158
|
|
|
* in input. In this case, the weights will be multiplied by the score of |
159
|
|
|
* each element in the sorted set before applying the aggregation. The |
160
|
|
|
* forth argument defines the AGGREGATE option which specify how the |
161
|
|
|
* results of the union are aggregated. |
162
|
|
|
* |
163
|
|
|
* Note: zInter is an alias for zinterstore and will be removed in future |
164
|
|
|
* versions of phpredis. |
165
|
|
|
* |
166
|
|
|
* See: https://redis.io/commands/zinterstore. |
167
|
|
|
* |
168
|
|
|
* @param string $keyOutput |
169
|
|
|
* @param array $arrayZSetKeys |
170
|
|
|
* @param array $arrayWeights |
171
|
|
|
* @param string $aggregateFunction Either "SUM", "MIN", or "MAX": |
172
|
|
|
* defines the behaviour to use on |
173
|
|
|
* duplicate entries during the |
174
|
|
|
* zInterStore. |
175
|
|
|
* |
176
|
|
|
* @return int The number of values in the new |
177
|
|
|
* sorted set. |
178
|
|
|
*/ |
179
|
|
|
public function zInter( |
180
|
|
|
string $keyOutput, |
181
|
|
|
array $arrayZSetKeys, |
182
|
|
|
?array $arrayWeights = null, |
183
|
|
|
?string $aggregateFunction = null |
184
|
|
|
): int { |
185
|
|
|
// Validate Aggregate Function |
186
|
|
|
if (!is_null($aggregateFunction) && !is_null($arrayWeights)) { |
187
|
|
|
$operation = strtoupper($aggregateFunction); |
188
|
|
|
|
189
|
|
|
if (!in_array($operation, $this->SET_OPERATIONS)) { |
190
|
|
|
throw new SetOperationException('Operation not supported', 1); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $this->redis->zInterStore($keyOutput, $arrayZSetKeys, $arrayWeights, $operation); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
// Call using Weights |
197
|
|
|
if (!is_null($arrayWeights)) { |
198
|
|
|
return $this->redis->zInterStore($keyOutput, $arrayZSetKeys, $arrayWeights); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
// Make simplest call just with the required params |
202
|
|
|
return $this->redis->zInterStore($keyOutput, $arrayZSetKeys); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Can pop the highest or lowest scoring members from one ZSETs. |
207
|
|
|
* There are two commands (ZPOPMIN and ZPOPMAX for popping the lowest and |
208
|
|
|
* highest scoring elements respectively.). |
209
|
|
|
* |
210
|
|
|
* See: https://redis.io/commands/zpopmin. |
211
|
|
|
* See: https://redis.io/commands/zpopmax. |
212
|
|
|
* |
213
|
|
|
* @param string $key |
214
|
|
|
* @param int|integer $count |
215
|
|
|
* @param bool|boolean $max |
216
|
|
|
* |
217
|
|
|
* @return array Either an array with the key member and |
218
|
|
|
* score of the highest or lowest element |
219
|
|
|
* or an empty array if there is no element |
220
|
|
|
* available. |
221
|
|
|
*/ |
222
|
|
|
public function zPop(string $key, int $count = 1, bool $max = true): array |
223
|
|
|
{ |
224
|
|
|
return $max ? $this->redis->zPopMax($key, $count) : $this->redis->zPopMin($key, $count); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Can pop the lowest scoring members from one ZSETs. |
229
|
|
|
* |
230
|
|
|
* See: https://redis.io/commands/zpopmin. |
231
|
|
|
* |
232
|
|
|
* @param string $key |
233
|
|
|
* @param int|integer $count |
234
|
|
|
* |
235
|
|
|
* @return array Either an array with the key member and |
236
|
|
|
* score of the highest or lowest element |
237
|
|
|
* or an empty array if there is no element |
238
|
|
|
* available. |
239
|
|
|
*/ |
240
|
|
|
public function zPopMin(string $key, int $count = 1): array |
241
|
|
|
{ |
242
|
|
|
return $this->redis->zPopMin($key, $count); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Can pop the highest scoring members from one ZSETs. |
247
|
|
|
* |
248
|
|
|
* See: https://redis.io/commands/zpopmax. |
249
|
|
|
* |
250
|
|
|
* @param string $key |
251
|
|
|
* @param int|integer $count |
252
|
|
|
* |
253
|
|
|
* @return array Either an array with the key member and |
254
|
|
|
* score of the highest or lowest element |
255
|
|
|
* or an empty array if there is no element |
256
|
|
|
* available. |
257
|
|
|
*/ |
258
|
|
|
public function zPopMax(string $key, int $count = 1): array |
259
|
|
|
{ |
260
|
|
|
return $this->redis->zPopMax($key, $count); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Returns a range of elements from the ordered set stored at the specified |
265
|
|
|
* key, with values in the range [start, end]. |
266
|
|
|
* |
267
|
|
|
* Start and stop are interpreted as zero-based indices: |
268
|
|
|
* 0 the first element, 1 the second ... |
269
|
|
|
* -1 the last element, -2 the penultimate ... |
270
|
|
|
* |
271
|
|
|
* See: https://redis.io/commands/zrange. |
272
|
|
|
* |
273
|
|
|
* @param string $key |
274
|
|
|
* @param int|integer $start |
275
|
|
|
* @param int|integer $end |
276
|
|
|
* @param bool|boolean $withScores |
277
|
|
|
* |
278
|
|
|
* @return array Array containing the values in specified range. |
279
|
|
|
*/ |
280
|
|
|
public function zRange(string $key, int $start = 0, int $end = -1, bool $withScores = false): array |
281
|
|
|
{ |
282
|
|
|
return $this->redis->zRange($key, $start, $end, $withScores); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
public function zRangeByScore(): bool |
286
|
|
|
{ |
287
|
|
|
return false; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
public function zRevRangeByScore(): bool |
291
|
|
|
{ |
292
|
|
|
return false; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function zRangeByLex(): bool |
296
|
|
|
{ |
297
|
|
|
return false; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
public function zRank(): bool |
301
|
|
|
{ |
302
|
|
|
return false; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
public function zRevRank(): bool |
306
|
|
|
{ |
307
|
|
|
return false; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
public function zRem(): bool |
311
|
|
|
{ |
312
|
|
|
return false; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function zDelete(): bool |
316
|
|
|
{ |
317
|
|
|
return false; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
public function zRemove(): bool |
321
|
|
|
{ |
322
|
|
|
return false; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
public function zRemRangeByRank(): bool |
326
|
|
|
{ |
327
|
|
|
return false; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
public function zDeleteRangeByRank(): bool |
331
|
|
|
{ |
332
|
|
|
return false; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function zRemRangeByScore(): bool |
336
|
|
|
{ |
337
|
|
|
return false; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
public function zDeleteRangeByScore(): bool |
341
|
|
|
{ |
342
|
|
|
return false; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
public function zRemoveRangeByScore(): bool |
346
|
|
|
{ |
347
|
|
|
return false; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
public function zRevRange(): bool |
351
|
|
|
{ |
352
|
|
|
return false; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
public function zScore(): bool |
356
|
|
|
{ |
357
|
|
|
return false; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
public function zunionstore(): bool |
361
|
|
|
{ |
362
|
|
|
return false; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
public function zUnion(): bool |
366
|
|
|
{ |
367
|
|
|
return false; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
public function zScan(): bool |
371
|
|
|
{ |
372
|
|
|
return false; |
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.