English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Redis RANDOMKEY Command

Redis Key (key)

The Redis RANDOMKEY command randomly returns a key from the current database .

Syntax

The basic syntax of the redis RANDOMKEY command is as follows:

redis 127.0.0.1:6379> RANDOMKEY 

Available Version

>= 1.0.0

Return Value

When the database is not empty, it returns a key . When the database is empty, it returns nil (null is returned on Windows systems).

Online Examples

# Database is not empty
redis> MSET fruit "apple" drink "beer" food "cookies"   # Set multiple keys
OK
redis> RANDOMKEY
"fruit"
redis> RANDOMKEY
"food"
redis> KEYS *    # View all keys in the database to prove that RANDOMKEY does not delete keys
1) "food"
2) "drink"
3) "fruit"
# Database is empty
redis> FLUSHDB  # Delete all keys in the current database
OK
redis> RANDOMKEY
(nil)

Redis Key (key)