-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathListsCommandsTrait.php
269 lines (249 loc) · 7.86 KB
/
ListsCommandsTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
/**
* This file is part of RedisClient.
* git: https://github.com/cheprasov/php-redis-client
*
* (C) Alexander Cheprasov <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace RedisClient\Command\Traits\Version2x6;
use RedisClient\Command\Response\ResponseParser;
/**
* Lists Commands
* @link http://redis.io/commands#list
*/
trait ListsCommandsTrait {
/**
* BLPOP key [key ...] timeout
* Available since 2.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/blpop
*
* @param string|string[] $keys
* @param int $timeout In seconds
* @return array|null [list => value]
*/
public function blpop($keys, $timeout) {
$keys = (array)$keys;
return $this->returnCommand(['BLPOP'], $keys, [$keys, $timeout], ResponseParser::PARSE_ASSOC_ARRAY);
}
/**
* BRPOP key [key ...] timeout
* Available since 2.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/brpop
*
* @param string|string[] $keys
* @param int $timeout
* @return array|null [list => value]
*/
public function brpop($keys, $timeout) {
$keys = (array)$keys;
return $this->returnCommand(['BRPOP'], $keys, [$keys, $timeout], ResponseParser::PARSE_ASSOC_ARRAY);
}
/**
* BRPOPLPUSH source destination timeout
* Available since 2.2.0.
* Time complexity: O(1)
* @link http://redis.io/commands/brpoplpush
*
* @param string $source
* @param string $destination
* @param int $timeout
* @return mixed The element being popped from source and pushed to destination.
* If timeout is reached, a Null reply is returned.
*/
public function brpoplpush($source, $destination, $timeout) {
return $this->returnCommand(['BRPOPLPUSH'], [$source, $destination], [$source, $destination, $timeout]);
}
/**
* LINDEX key index
* Available since 1.0.0.
* Time complexity: O(N) or O(1)
* @link http://redis.io/commands/lindex
*
* @param string $key
* @param int $index
* @return string|null The requested element, or null when index is out of range.
*/
public function lindex($key, $index) {
return $this->returnCommand(['LINDEX'], $key, [$key, $index]);
}
/**
* LINSERT key BEFORE|AFTER pivot value
* Available since 2.2.0.
* Time complexity: O(N) or O(1)
* @link http://redis.io/commands/linsert
*
* @param string $key
* @param bool $after
* @param string $pivot
* @param string $value
* @return int The length of the list after the insert operation,
* or -1 when the value pivot was not found. Or 0 when key was not found.
*/
public function linsert($key, $after, $pivot, $value) {
return $this->returnCommand(['LINSERT'], $key, [$key, $after ? 'AFTER' : 'BEFORE', $pivot, $value]);
}
/**
* LLEN key
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/llen
*
* @param string $key
* @return int The length of the list at key.
*/
public function llen($key) {
return $this->returnCommand(['LLEN'], $key, [$key]);
}
/**
* LPOP key
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/lpop
*
* @param string $key
* @return string|null The value of the first element, or null when key does not exist.
*/
public function lpop($key) {
return $this->returnCommand(['LPOP'], $key, [$key]);
}
/**
* LPUSH key value [value ...]
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/lpush
*
* @param string $key
* @param string|string[] $values
* @return int The length of the list after the push operations.
*/
public function lpush($key, $values) {
return $this->returnCommand(['LPUSH'], $key, [$key, (array)$values]);
}
/**
* LPUSHX key value
* Available since 2.2.0.
* Time complexity: O(1)
* @link http://redis.io/commands/lpushx
*
* @param string $key
* @param string $value
* @return int The length of the list after the push operation.
*/
public function lpushx($key, $value) {
return $this->returnCommand(['LPUSHX'], $key, [$key, $value]);
}
/**
* LRANGE key start stop
* Available since 1.0.0.
* Time complexity: O(S+N) where S is the distance of start offset from HEAD for small lists.
* @link http://redis.io/commands/lrange
*
* @param string $key
* @param int $start
* @param int $stop
* @return array List of elements in the specified range.
*/
public function lrange($key, $start, $stop) {
return $this->returnCommand(['LRANGE'], $key, [$key, $start, $stop]);
}
/**
* LREM key count value
* Available since 1.0.0.
* Time complexity: O(N) where N is the length of the list.
* @link http://redis.io/commands/lrem
*
* @param string $key
* @param int $count
* @param string $value
* @return int The number of removed elements.
*/
public function lrem($key, $count, $value) {
return $this->returnCommand(['LREM'], $key, [$key, $count, $value]);
}
/**
* LSET key index value
* Available since 1.0.0.
* Time complexity: O(N) where N is the length of the list.
* Setting either the first or the last element of the list is O(1).
* @link http://redis.io/commands/lset
*
* @param string $key
* @param int $index
* @param string $value
* @return bool
*/
public function lset($key, $index, $value) {
return $this->returnCommand(['LSET'], $key, [$key, $index, $value]);
}
/**
* LTRIM key start stop
* Available since 1.0.0.
* Time complexity: O(N) where N is the number of elements to be removed by the operation.
* @link http://redis.io/commands/ltrim
*
* @param string $key
* @param int $start
* @param int $stop
* @return bool
*/
public function ltrim($key, $start, $stop) {
return $this->returnCommand(['LTRIM'], $key, [$key, $start, $stop]);
}
/**
* RPOP key
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/rpop
*
* @param string $key
* @return string|null The value of the last element, or null when key does not exist.
*/
public function rpop($key) {
return $this->returnCommand(['RPOP'], $key, [$key]);
}
/**
* RPOPLPUSH source destination
* Available since 1.2.0.
* Time complexity: O(1)
* @link http://redis.io/commands/rpoplpush
*
* @param string $source
* @param string $destination
* @return string The element being popped and pushed.
*/
public function rpoplpush($source, $destination) {
$keys = [$source, $destination];
return $this->returnCommand(['RPOPLPUSH'], $keys, $keys);
}
/**
* RPUSH key value [value ...]
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/rpush
*
* @param string $key
* @param string|string[] $values
* @return int The length of the list after the push operation.
*/
public function rpush($key, $values) {
return $this->returnCommand(['RPUSH'], $key, [$key, (array)$values]);
}
/**
* RPUSHX key value
* Available since 2.2.0.
* Time complexity: O(1)
* @link http://redis.io/commands/rpushx
*
* @param string $key
* @param string $value
* @return int The length of the list after the push operation.
*/
public function rpushx($key, $value) {
return $this->returnCommand(['RPUSHX'], $key, [$key, $value]);
}
}