1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Webdcg\Redis\Traits; |
4
|
|
|
|
5
|
|
|
use Webdcg\Redis\Exceptions\InvalidArgumentException; |
6
|
|
|
use Webdcg\Redis\Exceptions\SetOperationException; |
7
|
|
|
use Webdcg\Redis\Exceptions\UnsupportedOptionException; |
8
|
|
|
|
9
|
|
|
trait SortedSets |
10
|
|
|
{ |
11
|
|
|
/* |
12
|
|
|
* Available Set Operations |
13
|
|
|
*/ |
14
|
|
|
protected $SET_OPERATIONS = ['SUM', 'MIN', 'MAX']; |
15
|
|
|
|
16
|
|
|
public function bzPop(): bool |
17
|
|
|
{ |
18
|
|
|
return false; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Add one or more members to a sorted set or update its score if it |
24
|
|
|
* already exists. |
25
|
|
|
* |
26
|
|
|
* See: https://redis.io/commands/zadd. |
27
|
|
|
* |
28
|
|
|
* @param string $key |
29
|
|
|
* @param float $score |
30
|
|
|
* @param mixed] $member |
|
|
|
|
31
|
|
|
* |
32
|
|
|
* @return int 1 if the element is added. 0 otherwise. |
33
|
|
|
*/ |
34
|
|
|
public function zAdd(string $key, float $score, $member): int |
35
|
|
|
{ |
36
|
|
|
return $this->redis->zAdd($key, $score, $member); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the number of members in a sorted set. |
42
|
|
|
* See: https://redis.io/commands/zcard. |
43
|
|
|
* |
44
|
|
|
* @param string $key |
45
|
|
|
* |
46
|
|
|
* @return int The Set's cardinality |
47
|
|
|
*/ |
48
|
|
|
public function zCard(string $key): int |
49
|
|
|
{ |
50
|
|
|
return $this->redis->zCard($key); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the number of members in a sorted set. |
56
|
|
|
* |
57
|
|
|
* Note: zSize is an alias for zCard and will be removed in future |
58
|
|
|
* versions of phpredis. |
59
|
|
|
* |
60
|
|
|
* See: https://redis.io/commands/zcard. |
61
|
|
|
* |
62
|
|
|
* @param string $key |
63
|
|
|
* |
64
|
|
|
* @return int The Set's cardinality |
65
|
|
|
*/ |
66
|
|
|
public function zSize(string $key): int |
67
|
|
|
{ |
68
|
|
|
return $this->redis->zCard($key); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Returns the number of elements of the sorted set stored at the specified |
74
|
|
|
* key which have scores in the range [start, end]. Adding a parenthesis |
75
|
|
|
* before start or end excludes it from the range. +inf and -inf are also |
76
|
|
|
* valid limits. |
77
|
|
|
* |
78
|
|
|
* See: https://redis.io/commands/zcount. |
79
|
|
|
* |
80
|
|
|
* @param string $key |
81
|
|
|
* @param mixed|int|string $start |
82
|
|
|
* @param mixed|int|string $end |
83
|
|
|
* |
84
|
|
|
* @return int the size of a corresponding zRangeByScore. |
85
|
|
|
*/ |
86
|
|
|
public function zCount(string $key, $start, $end): int |
87
|
|
|
{ |
88
|
|
|
return $this->redis->zCount($key, $start, $end); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Increments the score of a member from a sorted set by a given amount. |
94
|
|
|
* |
95
|
|
|
* See: https://redis.io/commands/zincrby. |
96
|
|
|
* |
97
|
|
|
* @param string $key |
98
|
|
|
* @param float $value (double) value that will be added to the |
99
|
|
|
* member's score). |
100
|
|
|
* @param string $member |
101
|
|
|
* |
102
|
|
|
* @return float the new value |
103
|
|
|
*/ |
104
|
|
|
public function zIncrBy(string $key, float $value, $member): float |
105
|
|
|
{ |
106
|
|
|
return $this->redis->zIncrBy($key, $value, $member); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Creates an intersection of sorted sets given in second argument. |
112
|
|
|
* The result of the union will be stored in the sorted set defined by the |
113
|
|
|
* first argument. |
114
|
|
|
* |
115
|
|
|
* The third optional argument defines weights to apply to the sorted sets |
116
|
|
|
* in input. In this case, the weights will be multiplied by the score of |
117
|
|
|
* each element in the sorted set before applying the aggregation. The |
118
|
|
|
* forth argument defines the AGGREGATE option which specify how the |
119
|
|
|
* results of the union are aggregated. |
120
|
|
|
* |
121
|
|
|
* See: https://redis.io/commands/zinterstore. |
122
|
|
|
* |
123
|
|
|
* @param string $keyOutput |
124
|
|
|
* @param array $arrayZSetKeys |
125
|
|
|
* @param array $arrayWeights |
126
|
|
|
* @param string $aggregateFunction Either "SUM", "MIN", or "MAX": |
127
|
|
|
* defines the behaviour to use on |
128
|
|
|
* duplicate entries during the |
129
|
|
|
* zInterStore. |
130
|
|
|
* |
131
|
|
|
* @return int The number of values in the new |
132
|
|
|
* sorted set. |
133
|
|
|
*/ |
134
|
|
|
public function zInterStore( |
135
|
|
|
string $keyOutput, |
136
|
|
|
array $arrayZSetKeys, |
137
|
|
|
?array $arrayWeights = null, |
138
|
|
|
?string $aggregateFunction = null |
139
|
|
|
): int { |
140
|
|
|
// Validate Aggregate Function |
141
|
|
|
if (!is_null($aggregateFunction) && !is_null($arrayWeights)) { |
142
|
|
|
$operation = strtoupper($aggregateFunction); |
143
|
|
|
|
144
|
|
|
if (!in_array($operation, $this->SET_OPERATIONS)) { |
145
|
|
|
throw new SetOperationException('Operation not supported', 1); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $this->redis->zInterStore($keyOutput, $arrayZSetKeys, $arrayWeights, $operation); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// Call using Weights |
152
|
|
|
if (!is_null($arrayWeights)) { |
153
|
|
|
return $this->redis->zInterStore($keyOutput, $arrayZSetKeys, $arrayWeights); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// Make simplest call just with the required params |
157
|
|
|
return $this->redis->zInterStore($keyOutput, $arrayZSetKeys); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Creates an intersection of sorted sets given in second argument. |
163
|
|
|
* The result of the union will be stored in the sorted set defined by the |
164
|
|
|
* first argument. |
165
|
|
|
* |
166
|
|
|
* The third optional argument defines weights to apply to the sorted sets |
167
|
|
|
* in input. In this case, the weights will be multiplied by the score of |
168
|
|
|
* each element in the sorted set before applying the aggregation. The |
169
|
|
|
* forth argument defines the AGGREGATE option which specify how the |
170
|
|
|
* results of the union are aggregated. |
171
|
|
|
* |
172
|
|
|
* Note: zInter is an alias for zinterstore and will be removed in future |
173
|
|
|
* versions of phpredis. |
174
|
|
|
* |
175
|
|
|
* See: https://redis.io/commands/zinterstore. |
176
|
|
|
* |
177
|
|
|
* @param string $keyOutput |
178
|
|
|
* @param array $arrayZSetKeys |
179
|
|
|
* @param array $arrayWeights |
180
|
|
|
* @param string $aggregateFunction Either "SUM", "MIN", or "MAX": |
181
|
|
|
* defines the behaviour to use on |
182
|
|
|
* duplicate entries during the |
183
|
|
|
* zInterStore. |
184
|
|
|
* |
185
|
|
|
* @return int The number of values in the new |
186
|
|
|
* sorted set. |
187
|
|
|
*/ |
188
|
|
|
public function zInter( |
189
|
|
|
string $keyOutput, |
190
|
|
|
array $arrayZSetKeys, |
191
|
|
|
?array $arrayWeights = null, |
192
|
|
|
?string $aggregateFunction = null |
193
|
|
|
): int { |
194
|
|
|
// Validate Aggregate Function |
195
|
|
|
if (!is_null($aggregateFunction) && !is_null($arrayWeights)) { |
196
|
|
|
$operation = strtoupper($aggregateFunction); |
197
|
|
|
|
198
|
|
|
if (!in_array($operation, $this->SET_OPERATIONS)) { |
199
|
|
|
throw new SetOperationException('Operation not supported', 1); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $this->redis->zInterStore($keyOutput, $arrayZSetKeys, $arrayWeights, $operation); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// Call using Weights |
206
|
|
|
if (!is_null($arrayWeights)) { |
207
|
|
|
return $this->redis->zInterStore($keyOutput, $arrayZSetKeys, $arrayWeights); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// Make simplest call just with the required params |
211
|
|
|
return $this->redis->zInterStore($keyOutput, $arrayZSetKeys); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Can pop the highest or lowest scoring members from one ZSETs. |
217
|
|
|
* There are two commands (ZPOPMIN and ZPOPMAX for popping the lowest and |
218
|
|
|
* highest scoring elements respectively.). |
219
|
|
|
* |
220
|
|
|
* See: https://redis.io/commands/zpopmin. |
221
|
|
|
* See: https://redis.io/commands/zpopmax. |
222
|
|
|
* |
223
|
|
|
* @param string $key |
224
|
|
|
* @param int|integer $count |
225
|
|
|
* @param bool|boolean $max |
226
|
|
|
* |
227
|
|
|
* @return array Either an array with the key member and |
228
|
|
|
* score of the highest or lowest element |
229
|
|
|
* or an empty array if there is no element |
230
|
|
|
* available. |
231
|
|
|
*/ |
232
|
|
|
public function zPop(string $key, int $count = 1, bool $max = true): array |
233
|
|
|
{ |
234
|
|
|
return $max ? $this->redis->zPopMax($key, $count) : $this->redis->zPopMin($key, $count); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Can pop the lowest scoring members from one ZSETs. |
240
|
|
|
* |
241
|
|
|
* See: https://redis.io/commands/zpopmin. |
242
|
|
|
* |
243
|
|
|
* @param string $key |
244
|
|
|
* @param int|integer $count |
245
|
|
|
* |
246
|
|
|
* @return array Either an array with the key member and |
247
|
|
|
* score of the highest or lowest element |
248
|
|
|
* or an empty array if there is no element |
249
|
|
|
* available. |
250
|
|
|
*/ |
251
|
|
|
public function zPopMin(string $key, int $count = 1): array |
252
|
|
|
{ |
253
|
|
|
return $this->redis->zPopMin($key, $count); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Can pop the highest scoring members from one ZSETs. |
259
|
|
|
* |
260
|
|
|
* See: https://redis.io/commands/zpopmax. |
261
|
|
|
* |
262
|
|
|
* @param string $key |
263
|
|
|
* @param int|integer $count |
264
|
|
|
* |
265
|
|
|
* @return array Either an array with the key member and |
266
|
|
|
* score of the highest or lowest element |
267
|
|
|
* or an empty array if there is no element |
268
|
|
|
* available. |
269
|
|
|
*/ |
270
|
|
|
public function zPopMax(string $key, int $count = 1): array |
271
|
|
|
{ |
272
|
|
|
return $this->redis->zPopMax($key, $count); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Returns a range of elements from the ordered set stored at the specified |
278
|
|
|
* key, with values in the range [start, end]. |
279
|
|
|
* |
280
|
|
|
* Start and stop are interpreted as zero-based indices: |
281
|
|
|
* 0 the first element, 1 the second ... |
282
|
|
|
* -1 the last element, -2 the penultimate ... |
283
|
|
|
* |
284
|
|
|
* See: https://redis.io/commands/zrange. |
285
|
|
|
* |
286
|
|
|
* @param string $key |
287
|
|
|
* @param int|integer $start |
288
|
|
|
* @param int|integer $end |
289
|
|
|
* @param bool|boolean $withScores |
290
|
|
|
* |
291
|
|
|
* @return array Array containing the values in specified range. |
292
|
|
|
*/ |
293
|
|
|
public function zRange(string $key, int $start = 0, int $end = -1, bool $withScores = false): array |
294
|
|
|
{ |
295
|
|
|
return $this->redis->zRange($key, $start, $end, $withScores); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Returns the elements of the sorted set stored at the specified key which |
301
|
|
|
* have scores in the range [start,end]. Adding a parenthesis before start |
302
|
|
|
* or end excludes it from the range. +inf and -inf are also valid limits. |
303
|
|
|
* |
304
|
|
|
* See: https://redis.io/commands/zrangebyscore. |
305
|
|
|
* |
306
|
|
|
* @param string $key |
307
|
|
|
* @param mixed|int|string $start |
308
|
|
|
* @param mixed|int|string $end |
309
|
|
|
* @param array|null $options Two options are available: |
310
|
|
|
* - withscores => TRUE, |
311
|
|
|
* and limit => [$offset, $count] |
312
|
|
|
* |
313
|
|
|
* @return array Array containing the values in |
314
|
|
|
* specified range. |
315
|
|
|
*/ |
316
|
|
|
public function zRangeByScore(string $key, $start, $end, ?array $options = null): array |
317
|
|
|
{ |
318
|
|
|
if (is_null($options)) { |
319
|
|
|
return $this->redis->zRangeByScore($key, $start, $end); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
$rangeOptions = ['withscores', 'limit']; |
323
|
|
|
|
324
|
|
|
if (count(array_intersect(array_keys($options), $rangeOptions)) != count($options)) { |
325
|
|
|
throw new UnsupportedOptionException("Option Not Supported", 1); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
return $this->redis->zRangeByScore($key, $start, $end, $options); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Returns the elements of the sorted set stored at the specified key which |
334
|
|
|
* have scores in the range [start,end]. Adding a parenthesis before start |
335
|
|
|
* or end excludes it from the range. +inf and -inf are also valid limits. |
336
|
|
|
* |
337
|
|
|
* zRevRangeByScore returns the same items in reverse order, when the start |
338
|
|
|
* and end parameters are swapped. |
339
|
|
|
* |
340
|
|
|
* See: https://redis.io/commands/zrevrangebyscore. |
341
|
|
|
* |
342
|
|
|
* @param string $key |
343
|
|
|
* @param mixed|int|string $start |
344
|
|
|
* @param mixed|int|string $end |
345
|
|
|
* @param array|null $options Two options are available: |
346
|
|
|
* - withscores => TRUE, |
347
|
|
|
* and limit => [$offset, $count] |
348
|
|
|
* |
349
|
|
|
* @return array Array containing the values in |
350
|
|
|
* specified range. |
351
|
|
|
*/ |
352
|
|
|
public function zRevRangeByScore(string $key, $start, $end, ?array $options = null): array |
353
|
|
|
{ |
354
|
|
|
if (is_null($options)) { |
355
|
|
|
return $this->redis->zRevRangeByScore($key, $start, $end); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
$rangeOptions = ['withscores', 'limit']; |
359
|
|
|
|
360
|
|
|
if (count(array_intersect(array_keys($options), $rangeOptions)) != count($options)) { |
361
|
|
|
throw new UnsupportedOptionException("Option Not Supported", 1); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
return $this->redis->zRevRangeByScore($key, $start, $end, $options); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Returns a lexicographical range of members in a sorted set, assuming the |
370
|
|
|
* members have the same score. The min and max values are required to start |
371
|
|
|
* with '(' (exclusive), '[' (inclusive), or be exactly the values |
372
|
|
|
* '-' (negative inf) or '+' (positive inf). |
373
|
|
|
* |
374
|
|
|
* The command must be called with either three or five arguments or will |
375
|
|
|
* return FALSE. |
376
|
|
|
* |
377
|
|
|
* See: https://redis.io/commands/zrangebylex |
378
|
|
|
* |
379
|
|
|
* @param string $key The ZSET you wish to run against |
380
|
|
|
* @param mixed|string $min The minimum alphanumeric value you wish to get |
381
|
|
|
* @param mixed|string $max The maximum alphanumeric value you wish to get |
382
|
|
|
* @param int|null $offset Optional argument if you wish to start |
383
|
|
|
* somewhere other than the first element. |
384
|
|
|
* @param int|null $limit Optional argument if you wish to limit the |
385
|
|
|
* number of elements returned. |
386
|
|
|
* |
387
|
|
|
* @return array Array containing the values in the specified |
388
|
|
|
* range. |
389
|
|
|
*/ |
390
|
|
|
public function zRangeByLex(string $key, $min, $max, ?int $offset = null, ?int $limit = null): array |
391
|
|
|
{ |
392
|
|
|
if (!$this->_validateLexParams($min, $max)) { |
393
|
|
|
throw new InvalidArgumentException("Redis::zRangeByLex(): min and max arguments must start with '[' or '('", 1); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
if (is_null($offset) && is_null($limit)) { |
397
|
|
|
return $this->redis->zRangeByLex($key, $min, $max); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
if (!is_null($offset) && !is_null($limit)) { |
401
|
|
|
return $this->redis->zRangeByLex($key, $min, $max, $offset, $limit); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
throw new InvalidArgumentException("The provided parameters do not match the required.", 1); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Returns the rank of a given member in the specified sorted set, starting |
410
|
|
|
* at 0 for the item with the smallest score. |
411
|
|
|
* |
412
|
|
|
* See: https://redis.io/commands/zrank. |
413
|
|
|
* |
414
|
|
|
* @param string $key The ZSET you wish to run against |
415
|
|
|
* @param mixed|string|int|float $member The member to look for |
416
|
|
|
* |
417
|
|
|
* @return int The member's rank position |
418
|
|
|
*/ |
419
|
|
|
public function zRank(string $key, $member): int |
420
|
|
|
{ |
421
|
|
|
return $this->redis->zRank($key, $member); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* Returns the rank of a given member in the specified sorted set, starting |
427
|
|
|
* at 0 for the item with the smallest score. |
428
|
|
|
* |
429
|
|
|
* zRevRank starts at 0 for the item with the largest score. |
430
|
|
|
* |
431
|
|
|
* See: https://redis.io/commands/zrevrank. |
432
|
|
|
* |
433
|
|
|
* @param string $key The ZSET you wish to run against |
434
|
|
|
* @param mixed|string|int|float $member The member to look for |
435
|
|
|
* |
436
|
|
|
* @return int The member's rank position |
437
|
|
|
*/ |
438
|
|
|
public function zRevRank(string $key, $member): int |
439
|
|
|
{ |
440
|
|
|
return $this->redis->zRevRank($key, $member); |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Delete one or more members from a sorted set. |
446
|
|
|
* |
447
|
|
|
* See: https://redis.io/commands/zrem. |
448
|
|
|
* |
449
|
|
|
* @param string $key The ZSET you wish to run against |
450
|
|
|
* @param splay $members Member(s) to be removed from the ZSET |
451
|
|
|
* |
452
|
|
|
* @return int The number of members deleted. |
453
|
|
|
*/ |
454
|
|
|
public function zRem(string $key, ...$members): int |
455
|
|
|
{ |
456
|
|
|
return $this->redis->zRem($key, ...$members); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* Delete one or more members from a sorted set. |
462
|
|
|
* |
463
|
|
|
* Note: zDelete and zRemove are an alias for zRem and will be removed |
464
|
|
|
* in future versions of phpredis. |
465
|
|
|
* |
466
|
|
|
* See: https://redis.io/commands/zrem. |
467
|
|
|
* |
468
|
|
|
* @param string $key The ZSET you wish to run against |
469
|
|
|
* @param splay $members Member(s) to be removed from the ZSET |
470
|
|
|
* |
471
|
|
|
* @return int The number of members deleted. |
472
|
|
|
*/ |
473
|
|
|
public function zDelete(string $key, ...$members): int |
474
|
|
|
{ |
475
|
|
|
return $this->redis->zRem($key, ...$members); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* Delete one or more members from a sorted set. |
481
|
|
|
* |
482
|
|
|
* Note: zDelete and zRemove are an alias for zRem and will be removed |
483
|
|
|
* in future versions of phpredis. |
484
|
|
|
* |
485
|
|
|
* See: https://redis.io/commands/zrem. |
486
|
|
|
* |
487
|
|
|
* @param string $key The ZSET you wish to run against |
488
|
|
|
* @param splay $members Member(s) to be removed from the ZSET |
489
|
|
|
* |
490
|
|
|
* @return int The number of members deleted. |
491
|
|
|
*/ |
492
|
|
|
public function zRemove(string $key, ...$members): int |
493
|
|
|
{ |
494
|
|
|
return $this->redis->zRem($key, ...$members); |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
|
498
|
|
|
/** |
499
|
|
|
* Deletes the elements of the sorted set stored at the specified key which |
500
|
|
|
* have rank in the range [start,end]. |
501
|
|
|
* |
502
|
|
|
* See: https://redis.io/commands/zremrangebyrank. |
503
|
|
|
* |
504
|
|
|
* @param string $key The ZSET you wish to run against |
505
|
|
|
* @param int $start |
506
|
|
|
* @param int $end |
507
|
|
|
* |
508
|
|
|
* @return int The number of values deleted from the sorted set |
509
|
|
|
*/ |
510
|
|
|
public function zRemRangeByRank(string $key, int $start, int $end): int |
511
|
|
|
{ |
512
|
|
|
if ($end < $start) { |
513
|
|
|
throw new InvalidArgumentException("End should be greater than Start.", 1); |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
return $this->redis->zRemRangeByRank($key, $start, $end); |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* Deletes the elements of the sorted set stored at the specified key which |
522
|
|
|
* have rank in the range [start,end]. |
523
|
|
|
* |
524
|
|
|
* Note: zDeleteRangeByRank is an alias for zRemRangeByRank and will be |
525
|
|
|
* removed in future versions of phpredis. |
526
|
|
|
* |
527
|
|
|
* See: https://redis.io/commands/zremrangebyrank. |
528
|
|
|
* |
529
|
|
|
* @param string $key The ZSET you wish to run against |
530
|
|
|
* @param int $start |
531
|
|
|
* @param int $end |
532
|
|
|
* |
533
|
|
|
* @return int The number of values deleted from the sorted set |
534
|
|
|
*/ |
535
|
|
|
public function zDeleteRangeByRank(string $key, int $start, int $end): int |
536
|
|
|
{ |
537
|
|
|
if ($end < $start) { |
538
|
|
|
throw new InvalidArgumentException("End should be greater than Start.", 1); |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
return $this->redis->zRemRangeByRank($key, $start, $end); |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
|
545
|
|
|
/** |
546
|
|
|
* Deletes the elements of the sorted set stored at the specified key which |
547
|
|
|
* have scores in the range [start,end]. |
548
|
|
|
* |
549
|
|
|
* See: https://redis.io/commands/zremrangebyscore. |
550
|
|
|
* |
551
|
|
|
* @param string $key The ZSET you wish to run against |
552
|
|
|
* @param mixed|float|string $start double or "+inf" or "-inf" string |
553
|
|
|
* @param mixed|float|string $end double or "+inf" or "-inf" string |
554
|
|
|
* |
555
|
|
|
* @return The number of values deleted from the sorted set |
556
|
|
|
*/ |
557
|
|
|
public function zRemRangeByScore(string $key, $start, $end): int |
558
|
|
|
{ |
559
|
|
|
if ((!is_float($start) && !is_string($start)) || (!is_float($end) && !is_string($end))) { |
560
|
|
|
throw new InvalidArgumentException("Start and End should be float or string.", 1); |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
if (is_float($start) && is_float($end) && ($end < $start)) { |
564
|
|
|
throw new InvalidArgumentException("End should be greater than Start.", 1); |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
return $this->redis->zRemRangeByScore($key, $start, $end); |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
|
571
|
|
|
/** |
572
|
|
|
* Deletes the elements of the sorted set stored at the specified key which |
573
|
|
|
* have scores in the range [start,end]. |
574
|
|
|
* |
575
|
|
|
* Note: zDeleteRangeByScore and zRemoveRangeByScore are an alias for |
576
|
|
|
* zRemRangeByScore and will be removed in future versions of phpredis. |
577
|
|
|
* |
578
|
|
|
* See: https://redis.io/commands/zremrangebyscore. |
579
|
|
|
* |
580
|
|
|
* @param string $key The ZSET you wish to run against |
581
|
|
|
* @param mixed|float|string $start double or "+inf" or "-inf" string |
582
|
|
|
* @param mixed|float|string $end double or "+inf" or "-inf" string |
583
|
|
|
* |
584
|
|
|
* @return The number of values deleted from the sorted set |
585
|
|
|
*/ |
586
|
|
|
public function zDeleteRangeByScore(string $key, $start, $end): int |
587
|
|
|
{ |
588
|
|
|
if ((!is_float($start) && !is_string($start)) || (!is_float($end) && !is_string($end))) { |
589
|
|
|
throw new InvalidArgumentException("Start and End should be float or string.", 1); |
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
if (is_float($start) && is_float($end) && ($end < $start)) { |
593
|
|
|
throw new InvalidArgumentException("End should be greater than Start.", 1); |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
return $this->redis->zRemRangeByScore($key, $start, $end); |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
|
600
|
|
|
/** |
601
|
|
|
* Deletes the elements of the sorted set stored at the specified key which |
602
|
|
|
* have scores in the range [start,end]. |
603
|
|
|
* |
604
|
|
|
* Note: zDeleteRangeByScore and zRemoveRangeByScore are an alias for |
605
|
|
|
* zRemRangeByScore and will be removed in future versions of phpredis. |
606
|
|
|
* |
607
|
|
|
* See: https://redis.io/commands/zremrangebyscore. |
608
|
|
|
* |
609
|
|
|
* @param string $key The ZSET you wish to run against |
610
|
|
|
* @param mixed|float|string $start double or "+inf" or "-inf" string |
611
|
|
|
* @param mixed|float|string $end double or "+inf" or "-inf" string |
612
|
|
|
* |
613
|
|
|
* @return The number of values deleted from the sorted set |
614
|
|
|
*/ |
615
|
|
|
public function zRemoveRangeByScore(string $key, $start, $end): int |
616
|
|
|
{ |
617
|
|
|
if ((!is_float($start) && !is_string($start)) || (!is_float($end) && !is_string($end))) { |
618
|
|
|
throw new InvalidArgumentException("Start and End should be float or string.", 1); |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
if (is_float($start) && is_float($end) && ($end < $start)) { |
622
|
|
|
throw new InvalidArgumentException("End should be greater than Start.", 1); |
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
return $this->redis->zRemRangeByScore($key, $start, $end); |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
|
629
|
|
|
/** |
630
|
|
|
* Returns the elements of the sorted set stored at the specified key in |
631
|
|
|
* the range [start, end] in reverse order. start and stop are interpreted |
632
|
|
|
* as zero-based indices: |
633
|
|
|
* 0 the first element, 1 the second ... |
634
|
|
|
* -1 the last element, -2 the penultimate ... |
635
|
|
|
* |
636
|
|
|
* See: https://redis.io/commands/zrevrange. |
637
|
|
|
* |
638
|
|
|
* @param string $key The ZSET you wish to run against |
639
|
|
|
* @param int $start 0 the first element, 1 the second ... |
640
|
|
|
* @param int $end -1 the last element, -2 the penultimate ... |
641
|
|
|
* @param bool|boolean $withScores false No Scores | true Scores in |
642
|
|
|
* associative array |
643
|
|
|
* |
644
|
|
|
* @return array Array containing the values in |
645
|
|
|
* specified range. |
646
|
|
|
*/ |
647
|
|
|
public function zRevRange(string $key, int $start = 0, int $end = -1, bool $withScores = false): array |
648
|
|
|
{ |
649
|
|
|
return $this->redis->zRevRange($key, $start, $end, $withScores); |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
|
653
|
|
|
/** |
654
|
|
|
* Returns the score of a given member in the specified sorted set. |
655
|
|
|
* |
656
|
|
|
* @param string $key The ZSET you wish to run against. |
657
|
|
|
* @param mixed|string|float|int $member |
658
|
|
|
* |
659
|
|
|
* @return float The Score associated with the Member. |
660
|
|
|
*/ |
661
|
|
|
public function zScore(string $key, $member): float |
662
|
|
|
{ |
663
|
|
|
return $this->redis->zScore($key, $member); |
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
|
667
|
|
|
/** |
668
|
|
|
* Creates an union of sorted sets given in second argument. |
669
|
|
|
* The result of the union will be stored in the sorted set defined by the |
670
|
|
|
* first argument. |
671
|
|
|
* |
672
|
|
|
* The third optional argument defines weights to apply to the sorted sets |
673
|
|
|
* in input. In this case, the weights will be multiplied by the score of |
674
|
|
|
* each element in the sorted set before applying the aggregation. The |
675
|
|
|
* forth argument defines the AGGREGATE option which specify how the |
676
|
|
|
* results of the union are aggregated. |
677
|
|
|
* |
678
|
|
|
* See: https://redis.io/commands/zunionstore. |
679
|
|
|
* |
680
|
|
|
* @param string $keyOutput The ZSET you wish to store the result. |
681
|
|
|
* @param array $arrayZSetKeys |
682
|
|
|
* @param array $arrayWeights |
683
|
|
|
* @param string $aggregateFunction Either "SUM", "MIN", or "MAX": |
684
|
|
|
* defines the behaviour to use on |
685
|
|
|
* duplicate entries during the |
686
|
|
|
* zunionstore. |
687
|
|
|
* |
688
|
|
|
* @return [type] |
|
|
|
|
689
|
|
|
*/ |
690
|
|
|
public function zUnionStore( |
691
|
|
|
string $keyOutput, |
692
|
|
|
array $arrayZSetKeys, |
693
|
|
|
?array $arrayWeights = null, |
694
|
|
|
?string $aggregateFunction = null |
695
|
|
|
): int { |
696
|
|
|
// Validate Aggregate Function |
697
|
|
|
if (!is_null($aggregateFunction) && !is_null($arrayWeights)) { |
698
|
|
|
$operation = strtoupper($aggregateFunction); |
699
|
|
|
|
700
|
|
|
if (!in_array($operation, $this->SET_OPERATIONS)) { |
701
|
|
|
throw new SetOperationException('Operation not supported', 1); |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
return $this->redis->zUnionStore($keyOutput, $arrayZSetKeys, $arrayWeights, $operation); |
705
|
|
|
} |
706
|
|
|
|
707
|
|
|
// Call using Weights |
708
|
|
|
if (!is_null($arrayWeights)) { |
709
|
|
|
return $this->redis->zUnionStore($keyOutput, $arrayZSetKeys, $arrayWeights); |
710
|
|
|
} |
711
|
|
|
|
712
|
|
|
return $this->redis->zUnionStore($keyOutput, $arrayZSetKeys); |
713
|
|
|
} |
714
|
|
|
|
715
|
|
|
|
716
|
|
|
/** |
717
|
|
|
* Creates an union of sorted sets given in second argument. |
718
|
|
|
* The result of the union will be stored in the sorted set defined by the |
719
|
|
|
* first argument. |
720
|
|
|
* |
721
|
|
|
* The third optional argument defines weights to apply to the sorted sets |
722
|
|
|
* in input. In this case, the weights will be multiplied by the score of |
723
|
|
|
* each element in the sorted set before applying the aggregation. The |
724
|
|
|
* forth argument defines the AGGREGATE option which specify how the |
725
|
|
|
* results of the union are aggregated. |
726
|
|
|
* |
727
|
|
|
* Note: zUnion is an alias for zunionstore and will be removed in future |
728
|
|
|
* versions of phpredis. |
729
|
|
|
* |
730
|
|
|
* See: https://redis.io/commands/zunionstore. |
731
|
|
|
* |
732
|
|
|
* @param string $keyOutput The ZSET you wish to store the result. |
733
|
|
|
* @param array $arrayZSetKeys |
734
|
|
|
* @param array $arrayWeights |
735
|
|
|
* @param string $aggregateFunction Either "SUM", "MIN", or "MAX": |
736
|
|
|
* defines the behaviour to use on |
737
|
|
|
* duplicate entries during the |
738
|
|
|
* zunionstore. |
739
|
|
|
* |
740
|
|
|
* @return [type] |
|
|
|
|
741
|
|
|
*/ |
742
|
|
|
public function zUnion( |
743
|
|
|
string $keyOutput, |
744
|
|
|
array $arrayZSetKeys, |
745
|
|
|
?array $arrayWeights = null, |
746
|
|
|
?string $aggregateFunction = null |
747
|
|
|
): int { |
748
|
|
|
// Validate Aggregate Function |
749
|
|
|
if (!is_null($aggregateFunction) && !is_null($arrayWeights)) { |
750
|
|
|
$operation = strtoupper($aggregateFunction); |
751
|
|
|
|
752
|
|
|
if (!in_array($operation, $this->SET_OPERATIONS)) { |
753
|
|
|
throw new SetOperationException('Operation not supported', 1); |
754
|
|
|
} |
755
|
|
|
|
756
|
|
|
return $this->redis->zUnionStore($keyOutput, $arrayZSetKeys, $arrayWeights, $operation); |
757
|
|
|
} |
758
|
|
|
|
759
|
|
|
// Call using Weights |
760
|
|
|
if (!is_null($arrayWeights)) { |
761
|
|
|
return $this->redis->zUnionStore($keyOutput, $arrayZSetKeys, $arrayWeights); |
762
|
|
|
} |
763
|
|
|
|
764
|
|
|
return $this->redis->zUnionStore($keyOutput, $arrayZSetKeys); |
765
|
|
|
} |
766
|
|
|
|
767
|
|
|
|
768
|
|
|
public function zScan(): bool |
769
|
|
|
{ |
770
|
|
|
return false; |
771
|
|
|
} |
772
|
|
|
|
773
|
|
|
|
774
|
|
|
/** |
775
|
|
|
* ======================================================================== |
776
|
|
|
* H E L P E R M E T H O D S |
777
|
|
|
* ======================================================================== |
778
|
|
|
*/ |
779
|
|
|
|
780
|
|
|
|
781
|
|
|
/** |
782
|
|
|
* Validate Lex Params |
783
|
|
|
* |
784
|
|
|
* @param splat $params |
785
|
|
|
* |
786
|
|
|
* @return bool |
787
|
|
|
*/ |
788
|
|
|
protected function _validateLexParams(...$params) |
789
|
|
|
{ |
790
|
|
|
return count(preg_grep("/^(\+|\-)?(\({1}.)?(\[{1}.)?$/", $params)) == count($params); |
791
|
|
|
} |
792
|
|
|
} |
793
|
|
|
|
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.