-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathKeysCommandsTrait.php
335 lines (312 loc) · 9.76 KB
/
KeysCommandsTrait.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
<?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;
/**
* Keys Commands
* @link http://redis.io/commands#generic
*/
trait KeysCommandsTrait {
/**
* DEL key [key ...]
* Available since 1.0.0.
* Time complexity: O(N) where N is the number of keys that will be removed.
* @link http://redis.io/commands/del
*
* @param string|string[] $keys
* @return int The number of keys that were removed.
*/
public function del($keys) {
$keys = (array)$keys;
return $this->returnCommand(['DEL'], $keys, $keys);
}
/**
* DUMP key
* Available since 2.6.0.
* Time complexity: O(1) to access the key and additional O(N*M) to serialized it,
* where N is the number of Redis objects composing the value and M their average size.
* @link http://redis.io/commands/dump
*
* @param string $key
* @return string The serialized value.
*/
public function dump($key) {
return $this->returnCommand(['DUMP'], $key, [$key]);
}
/**
* EXISTS key
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/exists
*
* @param string $key
* @return int 1 if the key exists. 0 if the key does not exist.
* Or the number of keys existing among the ones specified as arguments.
*/
public function exists($key) {
return $this->returnCommand(['EXISTS'], $key, [$key]);
}
/**
* EXPIRE key seconds
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/expire
*
* @param string $key
* @param int $seconds
* @return int 1 if the timeout was set. 0 if key does not exist or the timeout could not be set.
*/
public function expire($key, $seconds) {
return $this->returnCommand(['EXPIRE'], $key, [$key, $seconds]);
}
/**
* EXPIREAT key timestamp
* Available since 1.2.0.
* Time complexity: O(1)
* @link http://redis.io/commands/expireat
*
* @param string $key
* @param int $timestamp
* @return int 1 if the timeout was set. 0 if key does not exist or the timeout could not be set (see: EXPIRE).
*/
public function expireAt($key, $timestamp) {
return $this->returnCommand(['EXPIREAT'], $key, [$key, $timestamp]);
}
/**
* KEYS pattern
* Available since 1.0.0.
* Time complexity: O(N)
* @link http://redis.io/commands/keys
*
* @param string $pattern
* @return array List of keys matching pattern.
*/
public function keys($pattern) {
return $this->returnCommand(['KEYS'], null, [$pattern]);
}
/**
* MIGRATE host port key destination-db timeout
* Available since 2.6.0.
* @link http://redis.io/commands/migrate
*
* @param string $host
* @param int $port
* @param string $key
* @param int $destinationDb
* @param int $timeout In milliseconds
* @return bool The command returns True on success.
*/
public function migrate($host, $port, $key, $destinationDb, $timeout) {
return $this->returnCommand(['MIGRATE'], $key, [$host, $port, $key, $destinationDb, $timeout]);
}
/**
* MOVE key db
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/move
*
* @param string $key
* @param int $db
* @return int 1 if key was moved. 0 if key was not moved.
*/
public function move($key, $db) {
return $this->returnCommand(['MOVE'], $key, [$key, $db]);
}
/**
* OBJECT subcommand [arguments [arguments ...]]
* Available since 2.2.3.
* Time complexity: O(1) for all the currently implemented subcommands.
* @link http://redis.io/commands/object
*
* @param string $subcommand REFCOUNT|ENCODING|IDLETIME
* @param null|string|string[] $arguments
* @return int|string
*/
public function object($subcommand, $arguments = null) {
$params = [$subcommand];
if ($arguments) {
$params[] = (array) $arguments;
}
return $this->returnCommand(['OBJECT'], null, $params);
}
/**
* PERSIST key
* Available since 2.2.0.
* Time complexity: O(1)
* @link http://redis.io/commands/persist
*
* @param string $key
* @return int 1 if the timeout was removed.
* 0 if key does not exist or does not have an associated timeout.
*/
public function persist($key) {
return $this->returnCommand(['PERSIST'], $key, [$key]);
}
/**
* PEXPIRE key milliseconds
* Available since 2.6.0.
* Time complexity: O(1)
* @link http://redis.io/commands/pexpire
*
* @param string $key
* @param int $milliseconds
* @return int 1 if the timeout was set.
* 0 if key does not exist or the timeout could not be set.
*/
public function pexpire($key, $milliseconds) {
return $this->returnCommand(['PEXPIRE'], $key, [$key, $milliseconds]);
}
/**
* PEXPIREAT key milliseconds-timestamp
* Available since 2.6.0.
* Time complexity: O(1)
* @link http://redis.io/commands/pexpireat
*
* @param string $key
* @param int $millisecondsTimestamp
* @return int 1 if the timeout was set. 0 if key does not exist or the timeout could not be set (see: EXPIRE).
*/
public function pexpireat($key, $millisecondsTimestamp) {
return $this->returnCommand(['PEXPIREAT'], $key, [$key, $millisecondsTimestamp]);
}
/**
* PTTL key
* Available since 2.6.0.
* Time complexity: O(1)
* @link http://redis.io/commands/pttl
*
* @param string $key
* @return int TTL in milliseconds, or a negative value in order to signal an error.
*/
public function pttl($key) {
return $this->returnCommand(['PTTL'], $key, [$key]);
}
/**
* RANDOMKEY
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/randomkey
*
* @return string|null The random key, or null when the database is empty.
*/
public function randomkey() {
return $this->returnCommand(['RANDOMKEY']);
}
/**
* RENAME key newkey
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/rename
*
* @param string $key
* @param string $newkey
* @return bool True
*/
public function rename($key, $newkey) {
$keys = [$key, $newkey];
return $this->returnCommand(['RENAME'], $keys, $keys);
}
/**
* RENAMENX key newkey
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/renamenx
*
* @param string $key
* @param string $newkey
* @return int 1 if key was renamed to newkey. 0 if newkey already exists.
*/
public function renamenx($key, $newkey) {
$keys = [$key, $newkey];
return $this->returnCommand(['RENAMENX'], $keys, $keys);
}
/**
* RESTORE key ttl serialized-value
* Available since 2.6.0.
* Time complexity: O(1) to create the new key and additional O(N*M) to reconstruct the serialized value
* @link http://redis.io/commands/restore
*
* @param string $key
* @param int $ttl In milliseconds
* @param string $serializedValue
* @return bool The command returns True on success.
*/
public function restore($key, $ttl, $serializedValue) {
return $this->returnCommand(['RESTORE'], $key, [$key, $ttl, $serializedValue]);
}
/**
* SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination]
* Available since 1.0.0.
* Time complexity: O(N+M*log(M)) or O(N)
* @link http://redis.io/commands/sort
*
* @param string $key
* @param string|null $pattern
* @param int|array|null $limit
* @param string|string[]|null $patterns
* @param bool|null $asc
* @param bool $alpha
* @param string|null $destination
* @return mixed
*/
public function sort($key, $pattern = null, $limit = null, $patterns = null, $asc = null, $alpha = false, $destination = null) {
$params = [$key];
if ($pattern) {
$params[] = 'BY';
$params[] = $pattern;
}
if ($limit) {
$params[] = 'LIMIT';
$params[] = Parameter::limit($limit);
}
if ($patterns) {
foreach ((array)$patterns as $p) {
$params[] = 'GET';
$params[] = $p;
}
}
if (isset($asc)) {
$params[] = $asc ? 'ASC' : 'DESC';
}
if ($alpha) {
$params[] = 'ALPHA';
}
if ($destination) {
$params[] = 'STORE';
$params[] = $destination;
}
return $this->returnCommand(['SORT'], $key, $params);
}
/**
* TTL key
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/ttl
*
* @param string $key
* @return int TTL in seconds, or a negative value in order to signal an error
*/
public function ttl($key) {
return $this->returnCommand(['TTL'], $key, [$key]);
}
/**
* TYPE key
* Available since 1.0.0.
* Time complexity: O(1)
* @link http://redis.io/commands/type
*
* @param string $key
* @return string
*/
public function type($key) {
return $this->returnCommand(['TYPE'], $key, [$key]);
}
}