-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathStringsCommandsTrait.php
373 lines (347 loc) · 10.5 KB
/
StringsCommandsTrait.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
/**
* This file is part of RedisClient.
* git: https://github.com/cheprasov/php-redis-client
*
* (C) Alexander Cheprasov <acheprasov84@gmail.com>
*
* 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\Parameter\Parameter;
use RedisClient\Exception\InvalidArgumentException;
/**
* Strings Commands
* @link http://redis.io/commands#string
*/
trait StringsCommandsTrait {
/**
* APPEND key value
* Available since 2.0.0.
* Time complexity: O(1).
* @link http://redis.io/commands/append
*
* @param string $key
* @param string $value
* @return int The length of the string after the append operation.
*/
public function append($key, $value) {
return $this->returnCommand(['APPEND'], $key, [$key, $value]);
}
/**
* BITCOUNT key [start end]
* Available since 2.6.0.
* Time complexity: O(N)
* @link http://redis.io/commands/bitcount
*
* @param string $key
* @param null|int $start
* @param null|int $end
* @return int The number of bits set to 1.
*/
public function bitcount($key, $start = null, $end = null) {
if (isset($start) xor isset($end)) {
throw new InvalidArgumentException('Start and End must be used together');
}
$params = [$key];
if (isset($start, $end)) {
$params[] = $start;
$params[] = $end;
}
return $this->returnCommand(['BITCOUNT'], $key, $params);
}
/**
* BITOP operation destkey key [key ...]
* Available since 2.6.0.
* Time complexity: O(N)
* @link http://redis.io/commands/bitop
*
* @param string $operation AND, OR, XOR and NOT
* @param string $destkey
* @param string|string[] $keys
* @return int The size of the string stored in the destination key,
* that is equal to the size of the longest input string.
*/
public function bitop($operation, $destkey, $keys) {
$keys = (array)$keys;
$allKeys = $keys;
array_unshift($allKeys, $destkey);
return $this->returnCommand(['BITOP'], $allKeys, [$operation, $destkey, $keys]);
}
/**
* DECR key
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/decr
*
* @param string $key
* @return int The value of key after the decrement
*/
public function decr($key) {
return $this->returnCommand(['DECR'], $key, [$key]);
}
/**
* DECRBY key decrement
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/decrby
*
* @param string $key
* @param int $decrement
* @return int The value of key after the decrement
*/
public function decrby($key, $decrement) {
return $this->returnCommand(['DECRBY'], $key, [$key, $decrement]);
}
/**
* GET key
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/get
*
* @param string $key
* @return string|null
*/
public function get($key) {
return $this->returnCommand(['GET'], $key, [$key]);
}
/**
* GETBIT key offset
* Available since 2.2.0.
* Time complexity: O(1)
* @link http://redis.io/commands/getbit
*
* @param string $key
* @param int $offset
* @return int The bit value stored at offset.
*/
public function getbit($key, $offset) {
return $this->returnCommand(['GETBIT'], $key, [$key, $offset]);
}
/**
* GETRANGE key start end
* Available since 2.4.0.
* Time complexity: O(N) where N is the length of the returned string.
* @link http://redis.io/commands/getrange
*
* @param string $key
* @param int $start
* @param int $end
* @return string
*/
public function getrange($key, $start, $end) {
return $this->returnCommand(['GETRANGE'], $key, [$key, $start, $end]);
}
/**
* SUBSTR key start end
* @deprecated
* @see StringsCommandsTrait::getrange
*
* @param string $key
* @param int $start
* @param int $end
* @return string
*/
public function substr($key, $start, $end) {
return $this->returnCommand(['SUBSTR'], $key, [$key, $start, $end]);
}
/**
* GETSET key value
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/getset
*
* @param string $key
* @param string $value
* @return string|null The old value stored at key, or nil when key did not exist.
*/
public function getset($key, $value) {
return $this->returnCommand(['GETSET'], $key, [$key, $value]);
}
/**
* INCR key
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/incr
*
* @param string $key
* @return int The value of key after the increment
*/
public function incr($key) {
return $this->returnCommand(['INCR'], $key, [$key]);
}
/**
* INCRBY key increment
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/incrby
*
* @param string $key
* @param int $increment
* @return int The value of key after the increment
*/
public function incrby($key, $increment) {
return $this->returnCommand(['INCRBY'], $key, [$key, $increment]);
}
/**
* INCRBYFLOAT key increment
* Available since 2.6.0.
* Time complexity: O(1)
* @link http://redis.io/commands/incrbyfloat
*
* @param string $key
* @param integer|float $increment
* @return string
*/
public function incrbyfloat($key, $increment) {
return $this->returnCommand(['INCRBYFLOAT'], $key, [$key, $increment]);
}
/**
* MGET key [key ...]
* Available since 1.0.0.
* Time complexity: O(N) where N is the number of keys to retrieve.
* @link http://redis.io/commands/mget
*
* @param string|string[] $keys
* @return array
*/
public function mget($keys) {
$keys = (array)$keys;
return $this->returnCommand(['MGET'], $keys, $keys);
}
/**
* MSET key value [key value ...]
* Available since 1.0.1.
* Time complexity: O(N) where N is the number of keys to set.
* @link http://redis.io/commands/mset
*
* @param array $keyValues
* @return bool always True since MSET can't fail.
*/
public function mset(array $keyValues) {
return $this->returnCommand(['MSET'], array_keys($keyValues), Parameter::assocArray($keyValues));
}
/**
* MSETNX key value [key value ...]
* Available since 1.0.1.
* Time complexity: O(N) where N is the number of keys to set.
* @link http://redis.io/commands/msetnx
*
* @param array $keyValues
* @return int 1 if the all the keys were set. 0 if no key was set (at least one key already existed).
*/
public function msetnx(array $keyValues) {
return $this->returnCommand(['MSETNX'], array_keys($keyValues), Parameter::assocArray($keyValues));
}
/**
* PSETEX key milliseconds value
* Available since 2.6.0.
* Time complexity: O(1)
* @link http://redis.io/commands/psetex
*
* @param string $key
* @param int $milliseconds
* @param string $value
* @return bool
*/
public function psetex($key, $milliseconds, $value) {
return $this->returnCommand(['PSETEX'], $key, [$key, $milliseconds, $value]);
}
/**
* SET key value [EX seconds | PX milliseconds] [NX|XX]
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/set
*
* @param string $key
* @param string $value
* @param null|int $seconds
* @param null|int $milliseconds
* @param null|string $exist NX - if not exist, XX - if it already exist.
* @return bool|null
* @throw InvalidArgumentException
*/
public function set($key, $value, $seconds = null, $milliseconds = null, $exist = null) {
$params = [$key, $value];
if (isset($seconds)) {
$params[] = 'EX';
$params[] = $seconds;
}
if (isset($milliseconds)) {
$params[] = 'PX';
$params[] = $milliseconds;
}
if (isset($exist)) {
$params[] = $exist;
}
return $this->returnCommand(['SET'], $key, $params);
}
/**
* SETBIT key offset value
* Available since 2.2.0.
* Time complexity: O(1)
* @link http://redis.io/commands/setbit
*
* @param string $key
* @param int $offset
* @param int|bool $bit 0/1 or true/false
* @return int The original bit value stored at offset.
*/
public function setbit($key, $offset, $bit) {
return $this->returnCommand(['SETBIT'], $key, [$key, $offset, $bit]);
}
/**
* SETEX key seconds value
* Available since 2.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/setex
*
* @param string $key
* @param int $seconds
* @param string $value
* @return bool
*/
public function setex($key, $seconds, $value) {
return $this->returnCommand(['SETEX'], $key, [$key, $seconds, $value]);
}
/**
* SETNX key value
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/setnx
*
* @param string $key
* @param string $value
* @return int 1 if the key was set, 0 if the key was not set
*/
public function setnx($key, $value) {
return $this->returnCommand(['SETNX'], $key, [$key, $value]);
}
/**
* SETRANGE key offset value
* Available since 2.2.0.
* Time complexity: O(1)
* @link http://redis.io/commands/setrange
*
* @param string $key
* @param int $offset
* @param string $value
* @return int The length of the string after it was modified by the command.
*/
public function setrange($key, $offset, $value) {
return $this->returnCommand(['SETRANGE'], $key, [$key, $offset, $value]);
}
/**
* STRLEN key
* Available since 2.2.0.
* Time complexity: O(1)
* @link http://redis.io/commands/strlen
*
* @param string $key
* @return int The length of the string at key, or 0 when key does not exist.
*/
public function strlen($key) {
return $this->returnCommand(['STRLEN'], $key, [$key]);
}
}