Redis Incrby Command

Redis Strings

The Redis Incrby command adds the specified increment value to the number stored in key.

If the key does not exist, the key's value is first initialized to 0 before the INCRBY command is executed.

If the value contains the wrong type, or if a string value cannot be represented as a number, an error is returned.

The value in this operation is limited to within the 64-bit signed integer representation.

Syntax

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

redis 127.0.0.1:6379> INCRBY KEY_NAME INCR_AMOUNT

Available Versions

>= 1.0.0

Return Value

The value of key after the specified increment value has been added.

Example

# key 存在且是数字值

redis> SET rank 50
OK

redis> INCRBY rank 20
(integer) 70

redis> GET rank
"70"


# key 不存在时

redis> EXISTS counter
(integer) 0

redis> INCRBY counter 30
(integer) 30

redis> GET counter
"30"


# key 不是数字值时

redis> SET book "long long ago..."
OK

redis> INCRBY book 200
(error) ERR value is not an integer or out of range

Redis Strings

Other Extensions