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

Redis Incrby command

Redis Incrby command

Redis string

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

If the key does not exist, its value is first initialized to 0, then the INCRBY command is performed.

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

The value of this operation is limited to a 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 Version

= 1.0.0

Return Value

The value of key after adding the specified increment.

Example



# key exists and is a numeric value



redis> SET rank 50

OK



redis> INCRBY rank 20

(integer) 70



redis> GET rank

"70"





# when key does not exist



redis> EXISTS counter

(integer) 0



redis> INCRBY counter 30

(integer) 30



redis> GET counter

"30"





# when key is not a numeric value



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

OK



redis> INCRBY book 200

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

Redis string