Redis RANDOMKEY Command

Redis 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 Versions

>= 1.0.0

Return Value

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

Example

# 数据库不为空

redis> MSET fruit "apple" drink "beer" food "cookies"   # 设置多个 key
OK

redis> RANDOMKEY
"fruit"

redis> RANDOMKEY
"food"

redis> KEYS *    # 查看数据库内所有key,证明 RANDOMKEY 并不删除 key
1) "food"
2) "drink"
3) "fruit"


# 数据库为空

redis> FLUSHDB  # 删除当前数据库所有 key
OK

redis> RANDOMKEY
(nil)

Redis Key

Other Extensions