Redis Renamenx Command

Redis Key

The Redis Renamenx command is used to change the name of a key when the new key does not exist.

Syntax

The basic syntax of the Redis Renamenx command is as follows:

redis 127.0.0.1:6379> RENAMENX OLD_KEY_NAME NEW_KEY_NAME

Available Versions

>= 1.0.0

Return Value

Returns 1 if the modification is successful. If NEW_KEY_NAME already exists, returns 0.

Example

# newkey 不存在,改名成功

redis> SET player "MPlyaer"
OK

redis> EXISTS best_player
(integer) 0

redis> RENAMENX player best_player
(integer) 1


# newkey存在时,失败

redis> SET animal "bear"
OK

redis> SET favorite_animal "butterfly"
OK

redis> RENAMENX animal favorite_animal
(integer) 0

redis> get animal
"bear"

redis> get favorite_animal
"butterfly"

Redis Key

Other Extensions