INCR

Redis Developer Course Redis Cloud Service Redis-Gate Server

Increment by one

Increment(adding) the number by 1.   If the key does not exist in the database, it is set to 0 before performing the operation, add 1 and return it (1).
Usage is INCR key.

Return value

Integer reply: the value of key after the increment

Example

command>incr key
result>1
command>incr key
result>2
command>set key 10
result>OK
command>incr key
result>11

Animation



Error

An error occurs if you execute incr command on a string or if the integer value is exceeded as a result of the incr command. This operation is limited to 64 bit signed integers.   Redis integer is a 64-bit signed integer with a range of -9,223,372,036,854,775,808(-264) ~ 9,223,372,036,854,775,807(263-1).

Example

command>set key Hello
result>OK
command>incr key
result>ERR value is not an integer or out of range
command>set key 234293482390480948029348230948   This is stored as a string rather than a number.  
result>OK
command>incr key
result>ERR value is not an integer or out of range
command>set key 9223372036854775807   This is the largest positive integer (263 − 1) 
result>OK
command>incr key
result>ERR increment or decrement would overflow


Usage: page view counter

If you use the incr command for page view counter, the key should look like 'pagename:date'.

Example

command>incr pagename:20170221
result>1
command>incr pagename:20170221
result>2
command>incr pagename:20170222   New date  
result>1


Syntax

INCR key

  • Available since 1.0.0.
  • Time complexity: O(1)
Related Commands DECR, INCRBY, INCRBYFLOAT
Clients for Java Jedis, Lettuce, Redisson
Clients for C Hiredis

To practice the command, click here to run Redis Web Client


<< GET INCR DECR >>

Click for a question or to see comments.  Comments :    Viewed :

Email 답글이 올라오면 이메일로 알려드리겠습니다.