Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Redis RANDOMKEY Command

Redis RANDOMKEY Command

Redis Key

The Redis RANDOMKEY command returns a random 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

Returns a key when the database is not empty. Returns nil when the database is empty (returns null on Windows systems).

Example



# Database is not empty



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"





# Database is empty



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

OK



redis> RANDOMKEY

(nil)

Redis Key