In this tutorial, we will learn about how to increment the string representing a integer value stored at a key in redis datastore, by using redis INCR and INCRBY commands.
INCR Command
The INCR command is used to increment the string representing a integer value stored at the specified key by one. If the key does not exist, it is first created and set to 0 before performing the increment operation. If the key exist but value stored at the key is of wrong datatype ( not string datatype ) or contains a string value that can not be represented as a integer then error is returned. This operation is limited to 64 bit signed integers. The syntax of redis INCR command is as follows :-
Syntax :-
redis host:post> INCR <keyname>
Output :-
- (integer) reply, representing the value of the key after the increment operation.
Example :-
INCRBY Command
The INCRBY command is used to increment the string representing a integer value stored at the key by a specified value. This command is very similar to INCR command with difference is that, in INCRBY integer value is increased by a specified value while in INCR integer value is always increased by one. The syntax of redis INCRBY command is as follows :-
Syntax :-
redis host:post> INCRBY <keyname> <increment>
Output :-
- (integer) reply, representing the value of the key after the increment operation.
Example :-
References :-
- INCR Command Docs
- INCRBY Command Docs
That’s all for how to increment the string representing a integer value stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.