Skip to content

Commit 3f6367a

Browse files
authored
Merge pull request #79 from webdcg/Keys
Keys
2 parents 3339c54 + 21d2738 commit 3f6367a

16 files changed

+923
-168
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ composer.lock
33
vendor
44
coverage
55
.idea
6-
clover.xml
6+
clover.xml
7+
.ecs_cache
8+
.phpintel

.styleci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
preset: laravel
1+
preset: psr12
22

33
disabled:
44
- single_class_element_per_statement

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ before_script:
2525
- travis_retry composer self-update
2626
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
2727
- sudo redis-server /etc/redis/redis.conf --port 6380 --requirepass 'secret'
28+
- sudo redis-server /etc/redis/redis.conf --port 6381
2829

2930
script:
3031
- "vendor/bin/phpunit --coverage-text --coverage-clover build/logs/clover.xml"

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"require-dev": {
2525
"phpunit/phpunit": "^7.5",
2626
"scrutinizer/ocular": "^1.6",
27-
"symfony/var-dumper": "^5.0"
27+
"symfony/var-dumper": "^5.0",
28+
"symplify/easy-coding-standard": "^7.2"
2829
},
2930
"autoload": {
3031
"psr-4": {
@@ -37,6 +38,7 @@
3738
}
3839
},
3940
"scripts": {
41+
"check": "vendor/bin/ecs check",
4042
"test": "vendor/bin/phpunit",
4143
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
4244

docs/keys.md

Lines changed: 145 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
|[pexpireAt](#pexpireAt) |Set the expiration for a key as a UNIX timestamp with millisecond precision |:white\_check\_mark: |:white\_check\_mark: |Keys |pexpireAt |
1616
|[keys](#keys) |Find all keys matching the given pattern |:white\_check\_mark: |:white\_check\_mark: |Keys |keys |
1717
|[getKeys](#getKeys) |Find all keys matching the given pattern |:white\_check\_mark: |:white\_check\_mark: |Keys |getKeys |
18-
|[scan](#scan) | Scan for keys in the keyspace (Redis >= 2.8.0) |:x: |:x: |Keys |scan |
19-
|[migrate](#migrate) | Atomically transfer a key from a Redis instance to another one |:x: |:x: |Keys |migrate |
20-
|[move](#move) | Move a key to another database |:x: |:x: |Keys |move |
21-
|[object](#object) | Inspect the internals of Redis objects |:x: |:x: |Keys |object |
22-
|[persist](#persist) | Remove the expiration from a key |:x: |:x: |Keys |persist |
23-
|[randomKey](#randomKey) | Return a random key from the keyspace |:x: |:x: |Keys |randomKey |
24-
|[rename](#rename) | Rename a key |:x: |:x: |Keys |rename |
25-
|[renameKey](#renameKey) | Rename a key |:x: |:x: |Keys |renameKey |
26-
|[renameNx](#renameNx) | Rename a key, only if the new key does not exist |:x: |:x: |Keys |renameNx |
27-
|[type](#type) | Determine the type stored at key |:x: |:x: |Keys |type |
18+
|[scan](#scan) | Scan for keys in the keyspace (Redis >= 2.8.0) |:white\_check\_mark: |:white\_check\_mark: |Keys |scan |
19+
|[migrate](#migrate) | Atomically transfer a key from a Redis instance to another one |:white\_check\_mark: |:white\_check\_mark: |Keys |migrate |
20+
|[move](#move) | Move a key to another database |:white\_check\_mark: |:white\_check\_mark: |Keys |move |
21+
|[object](#object) | Inspect the internals of Redis objects |:white\_check\_mark: |:x: |Keys |object |
22+
|[persist](#persist) | Remove the expiration from a key |:white\_check\_mark: |:x: |Keys |persist |
23+
|[randomKey](#randomKey) | Return a random key from the keyspace |:white\_check\_mark: |:x: |Keys |randomKey |
24+
|[rename](#rename) | Rename a key |:white\_check\_mark: |:x: |Keys |rename |
25+
|[renameKey](#renameKey) | Rename a key |:white\_check\_mark: |:x: |Keys |renameKey |
26+
|[renameNx](#renameNx) | Rename a key, only if the new key does not exist |:white\_check\_mark: |:x: |Keys |renameNx |
27+
|[type](#type) | Determine the type stored at key |:white\_check\_mark: |:x: |Keys |type |
2828
|[sort](#sort) | Sort the elements in a list, set or sorted set |:x: |:x: |Keys |sort |
29-
|[ttl](#ttl) | Get the time to live for a key |:x: |:x: |Keys |ttl |
30-
|[pttl](#pttl) | Get the time to live for a key |:x: |:x: |Keys |pttl |
31-
|[restore](#restore) | Create a key using the provided serialized value, previously obtained with dump. |:x: |:x: |Keys |restore |
29+
|[ttl](#ttl) | Get the time to live for a key |:white\_check\_mark: |:x: |Keys |ttl |
30+
|[pttl](#pttl) | Get the time to live for a key |:white\_check\_mark: |:x: |Keys |pttl |
31+
|[restore](#restore) | Create a key using the provided serialized value, previously obtained with dump. |:white\_check\_mark: |:x: |Keys |restore |
3232

3333
## del
3434

@@ -412,3 +412,135 @@ array(3) {
412412
}
413413
*/
414414
```
415+
416+
## scan
417+
418+
_**Description**_: Scan the keyspace for keys.
419+
420+
##### *Prototype*
421+
422+
```php
423+
public function scan($iterator = null, string $pattern = '*', int $count = 10) {
424+
return $this->redis->scan($iterator, $pattern, $count);
425+
}
426+
```
427+
428+
##### *Parameters*
429+
430+
- *iterator*: String. LONG (reference): Iterator, initialized to NULL.
431+
- *pattern*: String. Pattern to match, using '\*' as a wildcard.
432+
- *count*: Integer. LONG, Optional: Count of keys per iteration (only a suggestion to Redis).
433+
434+
##### *Return value*
435+
436+
*array*: Array, boolean: This function will return an array of keys or FALSE if Redis returned zero keys.
437+
438+
##### *Example*
439+
440+
```php
441+
/* Without enabling Redis::SCAN_RETRY (default condition) */
442+
$it = NULL;
443+
do {
444+
// Scan for some keys
445+
$arr_keys = $redis->scan($it);
446+
447+
// Redis may return empty results, so protect against that
448+
if ($arr_keys !== FALSE) {
449+
foreach($arr_keys as $str_key) {
450+
echo "Here is a key: $str_key\n";
451+
}
452+
}
453+
} while ($it > 0);
454+
echo "No more keys to scan!\n";
455+
456+
/* With Redis::SCAN_RETRY enabled */
457+
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
458+
$it = NULL;
459+
460+
/* phpredis will retry the SCAN command if empty results are returned from the
461+
server, so no empty results check is required. */
462+
while ($arr_keys = $redis->scan($it)) {
463+
foreach ($arr_keys as $str_key) {
464+
echo "Here is a key: $str_key\n";
465+
}
466+
}
467+
echo "No more keys to scan!\n";
468+
```
469+
470+
## migrate
471+
472+
_**Description**_: Migrates a key to a different Redis instance.
473+
474+
Note:: Redis introduced migrating multiple keys in 3.0.6, so you must have at least that version in order to call migrate with an array of keys.
475+
476+
##### *Prototype*
477+
478+
```php
479+
public function migrate(
480+
string $host,
481+
int $port,
482+
array $keys,
483+
int $db,
484+
int $timeout,
485+
?bool $copy = false,
486+
?bool $replace = false
487+
) : bool {
488+
return $this->redis->migrate($host, $port, $keys, $db, $timeout);
489+
}
490+
```
491+
492+
##### *Parameters*
493+
494+
- *host*: String. The destination host.
495+
- *port*: Integer. The TCP port to connect to.
496+
- *keys*: Array. Key(s) to be moved.
497+
- *db*: Integer. The target DB.
498+
- *timeout*: Integer. The maximum amount of time given to this transfer.
499+
- *copy*: Boolean. (optional) Should we send the COPY flag to redis.
500+
- *replace*: Boolean. (optional) Should we send the REPLACE flag to redis.
501+
502+
##### *Return value*
503+
504+
*array*: Array of string: The keys that match a certain pattern.
505+
506+
##### *Example*
507+
508+
```php
509+
$redis->migrate('backup', 6379, 'foo', 0, 3600);
510+
$redis->migrate('backup', 6379, 'foo', 0, 3600, true, true); /* copy and replace */
511+
$redis->migrate('backup', 6379, 'foo', 0, 3600, false, true); /* just REPLACE flag */
512+
513+
/* Migrate multiple keys (requires Redis >= 3.0.6)
514+
$redis->migrate('backup', 6379, ['key1', 'key2', 'key3'], 0, 3600);
515+
```
516+
517+
## move
518+
519+
_**Description**_: Moves a key to a different database.
520+
521+
##### *Prototype*
522+
523+
```php
524+
public function move(string $key, int $db): bool {
525+
return $this->redis->move($key, $db);
526+
}
527+
```
528+
529+
##### *Parameters*
530+
531+
- *key*: String. the key to move.
532+
- *db*: Integer. dbindex, the database number to move the key to.
533+
534+
##### *Return value*
535+
536+
*bool*: true in case of success, false in case of failure.
537+
538+
##### *Example*
539+
540+
```php
541+
$redis->select(0); // switch to DB 0
542+
$redis->set('x', '42'); // write 42 to x
543+
$redis->move('x', 1); // move to DB 1
544+
$redis->select(1); // switch to DB 1
545+
$redis->get('x'); // will return 42
546+
```

ecs.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# ecs.yaml
2+
parameters:
3+
cache_directory: .ecs_cache
4+
sets:
5+
- 'clean-code'
6+
- 'psr12'
7+
paths:
8+
- 'src'
9+
- 'tests'

src/Redis.php

100755100644
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,16 @@ public function __construct()
2424
{
2525
$this->redis = new \Redis();
2626
}
27+
28+
/**
29+
* Check that the given array is associative.
30+
*
31+
* @param array $array
32+
*
33+
* @return bool
34+
*/
35+
public function is_associative(array $array)
36+
{
37+
return array_keys($array) !== range(0, count($array) - 1);
38+
}
2739
}

src/Traits/Bits.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function bitOp(string $operation, string $returnKey, ...$keys): int
3636
{
3737
$operation = strtoupper($operation);
3838

39-
if (! in_array($operation, $this->BIT_OPERATIONS)) {
39+
if (!in_array($operation, $this->BIT_OPERATIONS)) {
4040
throw new BitwiseOperationException('Operation not supported', 1);
4141
}
4242

0 commit comments

Comments
 (0)