Summary
Command Name | INCR |
Usage | Increment integer value |
Group | string |
ACL Category | @write @string @fast |
Time Complexity | O(1) |
Flag | WRITE DENYOOM FAST |
Arity | 2 |
Signature
INCR <key>
Usage
Increase the value stored in <key> by One(1).
Used when we want to increase some count by one. For example, we want to save-
- Total number of logged-in users.
- Number of times the user tries to log in or reset the password.
- Number of times the user tries to access some resource or API endpoint.
Notes
- If the key does not exist then this command will set the key and the value will be Zero(0), before performing the operation. And after the operation is performed, the value will be one.
Arguments
Parameter | Description | Name | Type |
---|---|---|---|
<key> | The key name | key | key |
Return Value
Returns value after increament is performed.
Return value | Case for the return value | Type |
---|---|---|
Integer value | The value of key after the increment | integer |
error | When the saved value for the key is not a valid integer value or the increased value exceeds the max allowed value | error |
Notes
- If the value is not an integer, like a string that can not be covered to an integer, or a list, set, etc. then this command will return an error. The error message will be as below-
(error) ERR value is not an integer or out of range - If the value exceeds the maximum allowed integer value of 64-bit signed integer, then we get the following error message-
(error) ERR increment or decrement would overflow
Examples
Here are a few examples of the INCR command usage-
# Redis INCR command examples
# Set the value of total-user-no key to 10
127.0.0.1:6379> set total-user-no 10
OK
# Increament value of total-user-no
127.0.0.1:6379> incr total-user-no
(integer) 11
# Check value of total-user-no key
127.0.0.1:6379> get total-user-no
"11"
# Check type of total-user-no
127.0.0.1:6379> type total-user-no
string
# Check if some key named "unknownkey" exists
# it does not exist yet
127.0.0.1:6379> get unknownkey
(nil)
# Try to increament the value of "unknownkey" using INCR command
# The value of "unknownkey" is increamented to 1
127.0.0.1:6379> incr unknownkey
(integer) 1
# Check the value of "unknownkey"
127.0.0.1:6379> get unknownkey
"1"
# Set a string vlaue to sitename key
127.0.0.1:6379> set sitename bigboxcode
OK
# Try to apply INCR command to sitename
# We get an error as the value in sitename key is not an integer
127.0.0.1:6379> incr sitename
(error) ERR value is not an integer or out of range
# Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
# Let's set the value of key "mymaxtest" to a value close to the max value
127.0.0.1:6379> set mymaxtest 9223372036854775806
OK
# Let's increament the vlaue of "mymaxtest"
# It reaches the max value
127.0.0.1:6379> incr mymaxtest
(integer) 9223372036854775807
# Let's try to increase the value of "mymaxtest"
# We get an error as it goes beyond the max value
127.0.0.1:6379> incr mymaxtest
(error) ERR increment or decrement would overflow
# Min value allowed as 64-bit int is -9,223,372,036,854,775,808
# Lets set a value less than that minmum interger value
# It will be set, as it is a valid string
127.0.0.1:6379> set mymintest -9223372036854775809
OK
# If we try to perform the INCR operation then we get error
127.0.0.1:6379> incr mymintest
(error) ERR value is not an integer or out of range
Notes
- Redis does not have a specific data type for integer. We have to save the value in a string, for storing an integer value.
- Before performing the INCR operation the string value is converted to a 64-bit signed integer(base 10), and then the operation is performed.
- As the max value of a 64-bit signed integer is 9,223,372,036,854,775,807. So if we try to apply the INCR command to a key that has that max value, then we get the following error-
(error) ERR increment or decrement would overflow - The minimum value that we can perform this operation is -9,223,372,036,854,775,808.
Code Implementations
Here are the usage examples of the Redis APPEND command in different programming languages.
// Redis INCR command example in Golang
package main
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
var rdb *redis.Client
var ctx context.Context
func init() {
rdb = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Username: "default",
Password: "",
DB: 0,
})
ctx = context.Background()
}
func main() {
// Set the value of total-user-no key to 10
// Command: set total-user-no 10
// Result: OK
setCommandResult, err := rdb.Set(ctx, "total-user-no", "10", 0).Result()
if err != nil {
fmt.Println("Command: set total-user-no 10 | Error: " + err.Error())
}
fmt.Println("Command: set total-user-no 10 | Result: " + setCommandResult)
// Increment value of total-user-no
// Command: incr total-user-no
// Result: (integer) 11
incrCommandResult, err := rdb.Incr(ctx, "total-user-no").Result()
if err != nil {
fmt.Println("Command: incr total-user-no | Error: " + err.Error())
}
fmt.Printf("Command: incr total-user-no | Result: %vn", incrCommandResult)
// Check value of total-user-no key
// Command: get total-user-no
// Result: "11"
getCommandResult, err := rdb.Get(ctx, "total-user-no").Result()
if err != nil {
fmt.Println("Command: get total-user-no | Error: " + err.Error())
}
fmt.Println("Command: get total-user-no | Result: " + getCommandResult)
// Check type of total-user-no
// Command: type total-user-no
// Result: string
getCommandResult, err = rdb.Type(ctx, "total-user-no").Result()
if err != nil {
fmt.Println("Command: type total-user-no | Error: " + err.Error())
}
fmt.Println("Command: type total-user-no | Result: " + getCommandResult)
// Check if some key named "unknownkey" exists
// it does not exist yet
// Command: get unknownkey
// Result: (nil)
getCommandResult, err = rdb.Get(ctx, "unknownkey").Result()
if err != nil {
fmt.Println("Command: get unknownkey | Error: " + err.Error())
}
fmt.Println("Command: get unknownkey | Result: " + getCommandResult)
// Try to increament the value of "unknownkey" using INCR command
// The value of "unknownkey" is increamented to 1
// Command: incr unknownkey
// Result: (integer) 1
incrCommandResult, err = rdb.Incr(ctx, "unknownkey").Result()
if err != nil {
fmt.Println("Command: incr unknownkey | Error: " + err.Error())
}
fmt.Printf("Command: incr unknownkey | Result: %vn", incrCommandResult)
// Check the value of "unknownkey"
// Command: get unknownkey
// Result: "1"
getCommandResult, err = rdb.Get(ctx, "unknownkey").Result()
if err != nil {
fmt.Println("Command: get unknownkey | Error: " + err.Error())
}
fmt.Println("Command: get unknownkey | Result: " + getCommandResult)
// Set a string vlaue to sitename key
// Command: set sitename bigboxcode
// Result: OK
setCommandResult, err = rdb.Set(ctx, "sitename", "bigboxcode", 0).Result()
if err != nil {
fmt.Println("Command: set sitename bigboxcode | Error: " + err.Error())
}
fmt.Println("Command: set sitename bigboxcode | Result: " + setCommandResult)
// Try to apply INCR command to sitename
// We get an error as the value in sitename key is not an integer
// Command: incr sitename
// Result: (error) ERR value is not an integer or out of range
incrCommandResult, err = rdb.Incr(ctx, "sitename").Result()
if err != nil {
fmt.Println("Command: incr sitename | Error: " + err.Error())
}
fmt.Printf("Command: incr sitename | Result: %vn", incrCommandResult)
// Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
// Let's set the value of key "mymaxtest" to a value close to the max value
// Command: set mymaxtest 9223372036854775806
// Result: OK
setCommandResult, err = rdb.Set(ctx, "mymaxtest", "9223372036854775806", 0).Result()
if err != nil {
fmt.Println("Command: set mymaxtest 9223372036854775806 | Error: " + err.Error())
}
fmt.Println("Command: set mymaxtest 9223372036854775806 | Result: " + setCommandResult)
// Let's increament the vlaue of "mymaxtest"
// It reaches the max value
// Command: incr mymaxtest
// Result: (integer) 9223372036854775807
incrCommandResult, err = rdb.Incr(ctx, "mymaxtest").Result()
if err != nil {
fmt.Println("Command: incr mymaxtest | Error: " + err.Error())
}
fmt.Printf("Command: incr mymaxtest | Result: %vn", incrCommandResult)
// Let's try to increase the value of "mymaxtest"
// We get an error as it goes beyond the max value
// Command: incr mymaxtest
// Result: (error) ERR increment or decrement would overflow
incrCommandResult, err = rdb.Incr(ctx, "mymaxtest").Result()
if err != nil {
fmt.Println("Command: incr mymaxtest | Error: " + err.Error())
}
fmt.Printf("Command: incr mymaxtest | Result: %vn", incrCommandResult)
}
Output:
Command: set total-user-no 10 | Result: OK
Command: incr total-user-no | Result: 11
Command: get total-user-no | Result: 11
Command: type total-user-no | Result: string
Command: get unknownkey | Error: redis: nil
Command: get unknownkey | Result:
Command: incr unknownkey | Result: 1
Command: get unknownkey | Result: 1
Command: set sitename bigboxcode | Result: OK
Command: incr sitename | Error: ERR value is not an integer or out of range
Command: incr sitename | Result: 0
Command: set mymaxtest 9223372036854775806 | Result: OK
Command: incr mymaxtest | Result: 9223372036854775807
Command: incr mymaxtest | Error: ERR increment or decrement would overflow
Command: incr mymaxtest | Result: 0
Notes
- Use method “Incr” from package go-redis.
- Signature of the method is-
Incr(ctx context.Context, key string) *IntCmd
// Redis INCR command example in JavaScript(NodeJS)
import { createClient } from 'redis';
// Create redis client
const redisClient = createClient({
url: 'redis://default:@localhost:6379'
});
redisClient.on('error', err => console.log('Error while connecting to Redis', err));
// Connect Redis client
await redisClient.connect();
/**
* Set the value of total-user-no key to 10
*
* Command: set total-user-no 10
* Result: OK
*/
let commandResult = await redisClient.set("total-user-no", "10");
console.log("Command: set total-user-no 10 | Result: " + commandResult);
/**
* Increment value of total-user-no
*
* Command: incr total-user-no
* Result: (integer) 11
*/
commandResult = await redisClient.incr("total-user-no");
console.log("Command: incr total-user-no | Result: " + commandResult);
/**
* Check value of total-user-no key
* Command: get total-user-no
* Result: "11"
*/
commandResult = await redisClient.get("total-user-no");
console.log("Command: get total-user-no | Result: " + commandResult);
/**
* Check type of total-user-no
* Command: type total-user-no
* Result: string
*/
commandResult = await redisClient.type("total-user-no");
console.log("Command: type total-user-no | Result: " + commandResult);
/**
* Check if some key named "unknownkey" exists
* it does not exist yet
* Command: get unknownkey
* Result: (nil)
*/
commandResult = await redisClient.get("unknownkey");
console.log("Command: get unknownkey | Result: " + commandResult);
/**
* Try to increament the value of "unknownkey" using INCR command
* The value of "unknownkey" is increamented to 1
* Command: incr unknownkey
* Result: (integer) 1
*/
commandResult = await redisClient.incr("unknownkey");
console.log("Command: incr unknownkey | Result: " + commandResult);
/**
* Check the value of "unknownkey"
* Command: get unknownkey
* Result: "1"
*/
commandResult = await redisClient.get("unknownkey");
console.log("Command: get unknownkey | Result: " + commandResult);
/**
* Set a string vlaue to sitename key
* Command: set sitename bigboxcode
* Result: OK
*/
commandResult = await redisClient.set("sitename", "bigboxcode");
console.log("Command: set sitename bigboxcode | Result: " + commandResult);
/**
* Try to apply INCR command to sitename
* We get an error as the value in sitename key is not an integer
* Command: incr sitename
* Result: (error) ERR value is not an integer or out of range
*/
try {
commandResult = await redisClient.incr("sitename");
console.log("Command: incr sitename | Result: " + commandResult);
} catch (error) {
console.log("Command: incr sitename | " + error);
}
/**
* Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
* Let's set the value of key "mymaxtest" to a value close to the max value
* Command: set mymaxtest 9223372036854775806
* Result: OK
*/
commandResult = await redisClient.set("mymaxtest", "9223372036854775806");
console.log("Command: set mymaxtest 9223372036854775806 | Result: " + commandResult);
/**
* Let's increament the vlaue of "mymaxtest"
* It reaches the max value
* Command: incr mymaxtest
* Result: (integer) 9223372036854775807
*/
commandResult = await redisClient.incr("mymaxtest");
console.log("Command: incr mymaxtest | Result: " + commandResult);
/**
* Let's try to increase the value of "mymaxtest"
* We get an error as it goes beyond the max value
* Command: incr mymaxtest
* Result: (error) ERR increment or decrement would overflow
*/
try {
commandResult = await redisClient.incr("mymaxtest");
console.log("Command: incr mymaxtest | Result: " + commandResult);
} catch (error) {
console.log("Command: incr sitename | " + error);
}
process.exit(0);
Output:
Command: set total-user-no 10 | Result: OK
Command: incr total-user-no | Result: 11
Command: get total-user-no | Result: 11
Command: type total-user-no | Result: string
Command: get unknownkey | Result: null
Command: incr unknownkey | Result: 1
Command: get unknownkey | Result: 1
Command: set sitename bigboxcode | Result: OK
Command: incr sitename | Error: ERR value is not an integer or out of range
Command: set mymaxtest 9223372036854775806 | Result: OK
Command: incr mymaxtest | Result: 9223372036854776000
Command: incr sitename | Error: ERR increment or decrement would overflow
Notes
- Use method “incr” of the package node-redis.
// Redis INCR command example in Java
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class Incr {
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("localhost", 6379);
try (Jedis jedis = jedisPool.getResource()) {
/**
* Set the value of total-user-no key to 10
*
* Command: set total-user-no 10
* Result: OK
*/
String setResult = jedis.set("total-user-no", "10");
System.out.println("Command: set total-user-no 10 | Result: " + setResult);
/**
* Increment value of total-user-no
*
* Command: incr total-user-no
* Result: (integer) 11
*/
long incrResult = jedis.incr("total-user-no");
System.out.println("Command: incr total-user-no | Result: " + incrResult);
/**
* Check value of total-user-no key
* Command: get total-user-no
* Result: "11"
*/
String getResult = jedis.get("total-user-no");
System.out.println("Command: get total-user-no | Result: " + getResult);
/**
* Check type of total-user-no
* Command: type total-user-no
* Result: string
*/
String typeResult = jedis.type("total-user-no");
System.out.println("Command: type total-user-no | Result: " + typeResult);
/**
* Check if some key named "unknownkey" exists
* it does not exist yet
* Command: get unknownkey
* Result: (nil)
*/
getResult = jedis.get("unknownkey");
System.out.println("Command: get unknownkey | Result: " + getResult);
/**
* Try to increament the value of "unknownkey" using INCR command
* The value of "unknownkey" is increamented to 1
* Command: incr unknownkey
* Result: (integer) 1
*/
incrResult = jedis.incr("unknownkey");
System.out.println("Command: incr unknownkey | Result: " + incrResult);
/**
* Check the value of "unknownkey"
* Command: get unknownkey
* Result: "1"
*/
getResult = jedis.get("unknownkey");
System.out.println("Command: get unknownkey | Result: " + getResult);
/**
* Set a string vlaue to sitename key
* Command: set sitename bigboxcode
* Result: OK
*/
setResult = jedis.set("sitename", "bigboxcode");
System.out.println("Command: set sitename bigboxcode | Result: " + setResult);
/**
* Try to apply INCR command to sitename
* We get an error as the value in sitename key is not an integer
* Command: incr sitename
* Result: (error) ERR value is not an integer or out of range
*/
try {
incrResult = jedis.incr("sitename");
System.out.println("Command: incr sitename | Result: " + incrResult);
} catch (Exception e) {
System.out.println("Command: incr sitename | Error: " + e.getMessage());
}
/**
* Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
* Let's set the value of key "mymaxtest" to a value close to the max value
* Command: set mymaxtest 9223372036854775806
* Result: OK
*/
setResult = jedis.set("mymaxtest", "9223372036854775806");
System.out.println("Command: set mymaxtest 9223372036854775806 | Result: " + setResult);
/**
* Let's increament the vlaue of "mymaxtest"
* It reaches the max value
* Command: incr mymaxtest
* Result: (integer) 9223372036854775807
*/
incrResult = jedis.incr("mymaxtest");
System.out.println("Command: incr mymaxtest | Result: " + incrResult);
/**
* Let's try to increase the value of "mymaxtest"
* We get an error as it goes beyond the max value
* Command: incr mymaxtest
* Result: (error) ERR increment or decrement would overflow
*/
try {
incrResult = jedis.incr("mymaxtest");
System.out.println("Command: incr mymaxtest | Result: " + incrResult);
} catch (Exception e) {
System.out.println("Command: incr sitename | Error: " + e.getMessage());
}
}
}
}
Output:
Command: set total-user-no 10 | Result: OK
Command: incr total-user-no | Result: 11
Command: get total-user-no | Result: 11
Command: type total-user-no | Result: string
Command: get unknownkey | Result: null
Command: incr unknownkey | Result: 1
Command: get unknownkey | Result: 1
Command: set sitename bigboxcode | Result: OK
Command: incr sitename | Error: ERR value is not an integer or out of range
Command: set mymaxtest 9223372036854775806 | Result: OK
Command: incr mymaxtest | Result: 9223372036854775807
Command: incr sitename | Error: ERR increment or decrement would overflow
Notes
- Use method “incr” from Jedis package.
- Signature of the “incr” is-
public long incr(final String key)
// Redis INCR command examples in C#
using StackExchange.Redis;
namespace Incr
{
internal class Program
{
static void Main(string[] args)
{
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
IDatabase rdb = redis.GetDatabase();
/**
* Set the value of total-user-no key to 10
*
* Command: set total-user-no 10
* Result: OK
*/
bool setResult = rdb.StringSet("total-user-no", "10");
Console.WriteLine("Command: set total-user-no 10 | Result: " + setResult);
/**
* Increment value of total-user-no
*
* Command: incr total-user-no
* Result: (integer) 11
*/
long incrResult = rdb.StringIncrement("total-user-no");
Console.WriteLine("Command: incr total-user-no | Result: " + incrResult);
/**
* Check value of total-user-no key
* Command: get total-user-no
* Result: "11"
*/
RedisValue getResult = rdb.StringGet("total-user-no");
Console.WriteLine("Command: get total-user-no | Result: " + getResult);
/**
* Check type of total-user-no
* Command: type total-user-no
* Result: string
*/
RedisType typeResult = rdb.KeyType("total-user-no");
Console.WriteLine("Command: type total-user-no | Result: " + typeResult);
/**
* Check if some key named "unknownkey" exists
* it does not exist yet
* Command: get unknownkey
* Result: (nil)
*/
getResult = rdb.StringGet("unknownkey");
Console.WriteLine("Command: get unknownkey | Result: " + getResult);
/**
* Try to increament the value of "unknownkey" using INCR command
* The value of "unknownkey" is increamented to 1
* Command: incr unknownkey
* Result: (integer) 1
*/
incrResult = rdb.StringIncrement("unknownkey");
Console.WriteLine("Command: incr unknownkey | Result: " + incrResult);
/**
* Check the value of "unknownkey"
* Command: get unknownkey
* Result: "1"
*/
getResult = rdb.StringGet("unknownkey");
Console.WriteLine("Command: get unknownkey | Result: " + getResult);
/**
* Set a string vlaue to sitename key
* Command: set sitename bigboxcode
* Result: OK
*/
setResult = rdb.StringSet("sitename", "bigboxcode");
Console.WriteLine("Command: set sitename bigboxcode | Result: " + setResult);
/**
* Try to apply INCR command to sitename
* We get an error as the value in sitename key is not an integer
* Command: incr sitename
* Result: (error) ERR value is not an integer or out of range
*/
try
{
incrResult = rdb.StringIncrement("sitename");
Console.WriteLine("Command: incr sitename | Result: " + incrResult);
}
catch (Exception e)
{
Console.WriteLine("Command: incr sitename | Error: " + e.Message);
}
/**
* Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
* Let's set the value of key "mymaxtest" to a value close to the max value
* Command: set mymaxtest 9223372036854775806
* Result: OK
*/
setResult = rdb.StringSet("mymaxtest", "9223372036854775806");
Console.WriteLine("Command: set mymaxtest 9223372036854775806 | Result: " + setResult);
/**
* Let's increament the vlaue of "mymaxtest"
* It reaches the max value
* Command: incr mymaxtest
* Result: (integer) 9223372036854775807
*/
incrResult = rdb.StringIncrement("mymaxtest");
Console.WriteLine("Command: incr mymaxtest | Result: " + incrResult);
/**
* Let's try to increase the value of "mymaxtest"
* We get an error as it goes beyond the max value
* Command: incr mymaxtest
* Result: (error) ERR increment or decrement would overflow
*/
try
{
incrResult = rdb.StringIncrement("mymaxtest");
Console.WriteLine("Command: incr mymaxtest | Result: " + incrResult);
}
catch (Exception e)
{
Console.WriteLine("Command: incr sitename | Error: " + e.Message);
}
}
}
}
Output:
Command: set total-user-no 10 | Result: True
Command: incr total-user-no | Result: 11
Command: get total-user-no | Result: 11
Command: type total-user-no | Result: String
Command: get unknownkey | Result:
Command: incr unknownkey | Result: 1
Command: get unknownkey | Result: 1
Command: set sitename bigboxcode | Result: True
Command: incr sitename | Error: ERR value is not an integer or out of range
Command: set mymaxtest 9223372036854775806 | Result: True
Command: incr mymaxtest | Result: 9223372036854775807
Command: incr sitename | Error: ERR increment or decrement would overflow
Notes
- Use the method “StringIncreament” from StackExchange.Redis.
- Signature of the method is-
long StringIncrement(RedisKey key, long value = 1, CommandFlags flags = CommandFlags.None)
<?php
// Redis INCR command example in PHP
require 'vendor/autoload.php';
// Connect to Redis
$redisClient = new PredisClient([
'scheme' => 'tcp',
'host' => 'localhost',
'port' => 6379,
]);
/**
* Set the value of total-user-no key to 10
*
* Command: set total-user-no 10
* Result: OK
*/
$commandResult = $redisClient->set("total-user-no", "10");
echo "Command: set total-user-no 10 | Result: " . $commandResult . "n";
/**
* Increment value of total-user-no
*
* Command: incr total-user-no
* Result: (integer) 11
*/
$commandResult = $redisClient->incr("total-user-no");
echo "Command: incr total-user-no | Result: " . $commandResult . "n";
/**
* Check value of total-user-no key
* Command: get total-user-no
* Result: "11"
*/
$commandResult = $redisClient->get("total-user-no");
echo "Command: get total-user-no | Result: " . $commandResult . "n";
/**
* Check type of total-user-no
* Command: type total-user-no
* Result: string
*/
$commandResult = $redisClient->type("total-user-no");
echo "Command: type total-user-no | Result: " . $commandResult . "n";
/**
* Check if some key named "unknownkey" exists
* it does not exist yet
* Command: get unknownkey
* Result: (nil)
*/
$commandResult = $redisClient->get("unknownkey");
echo "Command: get unknownkey | Result: " . $commandResult . "n";
/**
* Try to increament the value of "unknownkey" using INCR command
* The value of "unknownkey" is increamented to 1
* Command: incr unknownkey
* Result: (integer) 1
*/
$commandResult = $redisClient->incr("unknownkey");
echo "Command: incr unknownkey | Result: " . $commandResult . "n";
/**
* Check the value of "unknownkey"
* Command: get unknownkey
* Result: "1"
*/
$commandResult = $redisClient->get("unknownkey");
echo "Command: get unknownkey | Result: " . $commandResult . "n";
/**
* Set a string vlaue to sitename key
* Command: set sitename bigboxcode
* Result: OK
*/
$commandResult = $redisClient->set("sitename", "bigboxcode");
echo "Command: set sitename bigboxcode | Result: " . $commandResult . "n";
/**
* Try to apply INCR command to sitename
* We get an error as the value in sitename key is not an integer
* Command: incr sitename
* Result: (error) ERR value is not an integer or out of range
*/
try {
$commandResult = $redisClient->incr("sitename");
echo "Command: incr sitename | Result: " . $commandResult . "n";
} catch (Exception $e) {
echo "Command: incr sitename | Error: " . $e->getMessage() . "n";
}
/**
* Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
* Let's set the value of key "mymaxtest" to a value close to the max value
* Command: set mymaxtest 9223372036854775806
* Result: OK
*/
$commandResult = $redisClient->set("mymaxtest", "9223372036854775806");
echo "Command: set mymaxtest 9223372036854775806 | Result: " . $commandResult . "n";
/**
* Let's increament the vlaue of "mymaxtest"
* It reaches the max value
* Command: incr mymaxtest
* Result: (integer) 9223372036854775807
*/
$commandResult = $redisClient->incr("mymaxtest");
echo "Command: incr mymaxtest | Result: " . $commandResult . "n";
/**
* Let's try to increase the value of "mymaxtest"
* We get an error as it goes beyond the max value
* Command: incr mymaxtest
* Result: (error) ERR increment or decrement would overflow
*/
try {
$commandResult = $redisClient->incr("mymaxtest");
echo "Command: incr mymaxtest | Result: " . $commandResult . "n";
} catch (Exception $e) {
echo "Command: incr sitename | Error: " . $e->getMessage() . "n";
}
Output:
Command: set total-user-no 10 | Result: OK
Command: incr total-user-no | Result: 11
Command: get total-user-no | Result: 11
Command: type total-user-no | Result: string
Command: get unknownkey | Result:
Command: incr unknownkey | Result: 1
Command: get unknownkey | Result: 1
Command: set sitename bigboxcode | Result: OK
Command: incr sitename | Error: ERR value is not an integer or out of range
Command: set mymaxtest 9223372036854775806 | Result: OK
Command: incr mymaxtest | Result: 9223372036854775807
Command: incr sitename | Error: ERR increment or decrement would overflow
Notes
- Use the method “incr” of predis.
- Signature of the method is-
incr(string $key): int
# Redis INCR command example in Python
import redis
import time
# Create Redis client
redisClient = redis.Redis(host='localhost', port=6379,
username='default', password='',
decode_responses=True)
# Set the value of total-user-no key to 10
# Command: set total-user-no 10
# Result: OK
commandResult = redisClient.set("total-user-no", "10")
print("Command: set total-user-no 10 | Result: {}".format(commandResult))
# Increment value of total-user-no
# Command: incr total-user-no
# Result: (integer) 11
commandResult = redisClient.incr("total-user-no")
print("Command: incr total-user-no | Result: {}".format(commandResult))
# Check value of total-user-no key
# Command: get total-user-no
# Result: "11"
commandResult = redisClient.get("total-user-no")
print("Command: get total-user-no | Result: {}".format(commandResult))
# Check type of total-user-no
# Command: type total-user-no
# Result: string
typeResult = redisClient.type("total-user-no")
print("Command: type total-user-no | Result: {}".format(typeResult))
# Check if some key named "unknownkey" exists
# it does not exist yet
# Command: get unknownkey
# Result: (nil)
commandResult = redisClient.get("unknownkey")
print("Command: get unknownkey | Result: {}".format(commandResult))
# Try to increament the value of "unknownkey" using INCR command
# The value of "unknownkey" is increamented to 1
# Command: incr unknownkey
# Result: (integer) 1
commandResult = redisClient.incr("unknownkey")
print("Command: incr unknownkey | Result: {}".format(commandResult))
# Check the value of "unknownkey"
# Command: get unknownkey
# Result: "1"
commandResult = redisClient.get("unknownkey")
print("Command: get unknownkey | Result: {}".format(commandResult))
# Set a string vlaue to sitename key
# Command: set sitename bigboxcode
# Result: OK
commandResult = redisClient.set("sitename", "bigboxcode")
print("Command: set sitename bigboxcode | Result: {}".format(commandResult))
# Try to apply INCR command to sitename
# We get an error as the value in sitename key is not an integer
# Command: incr sitename
# Result: (error) ERR value is not an integer or out of range
try:
commandResult = redisClient.incr("sitename")
print("Command: incr sitename | Result: {}".format(commandResult))
except Exception as error:
print("Command: incr sitename | Error: ", error)
# Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
# Let's set the value of key "mymaxtest" to a value close to the max value
# Command: set mymaxtest 9223372036854775806
# Result: OK
commandResult = redisClient.set("mymaxtest", "9223372036854775806")
print("Command: set mymaxtest 9223372036854775806 | Result: {}".format(commandResult))
# Let's increament the vlaue of "mymaxtest"
# It reaches the max value
# Command: incr mymaxtest
# Result: (integer) 9223372036854775807
commandResult = redisClient.incr("mymaxtest")
print("Command: incr mymaxtest | Result: {}".format(commandResult))
# Let's try to increase the value of "mymaxtest"
# We get an error as it goes beyond the max value
# Command: incr mymaxtest
# Result: (error) ERR increment or decrement would overflow
try:
commandResult = redisClient.incr("mymaxtest")
print("Command: incr mymaxtest | Result: {}".format(commandResult))
except Exception as error:
print("Command: incr sitename | Error: ", error)
Output:
Command: set total-user-no 10 | Result: True
Command: incr total-user-no | Result: 11
Command: get total-user-no | Result: 11
Command: type total-user-no | Result: string
Command: get unknownkey | Result: None
Command: incr unknownkey | Result: 1
Command: get unknownkey | Result: 1
Command: set sitename bigboxcode | Result: True
Command: incr sitename | Error: value is not an integer or out of range
Command: set mymaxtest 9223372036854775806 | Result: True
Command: incr mymaxtest | Result: 9223372036854775807
Command: incr sitename | Error: increment or decrement would overflow
Notes
- Use method “incr” from redis-py, for the Redis INCR command usage.
- In the redis-py package incr and incrby has the same definition. incr is defined as-
incr = incrby
Here is the definition of incrby method-
def incrby(self, name: KeyT, amount: int = 1) -> ResponseT
if we just ignore passing the last param “amount” then we will active the behavior of “incr”.
# Redis INCR command example in Ruby
require 'redis'
redis = Redis.new(host: "localhost", port: 6379)
# Set the value of total-user-no key to 10
# Command: set total-user-no 10
# Result: OK
commandResult = redis.set("total-user-no", "10")
print("Command: set total-user-no 10 | Result: ", commandResult, "n")
# Increment value of total-user-no
# Command: incr total-user-no
# Result: (integer) 11
commandResult = redis.incr("total-user-no")
print("Command: incr total-user-no | Result: ", commandResult, "n")
# Check value of total-user-no key
# Command: get total-user-no
# Result: "11"
commandResult = redis.get("total-user-no")
print("Command: get total-user-no | Result: ", commandResult, "n")
# Check type of total-user-no
# Command: type total-user-no
# Result: string
commandResult = redis.type("total-user-no")
print("Command: type total-user-no | Result: ", commandResult, "n")
# Check if some key named "unknownkey" exists
# it does not exist yet
# Command: get unknownkey
# Result: (nil)
commandResult = redis.get("unknownkey")
print("Command: get unknownkey | Result: ", commandResult, "n")
# Try to increament the value of "unknownkey" using INCR command
# The value of "unknownkey" is increamented to 1
# Command: incr unknownkey
# Result: (integer) 1
commandResult = redis.incr("unknownkey")
print("Command: incr unknownkey | Result: ", commandResult, "n")
# Check the value of "unknownkey"
# Command: get unknownkey
# Result: "1"
commandResult = redis.get("unknownkey")
print("Command: get unknownkey | Result: ", commandResult, "n")
# Set a string vlaue to sitename key
# Command: set sitename bigboxcode
# Result: OK
commandResult = redis.set("sitename", "bigboxcode")
print("Command: set sitename bigboxcode | Result: ", commandResult, "n")
# Try to apply INCR command to sitename
# We get an error as the value in sitename key is not an integer
# Command: incr sitename
# Result: (error) ERR value is not an integer or out of range
begin
commandResult = redis.incr("sitename")
print("Command: incr sitename | Result: ", commandResult, "n")
rescue => e
print("Command: incr sitename | Error: ", e, "n")
end
# Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
# Let's set the value of key "mymaxtest" to a value close to the max value
# Command: set mymaxtest 9223372036854775806
# Result: OK
commandResult = redis.set("mymaxtest", "9223372036854775806")
print("Command: set mymaxtest 9223372036854775806 | Result: ", commandResult, "n")
# Let's increament the vlaue of "mymaxtest"
# It reaches the max value
# Command: incr mymaxtest
# Result: (integer) 9223372036854775807
commandResult = redis.incr("mymaxtest")
print("Command: incr mymaxtest | Result: ", commandResult, "n")
# Let's try to increase the value of "mymaxtest"
# We get an error as it goes beyond the max value
# Command: incr mymaxtest
# Result: (error) ERR increment or decrement would overflow
begin
commandResult = redis.incr("mymaxtest")
print("Command: incr mymaxtest | Result: ", commandResult, "n")
rescue => e
print("Command: incr sitename | Error: ", e, "n")
end
Output:
Command: set total-user-no 10 | Result: OK
Command: incr total-user-no | Result: 11
Command: get total-user-no | Result: 11
Command: type total-user-no | Result: string
Command: get unknownkey | Result:
Command: incr unknownkey | Result: 1
Command: get unknownkey | Result: 1
Command: set sitename bigboxcode | Result: OK
Command: incr sitename | Error: ERR value is not an integer or out of range
Command: set mymaxtest 9223372036854775806 | Result: OK
Command: incr mymaxtest | Result: 9223372036854775807
Command: incr sitename | Error: ERR increment or decrement would overflow
Notes
- Use method “incr” from the redis-rb.
- Signature of the “incr” method is-
def incr(key)
Source Code
Use the following links to get the source code used in this article-
Source Code of | Source Code Link |
---|---|
Command Examples | GitHub |
Golang Implementation | GitHub |
NodeJS Implementation | GitHub |
Java Implementation | GitHub |
C# Implementation | GitHub |
PHP Implementation | GitHub |
Python Implementation | GitHub |
Ruby Implementation | GitHub |
Related Commands
Command | Details |
---|---|
INCRBY | Command Details |
INCRBYFLOAT | Command Details |
DECR | Command Details |
DECRBY | Command Details |