RENAMENX command in redis

RENAMENX oldkey newkey

Rename oldkey into newkey but fails if the destination key newkey already exists.

TIME COMPLEXITY: O(1)

RETURN VALUE: Integer reply, specifically:

1 if the key was renamed

0 if the target key already exists.

Syntax:

RENAMENX oldkey newkey

Example 1: When the newKey is not present.

redis:6379> SET redisKey1  1000
"OK"
redis:6379> RENAMENX redisKey1 redis
(integer) 1

Output

1


Example 2: When the newKey is present.

redis:6379> SET redisKey 1000
"OK"
redis:6379> SET redis 1000
"OK"
redis:6379> RENAMENX redis redisKey
(integer) 0

Output

0


Example 3: When the oldkey is not present.

redis:6379> RENAMENX myredis123 redis19
(error) no such key

Output

(error) no such key

No comments:

Post a Comment