INCR command in redis

INCR key

Increment the number stored at key by one. If the key does not exist or contains a value of the wrong type, set the key to the value of "0" before performing the increment or decrement operation.

TIME COMPLEXITY: O(1)

Note: INCR commands are limited to 64-bit signed integers.

RETURN VALUE: Integer reply, this command will reply with the new value of the key after the increment or decrement.

Syntax:

INCR KEY_NAME

Example 1: When the value is in the range of 64-bit.

redis:6379> SET MY_KEY 100
"OK"
redis:6379> INCR MY_KEY
(integer) 101

Output

101


Example 2: When the value is not an integer.

redis:6379> SET MY_KEY 100.881
"OK"
redis:6379> INCR MY_KEY
(error) value is not an integer or out of range

Output

(error) value is not an integer or out of range

Example 3: When the value is out of range of 64-bit

redis:6379> SET MY_KEY 1.8446744e+21
"OK"
redis:6379>

redis:6379> INCR MY_KEY
(error) value is not an integer or out of range

Output

(error) value is not an integer or out of range


No comments:

Post a Comment