| Command | Description | Supported | Tested | Class/Trait | Method |
|---|---|---|---|---|---|
| del | Delete a key [Blocking] | ✅ | ✅ | Keys | del |
| delete | Delete a key [Blocking] | ✅ | ✅ | Keys | delete |
| dump | Return a serialized version of the value stored at the specified key. | ✅ | ✅ | Keys | dump |
| exists | Determine if a key exists | ✅ | ✅ | Keys | exists |
| expire | Set a key's time to live in seconds | ✅ | ✅ | Keys | expire |
| expireAt | Set the expiration for a key as a UNIX timestamp | ✅ | ✅ | Keys | pexpireAt |
| getKeys | Find all keys matching the given pattern | ✅ | ✅ | Keys | getKeys |
| keys | Find all keys matching the given pattern | ✅ | ✅ | Keys | keys |
| migrate | Atomically transfer a key from a Redis instance to another one | ✅ | ✅ | Keys | migrate |
| move | Move a key to another database | ✅ | ✅ | Keys | move |
| object | Inspect the internals of Redis objects | ✅ | ✅ | Keys | object |
| persist | Remove the expiration from a key | ✅ | ✅ | Keys | persist |
| pexpire | Set a key's time to live in seconds | ✅ | ✅ | Keys | pexpire |
| pexpireAt | Set the expiration for a key as a UNIX timestamp with millisecond precision | ✅ | ✅ | Keys | pexpireAt |
| pttl | Get the time to live for a key | ✅ | ✅ | Keys | pttl |
| randomKey | Return a random key from the keyspace | ✅ | ✅ | Keys | randomKey |
| rename | Rename a key | ✅ | ✅ | Keys | rename |
| renameKey | Rename a key | ✅ | ✅ | Keys | renameKey |
| renameNx | Rename a key, only if the new key does not exist | ✅ | ✅ | Keys | renameNx |
| restore | Create a key using the provided serialized value, previously obtained with dump. | ✅ | ✅ | Keys | restore |
| scan | Scan for keys in the keyspace (Redis >= 2.8.0) | ✅ | ✅ | Keys | scan |
| setTimeout | Set a key's time to live in seconds | ✅ | ✅ | Keys | setTimeout |
| sort | Sort the elements in a list, set or sorted set | ✅ | ✅ | Keys | sort |
| ttl | Get the time to live for a key | ✅ | ✅ | Keys | ttl |
| type | Determine the type stored at key | ✅ | ✅ | Keys | type |
| unlink | Delete a key [Background] | ✅ | ✅ | Keys | unlink |
- del, delete, unlink - Delete a key
- dump - Return a serialized version of the value stored at the specified key.
- exists - Determine if a key exists
- expire, pexpire - Set a key's time to live in seconds
- expireAt, pexpireAt - Set the expiration for a key as a UNIX timestamp
- keys - Find all keys matching the given pattern
- scan - Scan for keys in the keyspace (Valkey >= 2.8.0)
- migrate - Atomically transfer a key from a Valkey instance to another one
- move - Move a key to another database
- object - Inspect the internals of Valkey objects
- persist - Remove the expiration from a key
- randomKey - Return a random key from the keyspace
- rename - Rename a key
- renameNx - Rename a key, only if the new key does not exist
- type - Determine the type stored at key
- sort - Sort the elements in a list, set or sorted set
- ttl, pttl - Get the time to live for a key
- restore - Create a key using the provided serialized value, previously obtained with dump.
$valkey = new Valkey();
$valkey->connect('127.0.0.1', 6379);
$valkey->del('key');
$valkey->delete('key');
$valkey->unlink('key');
$valkey->dump('key');
$valkey->exists('key');
$valkey->expire('key', 123);
$valkey->expireAt('key', 1743016214.623306);
$valkey->keys();
$valkey->scan();
$valkey->migrate('key');
$valkey->move('key');
$valkey->object('key');
$valkey->persist('key');
$valkey->randomKey('key');
$valkey->rename('key', 'yek');
$valkey->renameNx('key', 'yek');
$valkey->type('key');
$valkey->sort('key');
$valkey->ttl('key');
$valkey->restore('key');Description: Dump a key out of a redis database, the value of which can later be passed into redis using the RESTORE command. The data that comes out of DUMP is a binary representation of the key as Valkey stores it.
key string
The Valkey encoded value of the key, or FALSE if the key doesn't exist
$valkey->set('foo', 'bar');
$val = $valkey->dump('foo'); // $val will be the Valkey encoded key valueDescription: Migrates a key to a different Valkey instance.
Note:: Valkey 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.
host string. The destination host
port integer. The TCP port to connect to.
key(s) string or array.
destination-db integer. The target DB.
timeout integer. The maximum amount of time given to this transfer.
copy boolean, optional. Should we send the COPY flag to redis.
replace boolean, optional. Should we send the REPLACE flag to redis
$valkey->migrate('backup', 6379, 'foo', 0, 3600);
$valkey->migrate('backup', 6379, 'foo', 0, 3600, true, true); /* copy and replace */
$valkey->migrate('backup', 6379, 'foo', 0, 3600, false, true); /* just REPLACE flag */
/* Migrate multiple keys (requires Valkey >= 3.0.6)
$valkey->migrate('backup', 6379, ['key1', 'key2', 'key3'], 0, 3600);Description: Remove the expiration timer from a key.
Key: key
BOOL: TRUE if a timeout was removed, FALSE if the key didn’t exist or didn’t have an expiration timer.
$valkey->persist('key');Description: Restore a key from the result of a DUMP operation.
key string. The key name
ttl integer. How long the key should live (if zero, no expire will be set on the key)
value string (binary). The Valkey encoded key value (from DUMP)
$valkey->set('foo', 'bar');
$val = $valkey->dump('foo');
$valkey->restore('bar', 0, $val); // The key 'bar', will now be equal to the key 'foo'Description: Sort the elements in a list, set or sorted set.
Key: key Options: [key => value, ...] - optional, with the following keys and values:
'by' => 'some_pattern_*',
'limit' => [0, 1],
'get' => 'some_other_pattern_*' or an array of patterns,
'sort' => 'asc' or 'desc',
'alpha' => TRUE,
'store' => 'external-key'
An array of values, or a number corresponding to the number of elements stored if that was used.
$valkey->del('s');
$valkey->sAdd('s', 5);
$valkey->sAdd('s', 4);
$valkey->sAdd('s', 2);
$valkey->sAdd('s', 1);
$valkey->sAdd('s', 3);
var_dump($valkey->sort('s')); // 1,2,3,4,5
var_dump($valkey->sort('s', ['sort' => 'desc'])); // 5,4,3,2,1
var_dump($valkey->sort('s', ['sort' => 'desc', 'store' => 'out'])); // (int)5Description: Returns the time to live left for a given key in seconds (ttl), or milliseconds (pttl).
Key: key
LONG: The time to live in seconds. If the key has no ttl, -1 will be returned, and -2 if the key doesn't exist.
$valkey->ttl('key');Description: Returns a random key.
None.
STRING: an existing key in redis.
$key = $valkey->randomKey();
$surprise = $valkey->get($key); // who knows what's in there.Description: Moves a key to a different database.
Key: key, the key to move.
INTEGER: dbindex, the database number to move the key to.
BOOL: TRUE in case of success, FALSE in case of failure.
$valkey->select(0); // switch to DB 0
$valkey->set('x', '42'); // write 42 to x
$valkey->move('x', 1); // move to DB 1
$valkey->select(1); // switch to DB 1
$valkey->get('x'); // will return 42Description: Renames a key.
STRING: srckey, the key to rename.
STRING: dstkey, the new name for the key.
BOOL: TRUE in case of success, FALSE in case of failure.
$valkey->set('x', '42');
$valkey->rename('x', 'y');
$valkey->get('y'); // → 42
$valkey->get('x'); // → `FALSE`Description: Same as rename, but will not replace a key if the destination already exists. This is the same behaviour as setNx.
Description: Sets an expiration on a key in either seconds or milliseconds.
public function expire(string $key, int $seconds, ?string $mode = NULL): Valkey|bool;
public function pexpire(string $key, int $milliseconds, ?string $mode = NULL): Valkey|bool;BOOL: TRUE if an expiration was set, and FALSE on failure or if one was not set. You can distinguish between an error and an expiration not being set by checking getLastError().
$valkey->set('x', '42');
$valkey->expire('x', 3); // x will disappear in 3 seconds.
sleep(5); // wait 5 seconds
$valkey->get('x'); // will return `FALSE`, as 'x' has expired.Description: Seta specific timestamp for a key to expire in seconds or milliseconds.
public function expireat(string $key, int $unix_timestamp, ?string $mode = NULL): Valkey|bool;
public function pexpireat(string $key, int $unix_timestamp_millis, ?string $mode = NULL): Valkey|bool;BOOL: TRUE if an expiration was set and FALSE if one was not set or in the event on an error. You can detect an actual error by checking getLastError().
$valkey->set('x', '42');
$valkey->expireAt('x', time(NULL) + 3); // x will disappear in 3 seconds.
sleep(5); // wait 5 seconds
$valkey->get('x'); // will return `FALSE`, as 'x' has expired.Description: Returns the keys that match a certain pattern.
STRING: pattern, using '*' as a wildcard.
Array of STRING: The keys that match a certain pattern.
$allKeys = $valkey->keys('*'); // all keys will match this.
$keyWithUserPrefix = $valkey->keys('user*');Description: Scan the keyspace for keys
LONG (reference): Iterator, initialized to NULL STRING, Optional: Pattern to match LONG, Optional: Count of keys per iteration (only a suggestion to Valkey)
Array, boolean: This function will return an array of keys or FALSE if Valkey returned zero keys
Note: SCAN is a "directed node" command in ValkeyCluster
/* Without enabling Valkey::SCAN_RETRY (default condition) */
$it = NULL;
do {
// Scan for some keys
$arr_keys = $valkey->scan($it);
// Valkey may return empty results, so protect against that
if ($arr_keys !== FALSE) {
foreach($arr_keys as $str_key) {
echo "Here is a key: $str_key\n";
}
}
} while ($it > 0);
echo "No more keys to scan!\n";
/* With Valkey::SCAN_RETRY enabled */
$valkey->setOption(Valkey::OPT_SCAN, Valkey::SCAN_RETRY);
$it = NULL;
/* valkey-php will retry the SCAN command if empty results are returned from the
server, so no empty results check is required. */
while ($arr_keys = $valkey->scan($it)) {
foreach ($arr_keys as $str_key) {
echo "Here is a key: $str_key\n";
}
}
echo "No more keys to scan!\n";Description: Describes the object pointed to by a key.
The information to retrieve (string) and the key (string). Info can be one of the following:
- "encoding"
- "refcount"
- "idletime"
STRING for "encoding", LONG for "refcount" and "idletime", FALSE if the key doesn't exist.
$valkey->object("encoding", "l"); // → ziplist
$valkey->object("refcount", "l"); // → 1
$valkey->object("idletime", "l"); // → 400 (in seconds, with a precision of 10 seconds).Description: Returns the type of data pointed by a given key.
Key: key
Depending on the type of the data pointed by the key, this method will return the following value:
string: Valkey::VALKEY_STRING
set: Valkey::VALKEY_SET
list: Valkey::VALKEY_LIST
zset: Valkey::VALKEY_ZSET
hash: Valkey::VALKEY_HASH
other: Valkey::VALKEY_NOT_FOUND
$valkey->type('key');