|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Webdcg\Redis\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Webdcg\Redis\Exceptions\NotAssociativeArrayException; |
|
6
|
|
|
|
|
7
|
|
|
trait Keys |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Remove specified keys [Blocking]. |
|
11
|
|
|
* |
|
12
|
|
|
* @param mixed $keys |
|
13
|
|
|
* |
|
14
|
|
|
* @return int Number of keys deleted |
|
15
|
|
|
*/ |
|
16
|
|
|
public function del(...$keys): int |
|
17
|
|
|
{ |
|
18
|
|
|
return $this->redis->del(...$keys); |
|
|
|
|
|
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Remove specified keys [Non Blocking]. |
|
23
|
|
|
* |
|
24
|
|
|
* @param mixed $keys |
|
25
|
|
|
* |
|
26
|
|
|
* @return int Number of keys deleted |
|
27
|
|
|
*/ |
|
28
|
|
|
public function delete(...$keys): int |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->redis->unlink(...$keys); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Remove specified keys [NonBlocking]. |
|
35
|
|
|
* |
|
36
|
|
|
* Note: If you are connecting to Redis server >= 4.0.0 you can remove a |
|
37
|
|
|
* key with the unlink method in the exact same way you would use del. |
|
38
|
|
|
* The Redis unlink command is non-blocking and will perform the actual |
|
39
|
|
|
* deletion asynchronously. |
|
40
|
|
|
* |
|
41
|
|
|
* @param mixed $keys |
|
42
|
|
|
* |
|
43
|
|
|
* @return int Number of keys deleted |
|
44
|
|
|
*/ |
|
45
|
|
|
public function unlink(...$keys): int |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->redis->unlink(...$keys); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Return a serialized version of the value stored at the specified key. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $key |
|
54
|
|
|
* |
|
55
|
|
|
* @return mixed|string|false The Redis encoded value of the key, |
|
56
|
|
|
* or FALSE if the key doesn't exist |
|
57
|
|
|
*/ |
|
58
|
|
|
public function dump(string $key) |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->redis->dump($key); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Verify if the specified key exists. |
|
65
|
|
|
* |
|
66
|
|
|
* @param mixed] $keys |
|
|
|
|
|
|
67
|
|
|
* |
|
68
|
|
|
* @return int |
|
69
|
|
|
*/ |
|
70
|
|
|
public function exists(...$keys): int |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->redis->exists(...$keys); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Sets an expiration date (a timeout) on an item. pexpire requires a TTL in milliseconds. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $key. The key that will disappear. |
|
|
|
|
|
|
79
|
|
|
* @param int $ttl. The key's remaining Time To Live, in seconds. |
|
|
|
|
|
|
80
|
|
|
* |
|
81
|
|
|
* @return bool true in case of success, false in case of failure. |
|
82
|
|
|
*/ |
|
83
|
|
|
public function expire(string $key, int $ttl): bool |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->redis->expire($key, $ttl); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Sets an expiration date (a timeout) on an item. pexpire requires a TTL in milliseconds. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $key. The key that will disappear. |
|
|
|
|
|
|
92
|
|
|
* @param int $ttl. The key's remaining Time To Live, in seconds. |
|
|
|
|
|
|
93
|
|
|
* |
|
94
|
|
|
* @return bool true in case of success, false in case of failure. |
|
95
|
|
|
*/ |
|
96
|
|
|
public function setTimeout(string $key, int $ttl): bool |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->redis->expire($key, $ttl); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Sets an expiration date (a timeout) on an item. pexpire requires a TTL in milliseconds. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $key. The key that will disappear. |
|
|
|
|
|
|
105
|
|
|
* @param int $ttl. The key's remaining Time To Live, in seconds. |
|
|
|
|
|
|
106
|
|
|
* |
|
107
|
|
|
* @return bool true in case of success, false in case of failure. |
|
108
|
|
|
*/ |
|
109
|
|
|
public function pexpire(string $key, int $ttl): bool |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->redis->pexpire($key, $ttl); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Sets an expiration date (a timestamp) on an item. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $key The key that will disappear. |
|
118
|
|
|
* @param int $ttl Unix timestamp. The key's date of death, in seconds from Epoch time. |
|
119
|
|
|
* |
|
120
|
|
|
* @return bool true in case of success, false in case of failure. |
|
121
|
|
|
*/ |
|
122
|
|
|
public function expireAt(string $key, int $ttl): bool |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->redis->expireAt($key, $ttl); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Sets an expiration date (a timestamp) on an item in milliseconds. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $key The key that will disappear. |
|
131
|
|
|
* @param int $ttl Unix timestamp. The key's date of death, in |
|
132
|
|
|
* milliseconds from Epoch time with. |
|
133
|
|
|
* |
|
134
|
|
|
* @return bool true in case of success, false in case of failure. |
|
135
|
|
|
*/ |
|
136
|
|
|
public function pexpireAt(string $key, int $ttl): bool |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->redis->pexpireAt($key, $ttl); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Returns the keys that match a certain pattern. |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $pattern Pattern to match, using '*' as a wildcard. |
|
145
|
|
|
* |
|
146
|
|
|
* @return array The keys that match a certain pattern. |
|
147
|
|
|
*/ |
|
148
|
|
|
public function keys(string $pattern): array |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->redis->keys($pattern); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Returns the keys that match a certain pattern. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $pattern Pattern to match, using '*' as a wildcard. |
|
157
|
|
|
* |
|
158
|
|
|
* @return array The keys that match a certain pattern. |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getKeys(string $pattern): array |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->redis->keys($pattern); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Scan the keyspace for keys. |
|
167
|
|
|
* |
|
168
|
|
|
* @param int $iterator |
|
169
|
|
|
* @param string $pattern |
|
170
|
|
|
* @param int|int $count |
|
171
|
|
|
* |
|
172
|
|
|
* @return mixed|array|bool This function will return an array of keys |
|
173
|
|
|
* or FALSE if Redis returned zero keys. |
|
174
|
|
|
*/ |
|
175
|
|
|
public function scan(?int &$iterator = null, string $pattern = '*', int $count = 10) |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->redis->scan($iterator, $pattern, $count); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Migrates a key to a different Redis instance. |
|
182
|
|
|
* Note:: Redis introduced migrating multiple keys in 3.0.6, so you must |
|
183
|
|
|
* have at least that version in order to call migrate with an array of |
|
184
|
|
|
* keys. |
|
185
|
|
|
* See: https://redis.io/commands/migrate. |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $host The destination host |
|
188
|
|
|
* @param int $port The TCP port to connect to. |
|
189
|
|
|
* @param array $keys |
|
190
|
|
|
* @param int $db The target DB. |
|
191
|
|
|
* @param int $timeout The maximum amount of time given to this transfer. |
|
192
|
|
|
* @param bool $copy (optional) Should we send the COPY flag to redis. |
|
193
|
|
|
* @param bool $replace (optional) Should we send the REPLACE flag to redis. |
|
194
|
|
|
* |
|
195
|
|
|
* @return bool Simple string reply: The command returns OK |
|
196
|
|
|
* on success, or NOKEY if no keys were found |
|
197
|
|
|
* in the source instance. |
|
198
|
|
|
*/ |
|
199
|
|
|
public function migrate( |
|
200
|
|
|
string $host, |
|
201
|
|
|
int $port, |
|
202
|
|
|
array $keys, |
|
203
|
|
|
int $db, |
|
204
|
|
|
int $timeout, |
|
205
|
|
|
?bool $copy = false, |
|
|
|
|
|
|
206
|
|
|
?bool $replace = false |
|
|
|
|
|
|
207
|
|
|
): bool { |
|
208
|
|
|
return $this->redis->migrate($host, $port, $keys, $db, $timeout); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Moves a key to a different database. |
|
213
|
|
|
* See: https://redis.io/commands/move. |
|
214
|
|
|
* |
|
215
|
|
|
* @param string $key key, the key to move. |
|
216
|
|
|
* @param int $db dbindex, the database number to move the key to. |
|
217
|
|
|
* |
|
218
|
|
|
* @return bool TRUE in case of success, FALSE in case of failure. |
|
219
|
|
|
*/ |
|
220
|
|
|
public function move(string $key, int $db): bool |
|
221
|
|
|
{ |
|
222
|
|
|
return $this->redis->move($key, $db); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Describes the object pointed to by a key. |
|
227
|
|
|
* See: https://redis.io/commands/object. |
|
228
|
|
|
* |
|
229
|
|
|
* @param string $subcommand The information to retrieve. |
|
230
|
|
|
* @param string $key The key to fetch that data from. |
|
231
|
|
|
* |
|
232
|
|
|
* @return mixed|string|int|bool STRING for "encoding", LONG for |
|
233
|
|
|
* "refcount" and "idletime", FALSE if the |
|
234
|
|
|
* key doesn't exist. |
|
235
|
|
|
*/ |
|
236
|
|
|
public function object(string $subcommand, string $key) |
|
237
|
|
|
{ |
|
238
|
|
|
return $this->redis->object($subcommand, $key); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Remove the expiration timer from a key. |
|
243
|
|
|
* See: https://redis.io/commands/persist. |
|
244
|
|
|
* |
|
245
|
|
|
* @param string $key |
|
246
|
|
|
* |
|
247
|
|
|
* @return bool TRUE if a timeout was removed, FALSE if the key |
|
248
|
|
|
* didn’t exist or didn’t have an expiration timer. |
|
249
|
|
|
*/ |
|
250
|
|
|
public function persist(string $key): bool |
|
251
|
|
|
{ |
|
252
|
|
|
return $this->redis->persist($key); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Returns a random key. |
|
257
|
|
|
* See: https://redis.io/commands/randomkey. |
|
258
|
|
|
* |
|
259
|
|
|
* @return string An existing key in redis. |
|
260
|
|
|
*/ |
|
261
|
|
|
public function randomKey(): string |
|
262
|
|
|
{ |
|
263
|
|
|
return $this->redis->randomKey(); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Renames key to newkey. It returns an error when key does not exist. |
|
268
|
|
|
* If newkey already exists it is overwritten. |
|
269
|
|
|
* See: https://redis.io/commands/rename. |
|
270
|
|
|
* |
|
271
|
|
|
* @param string $key Source key, the key to rename. |
|
272
|
|
|
* @param string $newKey Destination key, the new name for the key. |
|
273
|
|
|
* |
|
274
|
|
|
* @return bool TRUE in case of success, FALSE in case of failure. |
|
275
|
|
|
*/ |
|
276
|
|
|
public function rename(string $key, string $newKey): bool |
|
277
|
|
|
{ |
|
278
|
|
|
return $this->redis->rename($key, $newKey); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Renames key to newkey. It returns an error when key does not exist. |
|
283
|
|
|
* If newkey already exists it is overwritten. |
|
284
|
|
|
* Note: renameKey is an alias for rename and will be removed in future versions of phpredis. |
|
285
|
|
|
* See: https://redis.io/commands/rename. |
|
286
|
|
|
* |
|
287
|
|
|
* @param string $key Source key, the key to rename. |
|
288
|
|
|
* @param string $newKey Destination key, the new name for the key. |
|
289
|
|
|
* |
|
290
|
|
|
* @return bool TRUE in case of success, FALSE in case of failure. |
|
291
|
|
|
*/ |
|
292
|
|
|
public function renameKey(string $key, string $newKey): bool |
|
293
|
|
|
{ |
|
294
|
|
|
return $this->redis->rename($key, $newKey); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* Renames key to newkey,. but will not replace a key if the destination |
|
299
|
|
|
* already exists. This is the same behaviour as setNx. |
|
300
|
|
|
* See: https://redis.io/commands/renamenx. |
|
301
|
|
|
* |
|
302
|
|
|
* @param string $key Source key, the key to rename. |
|
303
|
|
|
* @param string $newKey Destination key, the new name for the key. |
|
304
|
|
|
* |
|
305
|
|
|
* @return bool TRUE in case of success, FALSE in case of failure. |
|
306
|
|
|
*/ |
|
307
|
|
|
public function renameNx(string $key, string $newKey): bool |
|
308
|
|
|
{ |
|
309
|
|
|
return $this->redis->renameNx($key, $newKey); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* Returns the type of data pointed by a given key. |
|
314
|
|
|
* See: https://redis.io/commands/type. |
|
315
|
|
|
* |
|
316
|
|
|
* @param string $key |
|
317
|
|
|
* |
|
318
|
|
|
* @return mixed Depending on the type of the data pointed by the key, |
|
319
|
|
|
* this method will return the following value: |
|
320
|
|
|
* string: Redis::REDIS_STRING |
|
321
|
|
|
* set: Redis::REDIS_SET |
|
322
|
|
|
* list: Redis::REDIS_LIST |
|
323
|
|
|
* zset: Redis::REDIS_ZSET |
|
324
|
|
|
* hash: Redis::REDIS_HASH |
|
325
|
|
|
* other: Redis::REDIS_NOT_FOUND |
|
326
|
|
|
*/ |
|
327
|
|
|
public function type(string $key) |
|
328
|
|
|
{ |
|
329
|
|
|
return $this->redis->type($key); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* Sort the elements in a list, set or sorted set. |
|
334
|
|
|
* ONLY WORKS WITH NUMERIC VALUES |
|
335
|
|
|
* See: https://redis.io/commands/sort. |
|
336
|
|
|
* |
|
337
|
|
|
* @param string $key |
|
338
|
|
|
* @param array $options [key => value, ...] - optional, with the |
|
339
|
|
|
* following keys and values: |
|
340
|
|
|
* 'by' => 'some_pattern_*', |
|
341
|
|
|
* 'limit' => [0, 1], |
|
342
|
|
|
* 'get' => 'some_other_pattern_*' or an array of patterns, |
|
343
|
|
|
* 'sort' => 'asc' or 'desc', |
|
344
|
|
|
* 'alpha' => TRUE, |
|
345
|
|
|
* 'store' => 'external-key' |
|
346
|
|
|
* |
|
347
|
|
|
* @return mixed|array|int An array of values, or a number corresponding |
|
348
|
|
|
* to the number of elements stored if that was used. |
|
349
|
|
|
*/ |
|
350
|
|
|
public function sort(string $key, array $options = []) |
|
351
|
|
|
{ |
|
352
|
|
|
if (empty($options)) { |
|
353
|
|
|
return $this->redis->sort($key); |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
if (!$this->is_associative($options)) { |
|
|
|
|
|
|
357
|
|
|
throw new NotAssociativeArrayException('The array provided is not associative.', 1); |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
return $this->redis->sort($key, $options); |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* Returns the time to live left for a given key in seconds (ttl). |
|
365
|
|
|
* See: https://redis.io/commands/ttl. |
|
366
|
|
|
* |
|
367
|
|
|
* @param string $key |
|
368
|
|
|
* |
|
369
|
|
|
* @return int The time to live in seconds. If the key has no ttl, |
|
370
|
|
|
* -1 will be returned, and -2 if the key doesn't exist. |
|
371
|
|
|
*/ |
|
372
|
|
|
public function ttl(string $key): int |
|
373
|
|
|
{ |
|
374
|
|
|
return $this->redis->ttl($key); |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
/** |
|
378
|
|
|
* Returns the time to live left for a given key in milliseconds (pttl). |
|
379
|
|
|
* See: https://redis.io/commands/pttl. |
|
380
|
|
|
* |
|
381
|
|
|
* @param string $key |
|
382
|
|
|
* |
|
383
|
|
|
* @return int The time to live in seconds. If the key has no ttl, |
|
384
|
|
|
* -1 will be returned, and -2 if the key doesn't exist. |
|
385
|
|
|
*/ |
|
386
|
|
|
public function pttl(string $key): int |
|
387
|
|
|
{ |
|
388
|
|
|
return $this->redis->pttl($key); |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
/** |
|
392
|
|
|
* Restore a key from the result of a DUMP operation. |
|
393
|
|
|
* See: https://redis.io/commands/restore. |
|
394
|
|
|
* |
|
395
|
|
|
* @param string $key The key name |
|
396
|
|
|
* @param int $ttl How long the key should live (if zero, no expire will be set on the key) |
|
397
|
|
|
* @param string $value String (binary). The Redis encoded key value (from DUMP) |
|
398
|
|
|
* |
|
399
|
|
|
* @return mixed |
|
400
|
|
|
*/ |
|
401
|
|
|
public function restore(string $key, int $ttl = 0, string $value = '') |
|
402
|
|
|
{ |
|
403
|
|
|
return $this->redis->restore($key, $ttl, $value); |
|
404
|
|
|
} |
|
405
|
|
|
} |
|
406
|
|
|
|
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: