|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Webdcg\Redis\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
trait Lists |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Is a blocking lPop primitive. If at least one of the lists contains at |
|
11
|
|
|
* least one element, the element will be popped from the head of the list |
|
12
|
|
|
* and returned to the caller. If all the list identified by the keys |
|
13
|
|
|
* passed in arguments are empty, blPop will block during the specified |
|
14
|
|
|
* timeout until an element is pushed to one of those lists. This element |
|
15
|
|
|
* will be popped. |
|
16
|
|
|
* See: https://redis.io/commands/blpop. |
|
17
|
|
|
* |
|
18
|
|
|
* @param array $keys |
|
19
|
|
|
* @param int $timeout |
|
20
|
|
|
* |
|
21
|
|
|
* @return array ['listName', 'element'] |
|
22
|
|
|
*/ |
|
23
|
|
|
public function blPop(array $keys, int $timeout): array |
|
24
|
|
|
{ |
|
25
|
|
|
return $this->redis->blPop($keys, $timeout); |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Is a blocking rPop primitive. If at least one of the lists contains at |
|
31
|
|
|
* least one element, the element will be popped from the head of the list |
|
32
|
|
|
* and returned to the caller. If all the list identified by the keys |
|
33
|
|
|
* passed in arguments are empty, brPop will block during the specified |
|
34
|
|
|
* timeout until an element is pushed to one of those lists. This element |
|
35
|
|
|
* will be popped. |
|
36
|
|
|
* See: https://redis.io/commands/brpop. |
|
37
|
|
|
* |
|
38
|
|
|
* @param array $keys |
|
39
|
|
|
* @param int $timeout |
|
40
|
|
|
* |
|
41
|
|
|
* @return array ['listName', 'element'] |
|
42
|
|
|
*/ |
|
43
|
|
|
public function brPop(array $keys, int $timeout): array |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->redis->brPop($keys, $timeout); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* A blocking version of rPopLPush, with an integral timeout in the third |
|
51
|
|
|
* parameter. |
|
52
|
|
|
* See: https://redis.io/commands/brpoplpush. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $sourceKey |
|
55
|
|
|
* @param string $destinationKey |
|
56
|
|
|
* @param int $timeout |
|
57
|
|
|
* |
|
58
|
|
|
* @return string The element that was moved in case of |
|
59
|
|
|
* success, FALSE in case of timeout. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function bRPopLPush(string $sourceKey, string $destinationKey, int $timeout): string |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->redis->bRPopLPush($sourceKey, $destinationKey, $timeout); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Return the specified element of the list stored at the specified key. |
|
69
|
|
|
* 0 the first element, 1 the second ... |
|
70
|
|
|
* -1 the last element, -2 the penultimate ... |
|
71
|
|
|
* Return FALSE in case of a bad index or a key that doesn't point to a |
|
72
|
|
|
* list. |
|
73
|
|
|
* See: https://redis.io/commands/lindex. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $key |
|
76
|
|
|
* @param int $index |
|
77
|
|
|
* |
|
78
|
|
|
* @return string String the element at this index |
|
79
|
|
|
* Bool FALSE if the key identifies a non-string data |
|
80
|
|
|
* type, or no value corresponds to this index in the |
|
81
|
|
|
* list Key. |
|
82
|
|
|
*/ |
|
83
|
|
|
public function lIndex(string $key, int $index): string |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->redis->lIndex($key, $index); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Return the specified element of the list stored at the specified key. |
|
91
|
|
|
* 0 the first element, 1 the second ... |
|
92
|
|
|
* -1 the last element, -2 the penultimate ... |
|
93
|
|
|
* Return FALSE in case of a bad index or a key that doesn't point to a |
|
94
|
|
|
* list. |
|
95
|
|
|
* Note: lGet is an alias for lIndex and will be removed in future versions |
|
96
|
|
|
* of phpredis. |
|
97
|
|
|
* See: https://redis.io/commands/lindex. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $key |
|
100
|
|
|
* @param int $index |
|
101
|
|
|
* |
|
102
|
|
|
* @return string String the element at this index |
|
103
|
|
|
* Bool FALSE if the key identifies a non-string data |
|
104
|
|
|
* type, or no value corresponds to this index in the |
|
105
|
|
|
* list Key. |
|
106
|
|
|
*/ |
|
107
|
|
|
public function lGet(string $key, int $index): string |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->redis->lIndex($key, $index); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Insert value in the list before or after the pivot value. |
|
115
|
|
|
* The parameter options specify the position of the insert (before or |
|
116
|
|
|
* after). If the list didn't exists, or the pivot didn't exists, the |
|
117
|
|
|
* value is not inserted. |
|
118
|
|
|
* See: https://redis.io/commands/linsert. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $key |
|
121
|
|
|
* @param string $position b => Redis::BEFORE | a => Redis::AFTER |
|
122
|
|
|
* @param mixed $pivot |
|
123
|
|
|
* @param mixed $value |
|
124
|
|
|
* |
|
125
|
|
|
* @return int The number of the elements in the list, -1 if |
|
126
|
|
|
* the pivot didn't exists. |
|
127
|
|
|
*/ |
|
128
|
|
|
public function lInsert(string $key, string $position, $pivot, $value): int |
|
129
|
|
|
{ |
|
130
|
|
|
if (strtolower($position) == 'a') { |
|
131
|
|
|
return $this->redis->lInsert($key, \Redis::AFTER, $pivot, $value); |
|
132
|
|
|
} elseif (strtolower($position) == 'b') { |
|
133
|
|
|
return $this->redis->lInsert($key, \Redis::BEFORE, $pivot, $value); |
|
134
|
|
|
} else { |
|
135
|
|
|
throw new Exception("Error Processing Request", 1); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Returns the size of a list identified by Key. |
|
142
|
|
|
* If the list didn't exist or is empty, the command returns 0. If the data |
|
143
|
|
|
* type identified by Key is not a list, the command return FALSE. |
|
144
|
|
|
* See: https://redis.io/commands/llen. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $key |
|
147
|
|
|
* |
|
148
|
|
|
* @return int LONG The size of the list identified by Key exists. |
|
149
|
|
|
* BOOL FALSE if the data type identified by Key is |
|
150
|
|
|
* not list. |
|
151
|
|
|
*/ |
|
152
|
|
|
public function lLen(string $key): int |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->redis->lLen($key); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Returns the size of a list identified by Key. |
|
160
|
|
|
* If the list didn't exist or is empty, the command returns 0. If the data |
|
161
|
|
|
* type identified by Key is not a list, the command return FALSE. |
|
162
|
|
|
* Note: lSize is an alias for lLen and will be removed in future versions |
|
163
|
|
|
* of phpredis. |
|
164
|
|
|
* See: https://redis.io/commands/llen. |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $key |
|
167
|
|
|
* |
|
168
|
|
|
* @return int LONG The size of the list identified by Key exists. |
|
169
|
|
|
* BOOL FALSE if the data type identified by Key is |
|
170
|
|
|
* not list. |
|
171
|
|
|
*/ |
|
172
|
|
|
public function lSize(string $key): int |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->redis->lLen($key); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Return and remove the first element of the list. |
|
180
|
|
|
* See: https://redis.io/commands/lpop. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $key |
|
183
|
|
|
* |
|
184
|
|
|
* @return string STRING if command executed successfully |
|
185
|
|
|
* BOOL FALSE in case of failure (empty list) |
|
186
|
|
|
*/ |
|
187
|
|
|
public function lPop(string $key): string |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->redis->lPop($key); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Adds the string value to the head (left) of the list. Creates the list |
|
195
|
|
|
* if the key didn't exist. If the key exists and is not a list, FALSE is |
|
196
|
|
|
* returned. |
|
197
|
|
|
* See: https://redis.io/commands/lpush. |
|
198
|
|
|
* |
|
199
|
|
|
* @param string $key |
|
200
|
|
|
* @param mixed $value Value to push in key |
|
201
|
|
|
* |
|
202
|
|
|
* @return int LONG The new length of the list in case of |
|
203
|
|
|
* success, FALSE in case of Failure. |
|
204
|
|
|
*/ |
|
205
|
|
|
public function lPush(string $key, $value): int |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->redis->lPush($key, $value); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Adds the string value to the head (left) of the list if the list exists. |
|
213
|
|
|
* See: https://redis.io/commands/lpushx. |
|
214
|
|
|
* |
|
215
|
|
|
* @param string $key |
|
216
|
|
|
* @param mixed $value Value to push in key |
|
217
|
|
|
* |
|
218
|
|
|
* @return int LONG The new length of the list in case of |
|
219
|
|
|
* success, FALSE in case of Failure. |
|
220
|
|
|
*/ |
|
221
|
|
|
public function lPushx(string $key, $value): int |
|
222
|
|
|
{ |
|
223
|
|
|
return $this->redis->lPushx($key, $value); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Returns the specified elements of the list stored at the specified key |
|
229
|
|
|
* in the range [start, end]. start and stop are interpreted as indices: |
|
230
|
|
|
* 0 the first element, 1 the second ... |
|
231
|
|
|
* -1 the last element, -2 the penultimate ... |
|
232
|
|
|
* See: https://redis.io/commands/lrange. |
|
233
|
|
|
* |
|
234
|
|
|
* @param string $key |
|
235
|
|
|
* @param int|integer $start |
|
236
|
|
|
* @param int|integer $end |
|
237
|
|
|
* |
|
238
|
|
|
* @return array Array containing the values in specified range. |
|
239
|
|
|
*/ |
|
240
|
|
|
public function lRange(string $key, int $start = 0, int $end = -1): array |
|
241
|
|
|
{ |
|
242
|
|
|
return $this->redis->lRange($key, $start, $end); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Returns the specified elements of the list stored at the specified key |
|
248
|
|
|
* in the range [start, end]. start and stop are interpreted as indices: |
|
249
|
|
|
* 0 the first element, 1 the second ... |
|
250
|
|
|
* -1 the last element, -2 the penultimate ... |
|
251
|
|
|
* Note: lGetRange is an alias for lRange and will be removed in future |
|
252
|
|
|
* versions of phpredis. |
|
253
|
|
|
* See: https://redis.io/commands/lrange. |
|
254
|
|
|
* |
|
255
|
|
|
* @param string $key |
|
256
|
|
|
* @param int|integer $start |
|
257
|
|
|
* @param int|integer $end |
|
258
|
|
|
* |
|
259
|
|
|
* @return array Array containing the values in specified range. |
|
260
|
|
|
*/ |
|
261
|
|
|
public function lGetRange(string $key, int $start = 0, int $end = -1): array |
|
262
|
|
|
{ |
|
263
|
|
|
return $this->redis->lRange($key, $start, $end); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Removes the first count occurrences of the value element from the list. |
|
269
|
|
|
* If count is zero, all the matching elements are removed. If count is |
|
270
|
|
|
* negative, elements are removed from tail to head. |
|
271
|
|
|
* Note: The argument order is not the same as in the Redis documentation. |
|
272
|
|
|
* This difference is kept for compatibility reasons. |
|
273
|
|
|
* See: https://redis.io/commands/lrem. |
|
274
|
|
|
* |
|
275
|
|
|
* @param string $key |
|
276
|
|
|
* @param mixed $value |
|
277
|
|
|
* @param int|integer $count |
|
278
|
|
|
* |
|
279
|
|
|
* @return int LONG the number of elements to remove. |
|
280
|
|
|
* BOOL FALSE if the value identified by |
|
281
|
|
|
* key is not a list. |
|
282
|
|
|
*/ |
|
283
|
|
|
public function lRem(string $key, $value, int $count = 0): int |
|
284
|
|
|
{ |
|
285
|
|
|
return $this->redis->lRem($key, $value, $count); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Removes the first count occurrences of the value element from the list. |
|
291
|
|
|
* If count is zero, all the matching elements are removed. If count is |
|
292
|
|
|
* negative, elements are removed from tail to head. |
|
293
|
|
|
* Note: The argument order is not the same as in the Redis documentation. |
|
294
|
|
|
* This difference is kept for compatibility reasons. |
|
295
|
|
|
* Note: lRemove is an alias for lRem and will be removed in future |
|
296
|
|
|
* versions of phpredis. |
|
297
|
|
|
* See: https://redis.io/commands/lrem. |
|
298
|
|
|
* |
|
299
|
|
|
* @param string $key |
|
300
|
|
|
* @param mixed $value |
|
301
|
|
|
* @param int|integer $count |
|
302
|
|
|
* |
|
303
|
|
|
* @return int LONG the number of elements to remove. |
|
304
|
|
|
* BOOL FALSE if the value identified by |
|
305
|
|
|
* key is not a list. |
|
306
|
|
|
*/ |
|
307
|
|
|
public function lRemove(string $key, $value, int $count = 0): int |
|
308
|
|
|
{ |
|
309
|
|
|
return $this->redis->lRem($key, $value, $count); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* Set the list at index with the new value. |
|
315
|
|
|
* |
|
316
|
|
|
* @param string $key |
|
317
|
|
|
* @param int $index |
|
318
|
|
|
* @param mixed $value |
|
319
|
|
|
* |
|
320
|
|
|
* @return bool TRUE if the new value was set. FALSE if the |
|
321
|
|
|
* index is out of range, or data type identified |
|
322
|
|
|
* by key is not a list. |
|
323
|
|
|
*/ |
|
324
|
|
|
public function lSet(string $key, int $index, $value): bool |
|
325
|
|
|
{ |
|
326
|
|
|
return $this->redis->lSet($key, $index, $value); |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
|
|
330
|
|
|
/** |
|
331
|
|
|
* Trims an existing list so that it will contain only a specified range |
|
332
|
|
|
* of elements. |
|
333
|
|
|
* See: https://redis.io/commands/ltrim. |
|
334
|
|
|
* |
|
335
|
|
|
* @param string $key |
|
336
|
|
|
* @param int|integer $start |
|
337
|
|
|
* @param int|integer $stop |
|
338
|
|
|
* |
|
339
|
|
|
* @return bool return FALSE if the key identify a non-list value. |
|
340
|
|
|
*/ |
|
341
|
|
|
public function lTrim(string $key, int $start = 0, int $stop = -1): bool |
|
342
|
|
|
{ |
|
343
|
|
|
return $this->redis->lTrim($key, $start, $stop); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* Trims an existing list so that it will contain only a specified range |
|
349
|
|
|
* of elements. |
|
350
|
|
|
* Note: listTrim is an alias for lTrim and will be removed in future |
|
351
|
|
|
* versions of phpredis. |
|
352
|
|
|
* See: https://redis.io/commands/ltrim. |
|
353
|
|
|
* |
|
354
|
|
|
* @param string $key |
|
355
|
|
|
* @param int|integer $start |
|
356
|
|
|
* @param int|integer $stop |
|
357
|
|
|
* |
|
358
|
|
|
* @return bool return FALSE if the key identify a non-list value. |
|
359
|
|
|
*/ |
|
360
|
|
|
public function listTrim(string $key, int $start = 0, int $stop = -1): bool |
|
361
|
|
|
{ |
|
362
|
|
|
return $this->redis->lTrim($key, $start, $stop); |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
|
|
366
|
|
|
/** |
|
367
|
|
|
* Returns and removes the last element of the list. |
|
368
|
|
|
* See: https://redis.io/commands/rpop. |
|
369
|
|
|
* |
|
370
|
|
|
* @param string $key |
|
371
|
|
|
* |
|
372
|
|
|
* @return string STRING if command executed successfully |
|
373
|
|
|
* BOOL FALSE in case of failure (empty list) |
|
374
|
|
|
*/ |
|
375
|
|
|
public function rPop(string $key): string |
|
376
|
|
|
{ |
|
377
|
|
|
return $this->redis->rPop($key); |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
|
|
381
|
|
|
/** |
|
382
|
|
|
* Pops a value from the tail of a list, and pushes it to the front of |
|
383
|
|
|
* another list. Also return this value. (redis >= 1.1) |
|
384
|
|
|
* See: https://redis.io/commands/rpoplpush. |
|
385
|
|
|
* |
|
386
|
|
|
* @param string $sourceKey |
|
387
|
|
|
* @param string $destinationKey |
|
388
|
|
|
* |
|
389
|
|
|
* @return string The element that was moved in case of |
|
390
|
|
|
* success, FALSE in case of failure. |
|
391
|
|
|
*/ |
|
392
|
|
|
public function rPopLPush(string $sourceKey, string $destinationKey): string |
|
393
|
|
|
{ |
|
394
|
|
|
return $this->redis->rPopLPush($sourceKey, $destinationKey); |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
|
|
398
|
|
|
/** |
|
399
|
|
|
* Adds the string value to the tail (right) of the list. Creates the list |
|
400
|
|
|
* if the key didn't exist. If the key exists and is not a list, FALSE is |
|
401
|
|
|
* returned. |
|
402
|
|
|
* See: https://redis.io/commands/rpush. |
|
403
|
|
|
* |
|
404
|
|
|
* @param string $key |
|
405
|
|
|
* @param mixed $value |
|
406
|
|
|
* |
|
407
|
|
|
* @return int LONG The new length of the list in case of success, |
|
408
|
|
|
* FALSE in case of Failure. |
|
409
|
|
|
*/ |
|
410
|
|
|
public function rPush(string $key, $value): int |
|
411
|
|
|
{ |
|
412
|
|
|
return $this->redis->rPush($key, $value); |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
|
|
416
|
|
|
/** |
|
417
|
|
|
* Adds the string value to the tail (right) of the list if the list exists. |
|
418
|
|
|
* FALSE in case of Failure. |
|
419
|
|
|
* See: https://redis.io/commands/rpushx. |
|
420
|
|
|
* |
|
421
|
|
|
* @param string $key |
|
422
|
|
|
* @param mixed $value |
|
423
|
|
|
* |
|
424
|
|
|
* @return int LONG The new length of the list in case of success, |
|
425
|
|
|
* FALSE in case of Failure. |
|
426
|
|
|
*/ |
|
427
|
|
|
public function rPushX(string $key, $value): int |
|
428
|
|
|
{ |
|
429
|
|
|
return $this->redis->rPushX($key, $value); |
|
430
|
|
|
} |
|
431
|
|
|
} |
|
432
|
|
|
|
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: