incr
INCR
Increment by 1
Increments a number by 1. If the key does not exist in the database, 0 is used as the base, 1 is added, and the result is returned.
Example
| Command> | incr key
|
| Result> | 1
|
| Command> | incr key
|
| Result> | 2
|
| Command> | set key 10
|
| Result> | OK
|
| Command> | incr key
|
| Result> | 11
|
View animation
Error
An error occurs if you run incr on a string, or if the result of incr exceeds the integer range.
A Redis integer is a 64-bit signed integer. The range is
-9,223,372,036,854,775,808(263) ~ 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, not a number. |
| Result> | OK |
| Command> | incr key |
| Result> | ERR value is not an integer or out of range |
| Command> | set key 9223372036854775807 The largest positive integer 263 − 1 |
| Result> | OK |
| Command> | incr key |
| Result> | ERR increment or decrement would overflow |
Page views example
If you use the incr command for page views, use a key 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 version 1.0.0.
- Time complexity is O(1).