Skip to content

Commit 21d2738

Browse files
committed
Redis | Keys table
1 parent cab7cda commit 21d2738

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

docs/keys.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
|[scan](#scan) | Scan for keys in the keyspace (Redis >= 2.8.0) |:white\_check\_mark: |:white\_check\_mark: |Keys |scan |
1919
|[migrate](#migrate) | Atomically transfer a key from a Redis instance to another one |:white\_check\_mark: |:white\_check\_mark: |Keys |migrate |
2020
|[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: |:white\_check\_mark: |Keys |object |
22-
|[persist](#persist) | Remove the expiration from a key |:white\_check\_mark: |:white\_check\_mark: |Keys |persist |
23-
|[randomKey](#randomKey) | Return a random key from the keyspace |:white\_check\_mark: |:white\_check\_mark: |Keys |randomKey |
24-
|[rename](#rename) | Rename a key |:white\_check\_mark: |:white\_check\_mark: |Keys |rename |
25-
|[renameKey](#renameKey) | Rename a key |:white\_check\_mark: |:white\_check\_mark: |Keys |renameKey |
26-
|[renameNx](#renameNx) | Rename a key, only if the new key does not exist |:white\_check\_mark: |:white\_check\_mark: |Keys |renameNx |
27-
|[type](#type) | Determine the type stored at key |:x: |:x: |Keys |type |
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 |:white\_check\_mark: |:white\_check\_mark: |Keys |ttl |
30-
|[pttl](#pttl) | Get the time to live for a key |:white\_check\_mark: |:white\_check\_mark: |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

src/Traits/Keys.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,18 @@ public function pttl(string $key): int
385385
return $this->redis->pttl($key);
386386
}
387387

388-
public function restore(): bool
388+
/**
389+
* Restore a key from the result of a DUMP operation.
390+
* See: https://redis.io/commands/restore.
391+
*
392+
* @param string $key The key name
393+
* @param int $ttl How long the key should live (if zero, no expire will be set on the key)
394+
* @param string $value String (binary). The Redis encoded key value (from DUMP)
395+
*
396+
* @return mixed
397+
*/
398+
public function restore(string $key, int $ttl = 0, string $value = '')
389399
{
390-
return false;
400+
return $this->redis->restore($key, $ttl, $value);
391401
}
392402
}

tests/RedisKeysTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,54 @@ protected function setUp(): void
2121
$this->keyOptional = 'KeysOptional';
2222
}
2323

24+
/** @test */
25+
public function redis_keys_restore_string_key()
26+
{
27+
// Start from scratch
28+
$this->assertGreaterThanOrEqual(0, $this->redis->delete($this->key));
29+
$this->assertGreaterThanOrEqual(0, $this->redis->delete($this->keyOptional));
30+
$this->assertTrue($this->redis->set($this->key, 'value'));
31+
$value = $this->redis->dump($this->key);
32+
// -------------------- T E S T --------------------
33+
$this->assertTrue($this->redis->restore($this->keyOptional, 0, $value));
34+
// -------------------- T E S T --------------------
35+
$this->assertEquals('value', $this->redis->get($this->keyOptional));
36+
$this->assertGreaterThanOrEqual(0, $this->redis->delete($this->key));
37+
$this->assertGreaterThanOrEqual(0, $this->redis->delete($this->keyOptional));
38+
}
39+
40+
/** @test */
41+
public function redis_keys_restore_int_key()
42+
{
43+
// Start from scratch
44+
$this->assertGreaterThanOrEqual(0, $this->redis->delete($this->key));
45+
$this->assertGreaterThanOrEqual(0, $this->redis->delete($this->keyOptional));
46+
$this->assertTrue($this->redis->set($this->key, 123));
47+
$value = $this->redis->dump($this->key);
48+
// -------------------- T E S T --------------------
49+
$this->assertTrue($this->redis->restore($this->keyOptional, 0, $value));
50+
// -------------------- T E S T --------------------
51+
$this->assertEquals(123, $this->redis->get($this->keyOptional));
52+
$this->assertGreaterThanOrEqual(0, $this->redis->delete($this->key));
53+
$this->assertGreaterThanOrEqual(0, $this->redis->delete($this->keyOptional));
54+
}
55+
56+
/** @test */
57+
public function redis_keys_restore_float_key()
58+
{
59+
// Start from scratch
60+
$this->assertGreaterThanOrEqual(0, $this->redis->delete($this->key));
61+
$this->assertGreaterThanOrEqual(0, $this->redis->delete($this->keyOptional));
62+
$this->assertTrue($this->redis->set($this->key, 123.456));
63+
$value = $this->redis->dump($this->key);
64+
// -------------------- T E S T --------------------
65+
$this->assertTrue($this->redis->restore($this->keyOptional, 0, $value));
66+
// -------------------- T E S T --------------------
67+
$this->assertEquals(123.456, $this->redis->get($this->keyOptional));
68+
$this->assertGreaterThanOrEqual(0, $this->redis->delete($this->key));
69+
$this->assertGreaterThanOrEqual(0, $this->redis->delete($this->keyOptional));
70+
}
71+
2472
/** @test */
2573
public function redis_keys_type_string()
2674
{

0 commit comments

Comments
 (0)