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