Summary
Command Name | HINCRBY |
Usage | Increment hash field integer value |
Group | hash |
ACL Category | @write @hash @fast |
Time Complexity | O(1) |
Flag | WRITE DENYOOM FAST |
Arity | 4 |
Signature
HINCRBY <key> <field> <increment>
Usage
Increment the integer value of a hash field. This command increments the field value by a certain amount that is provided.
Notes
- If the hash(the provided <key>) does not exist, then the hash is created. Then the field is created and the value is set to Zero(0). Then the increment operation is performed.
- If the hash exists but the field does not exist, then the field is created and set to Zero(0). The increment operation is performed after that.
- The field’s max and min allowed value is of a 64-bit signed integer.
Arguments
Parameter | Description | Name | Type |
---|---|---|---|
<key> | Name of the key of the hash | key | key |
<field> | Name of the field | field | string |
<increment> | Increment (+ve) or decrement(-ve) by this amount. Both positive and negative values are allowed. | increment | integer |
Notes
- Both increment and decrement operations can be performed by providing positive(+ve) or negative(-ve) value for the <increment> argument.
Return Value
Return value | Case for the return value | Type |
---|---|---|
Value after increment | On successful this command returns the value of the field after incrementing | integer |
error | If applied to wrong data type, or the value is out of the 64-bit signed integer range | error |
Notes
- If the command is applied to a key that is not a hash, then the following error is returned-
(error) WRONGTYPE Operation against a key holding the wrong kind of value
- If the value is out of the range 64-bit integer then we get the following error-
(error) ERR increment or decrement would overflow
Examples
Here are a few examples of the usage examples of the Redis HINCRBY command-
# Redis HINCRBY command examples
# Set hash fields
127.0.0.1:6379> hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1
(integer) 4
# Check hash fields
127.0.0.1:6379> hgetall customer:100
1) "name"
2) "Kenneth Braun"
3) "gender"
4) "male"
5) "age"
6) "42"
7) "order_count"
8) "1"
# Increament order_count field by 2
127.0.0.1:6379> hincrby customer:100 order_count 2
(integer) 3
# Check the order_count field
127.0.0.1:6379> hget customer:100 order_count
"3"
# bigboxhash does not exist
Check field of a non existing hash
127.0.0.1:6379> hget bigboxhash firstfield
(nil)
# Try to apply HINCRBY on a hash that does not exist
127.0.0.1:6379> hincrby bigboxhash firstfield 100
(integer) 100
# Increament firstfield of bigboxhash
# We see the increased value
127.0.0.1:6379> hget bigboxhash firstfield
"100"
# Check a non existing field, of a hash that exists
127.0.0.1:6379> hget bigboxhash secondfield
(nil)
# Implement HINCRBY on a non existing field
127.0.0.1:6379> hincrby bigboxhash secondfield 5
(integer) 5
# Check the secondfield
127.0.0.1:6379> hget bigboxhash secondfield
"5"
# Use a negative value with HINCRBY
# That will decrease the existing value
127.0.0.1:6379> hincrby bigboxhash secondfield -3
(integer) 2
# Check secondfield value
127.0.0.1:6379> hget bigboxhash secondfield
"2"
# Decreament of the hash field by -5
127.0.0.1:6379> hincrby bigboxhash secondfield -5
(integer) -3
# Check the secondfield value
127.0.0.1:6379> hget bigboxhash secondfield
"-3"
# Set a string key
127.0.0.1:6379> set bigboxstr "some str value here"
OK
# Try to use HINCRBY on the string
# We get an error as command is applied to a wrong data type
127.0.0.1:6379> hincrby bigboxstr field1 10
(error) WRONGTYPE Operation against a key holding the wrong kind of value
# Check cutsomer name
127.0.0.1:6379> hget customer:100 name
"Kenneth Braun"
# Try to apply HINCRBY on the name field
# We get an error, as the field has string value
127.0.0.1:6379> hincrby customer:100 name 10
(error) ERR hash value is not an integer
# Set a filed of a hash to a larg integer value
127.0.0.1:6379> hset bigboxhash max_test_field 9223372036854775806
(integer) 1
# Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
# So if we try to increment max_test_field by 10 then it excedes the max integer limit
# We get an error related to max value overflow
127.0.0.1:6379> hincrby bigboxhash max_test_field 10
(error) ERR increment or decrement would overflow
# Set field value of a has to large negative nubmer
127.0.0.1:6379> hset bigboxhash max_test_field -9223372036854775709
(integer) 0
# Check the value, we se the negative value is set
# as it is withing the limit of 64-bit signed integer
127.0.0.1:6379> hget bigboxhash max_test_field
"-9223372036854775709"
# Min value allowed as 64-bit int is -9,223,372,036,854,775,808
# Try to decrease the value by 10
# We get an error as the target value goes beyond the minimum integer value
127.0.0.1:6379> hincrby bigboxhash max_test_field -100
(error) ERR increment or decrement would overflow
Notes
- If the hash does not exist, or the field does not exist, then the hash and/or the field will be created. So we will never get any error related to non-existing hash key or field.
Code Implementations
Redis HINCRBY command example implementation in Golang, NodeJS, Java, C#, PHP, Python, Ruby-
// Redis HINCRBY 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 hash fields
*
* Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1
* Result: (integer) 4
*/
hsetResult, err := rdb.HSet(ctx, "customer:100",
"name", "Kenneth Braun",
"gender", "male",
"age", "42",
"order_count", "1",
).Result()
if err != nil {
fmt.Println("Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1 | Error: " + err.Error())
}
fmt.Println("Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1 | Result: ", hsetResult)
/**
* Check hash fields
*
* Command: hgetall customer:100
* Result:
* 1) "name"
* 2) "Kenneth Braun"
* 3) "gender"
* 4) "male"
* 5) "age"
* 6) "42"
* 7) "order_count"
* 8) "1"
*/
hgetAllResult, err := rdb.HGetAll(ctx, "customer:100").Result()
if err != nil {
fmt.Println("Command: hgetall customer:100 | Error: " + err.Error())
}
fmt.Println("Command: hgetall customer:100 | Result: ", hgetAllResult)
/**
* Increament order_count field by 2
*
* Command: hincrby customer:100 order_count 2
* Result: (integer) 3
*/
hincrbyResult, err := rdb.HIncrBy(ctx, "customer:100", "order_count", 2).Result()
if err != nil {
fmt.Println("Command: hincrby customer:100 order_count 2 | Error: " + err.Error())
}
fmt.Println("Command: hincrby customer:100 order_count 2 | Result: ", hincrbyResult)
/**
* Check the order_count field
*
* Command: hget customer:100 order_count
* Result: "3"
*/
hgetResult, err := rdb.HGet(ctx, "customer:100", "order_count").Result()
if err != nil {
fmt.Println("Command: hget customer:100 order_count | Error: " + err.Error())
}
fmt.Println("Command: hget customer:100 order_count | Result: " + hgetResult)
/**
* bigboxhash does not exist
Check field of a non existing hash
*
* Command: hget bigboxhash firstfield
* Result: (nil)
*/
hgetResult, err = rdb.HGet(ctx, "bigboxhash", "firstfield").Result()
if err != nil {
fmt.Println("Command: hget bigboxhash firstfield | Error: " + err.Error())
}
fmt.Println("Command: hget bigboxhash firstfield | Result: " + hgetResult)
/**
* Try to apply HINCRBY on a hash that does not exist
*
* Command: hincrby bigboxhash firstfield 100
* Result: (integer) 100
*/
hincrbyResult, err = rdb.HIncrBy(ctx, "bigboxhash", "firstfield", 100).Result()
if err != nil {
fmt.Println("Command: hincrby bigboxhash firstfield 100 | Error: " + err.Error())
}
fmt.Println("Command: hincrby bigboxhash firstfield 100 | Result: ", hincrbyResult)
/**
* Increament firstfield of bigboxhash
* We see the increased value
*
* Command: hget bigboxhash firstfield
* Result: "100"
*/
hgetResult, err = rdb.HGet(ctx, "bigboxhash", "firstfield").Result()
if err != nil {
fmt.Println("Command: hget bigboxhash firstfield | Error: " + err.Error())
}
fmt.Println("Command: hget bigboxhash firstfield | Result: " + hgetResult)
/**
* Check a non existing field, of a hash that exists
*
* Command: hget bigboxhash secondfield
* Result: (nil)
*/
hgetResult, err = rdb.HGet(ctx, "bigboxhash", "secondfield").Result()
if err != nil {
fmt.Println("Command: hget bigboxhash secondfield | Error: " + err.Error())
}
fmt.Println("Command: hget bigboxhash secondfield | Result: " + hgetResult)
/**
* Implement HINCRBY on a non existing field
*
* Command: hincrby bigboxhash secondfield 5
* Result: (integer) 5
*/
hincrbyResult, err = rdb.HIncrBy(ctx, "bigboxhash", "secondfield", 5).Result()
if err != nil {
fmt.Println("Command: hincrby bigboxhash secondfield 5 | Error: " + err.Error())
}
fmt.Println("Command: hincrby bigboxhash secondfield 5 | Result: ", hincrbyResult)
/**
* Check the secondfield
*
* Command: hget bigboxhash secondfield
* Result: "5"
*/
hgetResult, err = rdb.HGet(ctx, "bigboxhash", "secondfield").Result()
if err != nil {
fmt.Println("Command: hget bigboxhash secondfield | Error: " + err.Error())
}
fmt.Println("Command: hget bigboxhash secondfield | Result: " + hgetResult)
/**
* Use a negative value with HINCRBY
* That will decrease the existing value
*
* Command: hincrby bigboxhash secondfield -3
* Result: (integer) 2
*/
hincrbyResult, err = rdb.HIncrBy(ctx, "bigboxhash", "secondfield", -3).Result()
if err != nil {
fmt.Println("Command: hincrby bigboxhash secondfield -3 | Error: " + err.Error())
}
fmt.Println("Command: hincrby bigboxhash secondfield -3 | Result: ", hincrbyResult)
/**
* Check secondfield value
*
* Command: hget bigboxhash secondfield
* Result: "2"
*/
hgetResult, err = rdb.HGet(ctx, "bigboxhash", "secondfield").Result()
if err != nil {
fmt.Println("Command: hget bigboxhash secondfield | Error: " + err.Error())
}
fmt.Println("Command: hget bigboxhash secondfield | Result: " + hgetResult)
/**
* Decreament of the hash field by -5
*
* Command: hincrby bigboxhash secondfield -5
* Result: (integer) -3
*/
hincrbyResult, err = rdb.HIncrBy(ctx, "bigboxhash", "secondfield", -5).Result()
if err != nil {
fmt.Println("Command: hincrby bigboxhash secondfield -5 | Error: " + err.Error())
}
fmt.Println("Command: hincrby bigboxhash secondfield -5 | Result: ", hincrbyResult)
/**
* Check the secondfield value
*
* Command: hget bigboxhash secondfield
* Result: "-3"
*/
hgetResult, err = rdb.HGet(ctx, "bigboxhash", "secondfield").Result()
if err != nil {
fmt.Println("Command: hget bigboxhash secondfield | Error: " + err.Error())
}
fmt.Println("Command: hget bigboxhash secondfield | Result: " + hgetResult)
/**
* Set a string key
*
* Command: set bigboxstr "some str value here"
* Result: OK
*/
setResult, err := rdb.Set(ctx, "bigboxstr", "some str value here", 0).Result()
if err != nil {
fmt.Println("Command: set bigboxstr "some str value here" | Error: " + err.Error())
}
fmt.Println("Command: set bigboxstr "some str value here" | Result: " + setResult)
/**
* Try to use HINCRBY on the string
* We get an error as command is applied to a wrong data type
*
* Command: hincrby bigboxstr field1 10
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
hincrbyResult, err = rdb.HIncrBy(ctx, "bigboxstr", "field1", 10).Result()
if err != nil {
fmt.Println("Command: hincrby bigboxstr field1 10 | Error: " + err.Error())
}
fmt.Println("Command: hincrby bigboxstr field1 10 | Result: ", hincrbyResult)
/**
* Check cutsomer name
*
* Command: hget customer:100 name
* Result: "Kenneth Braun"
*/
hgetResult, err = rdb.HGet(ctx, "customer:100", "name").Result()
if err != nil {
fmt.Println("Command: hget customer:100 name | Error: " + err.Error())
}
fmt.Println("Command: hget customer:100 name | Result: " + hgetResult)
/**
* Try to apply HINCRBY on the name field
* We get an error, as the field has string value
*
* Command: hincrby customer:100 name 10
* Result: (error) ERR hash value is not an integer
*/
hincrbyResult, err = rdb.HIncrBy(ctx, "customer:100", "name", 10).Result()
if err != nil {
fmt.Println("Command: hincrby customer:100 name 10 | Error: " + err.Error())
}
fmt.Println("Command: hincrby customer:100 name 10 | Result: ", hincrbyResult)
/**
* Set a filed of a hash to a larg integer value
*
* Command: hset bigboxhash max_test_field 9223372036854775806
* Result: (integer) 1
*/
hsetResult, err = rdb.HSet(ctx, "bigboxhash", "max_test_field", "9223372036854775806").Result()
if err != nil {
fmt.Println("Command: hset bigboxhash max_test_field 9223372036854775806 | Error: " + err.Error())
}
fmt.Println("Command: hset bigboxhash max_test_field 9223372036854775806 | Result: ", hsetResult)
/**
* Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
* So if we try to increment max_test_field by 10 then it excedes the max integer limit
* We get an error related to max value overflow
*
* Command: hincrby bigboxhash max_test_field 10
* Result: (error) ERR increment or decrement would overflow
*/
hincrbyResult, err = rdb.HIncrBy(ctx, "bigboxhash", "max_test_field", 10).Result()
if err != nil {
fmt.Println("Command: hincrby bigboxhash max_test_field 10 | Error: " + err.Error())
}
fmt.Println("Command: hincrby bigboxhash max_test_field 10 | Result: ", hincrbyResult)
/**
* Set field value of a has to large negative nubmer
*
* Command: hset bigboxhash max_test_field -9223372036854775709
* Result: (integer) 0
*/
hsetResult, err = rdb.HSet(ctx, "bigboxhash", "max_test_field", "-9223372036854775709").Result()
if err != nil {
fmt.Println("Command: hset bigboxhash max_test_field -9223372036854775709 | Error: " + err.Error())
}
fmt.Println("Command: hset bigboxhash max_test_field -9223372036854775709 | Result: ", hsetResult)
/**
* Check the value, we se the negative value is set
* as it is withing the limit of 64-bit signed integer
*
* Command: hget bigboxhash max_test_field
* Result: "-9223372036854775709"
*/
hgetResult, err = rdb.HGet(ctx, "bigboxhash", "max_test_field").Result()
if err != nil {
fmt.Println("Command: hget bigboxhash max_test_field | Error: " + err.Error())
}
fmt.Println("Command: hget bigboxhash max_test_field | Result: " + hgetResult)
/**
* Min value allowed as 64-bit int is -9,223,372,036,854,775,808
* Try to decrease the value by 10
* We get an error as the target value goes beyond the minimum integer value
*
* Command: hincrby bigboxhash max_test_field -100
* Result: (error) ERR increment or decrement would overflow
*/
hincrbyResult, err = rdb.HIncrBy(ctx, "bigboxhash", "max_test_field", -100).Result()
if err != nil {
fmt.Println("Command: hincrby bigboxhash max_test_field -100 | Error: " + err.Error())
}
fmt.Println("Command: hincrby bigboxhash max_test_field -100 | Result: ", hincrbyResult)
}
Output:
Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1 | Result: 4
Command: hgetall customer:100 | Result: map[age:42 gender:male name:Kenneth Braun order_count:1]
Command: hincrby customer:100 order_count 2 | Result: 3
Command: hget customer:100 order_count | Result: 3
Command: hget bigboxhash firstfield | Error: redis: nil
Command: hget bigboxhash firstfield | Result:
Command: hincrby bigboxhash firstfield 100 | Result: 100
Command: hget bigboxhash firstfield | Result: 100
Command: hget bigboxhash secondfield | Error: redis: nil
Command: hget bigboxhash secondfield | Result:
Command: hincrby bigboxhash secondfield 5 | Result: 5
Command: hget bigboxhash secondfield | Result: 5
Command: hincrby bigboxhash secondfield -3 | Result: 2
Command: hget bigboxhash secondfield | Result: 2
Command: hincrby bigboxhash secondfield -5 | Result: -3
Command: hget bigboxhash secondfield | Result: -3
Command: set bigboxstr "some str value here" | Result: OK
Command: hincrby bigboxstr field1 10 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: hincrby bigboxstr field1 10 | Result: 0
Command: hget customer:100 name | Result: Kenneth Braun
Command: hincrby customer:100 name 10 | Error: ERR hash value is not an integer
Command: hincrby customer:100 name 10 | Result: 0
Command: hset bigboxhash max_test_field 9223372036854775806 | Result: 1
Command: hincrby bigboxhash max_test_field 10 | Error: ERR increment or decrement would overflow
Command: hincrby bigboxhash max_test_field 10 | Result: 0
Command: hset bigboxhash max_test_field -9223372036854775709 | Result: 0
Command: hget bigboxhash max_test_field | Result: -9223372036854775709
Command: hincrby bigboxhash max_test_field -100 | Error: ERR increment or decrement would overflow
Command: hincrby bigboxhash max_test_field -100 | Result: 0
Notes
- Use “
HIncrBy
” method from redis-go module. - Signature of the method is-
HIncrBy(ctx context.Context, key, field string, incr int64) *IntCmd
// Redis HINCRBY 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 hash fields
*
* Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1
* Result: (integer) 4
*/
let commandResult = await redisClient.hSet("customer:100", {
name: "Kenneth Braun",
gender: "male",
age: "42",
order_count: "1",
});
console.log(
'Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1 | Result: ' +
commandResult
);
/**
* Check hash fields
*
* Command: hgetall customer:100
* Result:
* 1) "name"
* 2) "Kenneth Braun"
* 3) "gender"
* 4) "male"
* 5) "age"
* 6) "42"
* 7) "order_count"
* 8) "1"
*/
commandResult = await redisClient.hGetAll("customer:100");
console.log("Command: hgetall customer:100 | Result: ", commandResult);
/**
* Increament order_count field by 2
*
* Command: hincrby customer:100 order_count 2
* Result: (integer) 3
*/
commandResult = await redisClient.hIncrBy("customer:100", "order_count", 2);
console.log(
"Command: hincrby customer:100 order_count 2 | Result: " + commandResult
);
/**
* Check the order_count field
*
* Command: hget customer:100 order_count
* Result: "3"
*/
commandResult = await redisClient.hGet("customer:100", "order_count");
console.log(
"Command: hget customer:100 order_count | Result: " + commandResult
);
/**
* bigboxhash does not exist
Check field of a non existing hash
*
* Command: hget bigboxhash firstfield
* Result: (nil)
*/
commandResult = await redisClient.hGet("bigboxhash", "firstfield");
console.log("Command: hget bigboxhash firstfield | Result: " + commandResult);
/**
* Try to apply HINCRBY on a hash that does not exist
*
* Command: hincrby bigboxhash firstfield 100
* Result: (integer) 100
*/
commandResult = await redisClient.hIncrBy("bigboxhash", "firstfield", 100);
console.log(
"Command: hincrby bigboxhash firstfield 100 | Result: " + commandResult
);
/**
* Increament firstfield of bigboxhash
* We see the increased value
*
* Command: hget bigboxhash firstfield
* Result: "100"
*/
commandResult = await redisClient.hGet("bigboxhash", "firstfield");
console.log("Command: hget bigboxhash firstfield | Result: " + commandResult);
/**
* Check a non existing field, of a hash that exists
*
* Command: hget bigboxhash secondfield
* Result: (nil)
*/
commandResult = await redisClient.hGet("bigboxhash", "secondfield");
console.log("Command: hget bigboxhash secondfield | Result: " + commandResult);
/**
* Implement HINCRBY on a non existing field
*
* Command: hincrby bigboxhash secondfield 5
* Result: (integer) 5
*/
commandResult = await redisClient.hIncrBy("bigboxhash", "secondfield", 5);
console.log(
"Command: hincrby bigboxhash secondfield 5 | Result: " + commandResult
);
/**
* Check the secondfield
*
* Command: hget bigboxhash secondfield
* Result: "5"
*/
commandResult = await redisClient.hGet("bigboxhash", "secondfield");
console.log("Command: hget bigboxhash secondfield | Result: " + commandResult);
/**
* Use a negative value with HINCRBY
* That will decrease the existing value
*
* Command: hincrby bigboxhash secondfield -3
* Result: (integer) 2
*/
commandResult = await redisClient.hIncrBy("bigboxhash", "secondfield", -3);
console.log(
"Command: hincrby bigboxhash secondfield -3 | Result: " + commandResult
);
/**
* Check secondfield value
*
* Command: hget bigboxhash secondfield
* Result: "2"
*/
commandResult = await redisClient.hGet("bigboxhash", "secondfield");
console.log("Command: hget bigboxhash secondfield | Result: " + commandResult);
/**
* Decreament of the hash field by -5
*
* Command: hincrby bigboxhash secondfield -5
* Result: (integer) -3
*/
commandResult = await redisClient.hIncrBy("bigboxhash", "secondfield", -5);
console.log(
"Command: hincrby bigboxhash secondfield -5 | Result: " + commandResult
);
/**
* Check the secondfield value
*
* Command: hget bigboxhash secondfield
* Result: "-3"
*/
commandResult = await redisClient.hGet("bigboxhash", "secondfield");
console.log("Command: hget bigboxhash secondfield | Result: " + commandResult);
/**
* Set a string key
*
* Command: set bigboxstr "some str value here"
* Result: OK
*/
commandResult = await redisClient.set("bigboxstr", "some str value here");
console.log(
'Command: set bigboxstr "some str value here" | Result: ' + commandResult
);
/**
* Try to use HINCRBY on the string
* We get an error as command is applied to a wrong data type
*
* Command: hincrby bigboxstr field1 10
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
commandResult = await redisClient.hIncrBy("bigboxstr", "field1", 10);
console.log(
"Command: hincrby bigboxstr field1 10 | Result: " + commandResult
);
} catch (err) {
console.log("Command: hincrby bigboxstr field1 10 | Error: ", err);
}
/**
* Check cutsomer name
*
* Command: hget customer:100 name
* Result: "Kenneth Braun"
*/
commandResult = await redisClient.hGet("customer:100", "name");
console.log("Command: hget customer:100 name | Result: " + commandResult);
/**
* Try to apply HINCRBY on the name field
* We get an error, as the field has string value
*
* Command: hincrby customer:100 name 10
* Result: (error) ERR hash value is not an integer
*/
try {
commandResult = await redisClient.hIncrBy("customer:100", "name", 10);
console.log(
"Command: hincrby customer:100 name 10 | Result: " + commandResult
);
} catch (err) {
console.log("Command: hincrby customer:100 name 10 | Error: ", err);
}
/**
* Set a filed of a hash to a larg integer value
*
* Command: hset bigboxhash max_test_field 9223372036854775806
* Result: (integer) 1
*/
commandResult = await redisClient.hSet(
"bigboxhash",
"max_test_field",
"9223372036854775806"
);
console.log(
"Command: hset bigboxhash max_test_field 9223372036854775806 | Result: " +
commandResult
);
/**
* Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
* So if we try to increment max_test_field by 10 then it excedes the max integer limit
* We get an error related to max value overflow
*
* Command: hincrby bigboxhash max_test_field 10
* Result: (error) ERR increment or decrement would overflow
*/
try {
commandResult = await redisClient.hIncrBy("bigboxhash", "max_test_field", 10);
console.log(
"Command: hincrby bigboxhash max_test_field 10 | Result: " + commandResult
);
} catch (err) {
console.log("Command: hincrby bigboxhash max_test_field 10 | Error: ", err);
}
/**
* Set field value of a has to large negative nubmer
*
* Command: hset bigboxhash max_test_field -9223372036854775709
* Result: (integer) 0
*/
commandResult = await redisClient.hSet(
"bigboxhash",
"max_test_field",
"-9223372036854775709"
);
console.log(
"Command: hset bigboxhash max_test_field -9223372036854775709 | Result: " +
commandResult
);
/**
* Check the value, we se the negative value is set
* as it is withing the limit of 64-bit signed integer
*
* Command: hget bigboxhash max_test_field
* Result: "-9223372036854775709"
*/
commandResult = await redisClient.hGet("bigboxhash", "max_test_field");
console.log(
"Command: hget bigboxhash max_test_field | Result: " + commandResult
);
/**
* Min value allowed as 64-bit int is -9,223,372,036,854,775,808
* Try to decrease the value by 10
* We get an error as the target value goes beyond the minimum integer value
*
* Command: hincrby bigboxhash max_test_field -100
* Result: (error) ERR increment or decrement would overflow
*/
try {
commandResult = await redisClient.hIncrBy(
"bigboxhash",
"max_test_field",
-100
);
console.log(
"Command: hincrby bigboxhash max_test_field -100 | Result: " + commandResult
);
} catch (err) {
console.log("Command: hincrby bigboxhash max_test_field -100 | Error: ", err);
}
process.exit(0);
Output:
Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1 | Result: 4
Command: hgetall customer:100 | Result: [Object: null prototype] {
name: 'Kenneth Braun',
gender: 'male',
age: '42',
order_count: '1'
}
Command: hincrby customer:100 order_count 2 | Result: 3
Command: hget customer:100 order_count | Result: 3
Command: hget bigboxhash firstfield | Result: null
Command: hincrby bigboxhash firstfield 100 | Result: 100
Command: hget bigboxhash firstfield | Result: 100
Command: hget bigboxhash secondfield | Result: null
Command: hincrby bigboxhash secondfield 5 | Result: 5
Command: hget bigboxhash secondfield | Result: 5
Command: hincrby bigboxhash secondfield -3 | Result: 2
Command: hget bigboxhash secondfield | Result: 2
Command: hincrby bigboxhash secondfield -5 | Result: -3
Command: hget bigboxhash secondfield | Result: -3
Command: set bigboxstr "some str value here" | Result: OK
Command: hincrby bigboxstr field1 10 | Error: [ErrorReply: WRONGTYPE Operation against a key holding the wrong kind of value]
Command: hget customer:100 name | Result: Kenneth Braun
Command: hincrby customer:100 name 10 | Error: [ErrorReply: ERR hash value is not an integer]
Command: hset bigboxhash max_test_field 9223372036854775806 | Result: 1
Command: hincrby bigboxhash max_test_field 10 | Error: [ErrorReply: ERR increment or decrement would overflow]
Command: hset bigboxhash max_test_field -9223372036854775709 | Result: 0
Command: hget bigboxhash max_test_field | Result: -9223372036854775709
Command: hincrby bigboxhash max_test_field -100 | Error: [ErrorReply: ERR increment or decrement would overflow]
Notes
- Use the function “
hIncrBy
” of node-redis. - Signature of the method-
function hIncrBy(key: RedisCommandArgument, field: RedisCommandArgument, increment: number)
// Redis HINCRBY Command example in Java
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.HashMap;
import java.util.Map;
public class HIncrBy {
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("localhost", 6379);
try (Jedis jedis = jedisPool.getResource()) {
/**
* Set hash fields
*
* Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1
* Result: (integer) 4
*/
Map<String, String> hashData = new HashMap<>() {{
put("name", "Kenneth Braun");
put("gender", "male");
put("age", "42");
put("order_count", "1");
}};
long hsetResult = jedis.hset("customer:100", hashData);
System.out.println("Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1 | Result: " + hsetResult);
/**
* Check hash fields
*
* Command: hgetall customer:100
* Result:
* 1) "name"
* 2) "Kenneth Braun"
* 3) "gender"
* 4) "male"
* 5) "age"
* 6) "42"
* 7) "order_count"
* 8) "1"
*/
Map<String, String> hgetAllResult = jedis.hgetAll("customer:100");
System.out.println("Command: hgetall customer:100 | Result: " + hgetAllResult.toString());
/**
* Increament order_count field by 2
*
* Command: hincrby customer:100 order_count 2
* Result: (integer) 3
*/
long hincrbyResult = jedis.hincrBy("customer:100", "order_count", 2);
System.out.println("Command: hincrby customer:100 order_count 2 | Result: " + hincrbyResult);
/**
* Check the order_count field
*
* Command: hget customer:100 order_count
* Result: "3"
*/
String hgetResult = jedis.hget("customer:100", "order_count");
System.out.println("Command: hget customer:100 order_count | Result: " + hgetResult);
/**
* bigboxhash does not exist
Check field of a non existing hash
*
* Command: hget bigboxhash firstfield
* Result: (nil)
*/
hgetResult = jedis.hget("bigboxhash", "firstfield");
System.out.println("Command: hget bigboxhash firstfield | Result: " + hgetResult);
/**
* Try to apply HINCRBY on a hash that does not exist
*
* Command: hincrby bigboxhash firstfield 100
* Result: (integer) 100
*/
hincrbyResult = jedis.hincrBy("bigboxhash", "firstfield", 100);
System.out.println("Command: hincrby bigboxhash firstfield 100 | Result: " + hincrbyResult);
/**
* Increament firstfield of bigboxhash
* We see the increased value
*
* Command: hget bigboxhash firstfield
* Result: "100"
*/
hgetResult = jedis.hget("bigboxhash", "firstfield");
System.out.println("Command: hget bigboxhash firstfield | Result: " + hgetResult);
/**
* Check a non existing field, of a hash that exists
*
* Command: hget bigboxhash secondfield
* Result: (nil)
*/
hgetResult = jedis.hget("bigboxhash", "secondfield");
System.out.println("Command: hget bigboxhash secondfield | Result: " + hgetResult);
/**
* Implement HINCRBY on a non existing field
*
* Command: hincrby bigboxhash secondfield 5
* Result: (integer) 5
*/
hincrbyResult = jedis.hincrBy("bigboxhash", "secondfield", 5);
System.out.println("Command: hincrby bigboxhash secondfield 5 | Result: " + hincrbyResult);
/**
* Check the secondfield
*
* Command: hget bigboxhash secondfield
* Result: "5"
*/
hgetResult = jedis.hget("bigboxhash", "secondfield");
System.out.println("Command: hget bigboxhash secondfield | Result: " + hgetResult);
/**
* Use a negative value with HINCRBY
* That will decrease the existing value
*
* Command: hincrby bigboxhash secondfield -3
* Result: (integer) 2
*/
hincrbyResult = jedis.hincrBy("bigboxhash", "secondfield", -3);
System.out.println("Command: hincrby bigboxhash secondfield -3 | Result: " + hincrbyResult);
/**
* Check secondfield value
*
* Command: hget bigboxhash secondfield
* Result: "2"
*/
hgetResult = jedis.hget("bigboxhash", "secondfield");
System.out.println("Command: hget bigboxhash secondfield | Result: " + hgetResult);
/**
* Decreament of the hash field by -5
*
* Command: hincrby bigboxhash secondfield -5
* Result: (integer) -3
*/
hincrbyResult = jedis.hincrBy("bigboxhash", "secondfield", -5);
System.out.println("Command: hincrby bigboxhash secondfield -5 | Result: " + hincrbyResult);
/**
* Check the secondfield value
*
* Command: hget bigboxhash secondfield
* Result: "-3"
*/
hgetResult = jedis.hget("bigboxhash", "secondfield");
System.out.println("Command: hget bigboxhash secondfield | Result: " + hgetResult);
/**
* Set a string key
*
* Command: set bigboxstr "some str value here"
* Result: OK
*/
String setResult = jedis.set("bigboxstr", "some str value here");
System.out.println("Command: set bigboxstr "some str value here" | Result: " + setResult);
/**
* Try to use HINCRBY on the string
* We get an error as command is applied to a wrong data type
*
* Command: hincrby bigboxstr field1 10
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
hincrbyResult = jedis.hincrBy("bigboxstr", "field1", 10);
System.out.println("Command: hincrby bigboxstr field1 10 | Result: " + hincrbyResult);
} catch (Exception e) {
System.out.println("Command: hincrby bigboxstr field1 10 | Error: " + e.getMessage());
}
/**
* Check cutsomer name
*
* Command: hget customer:100 name
* Result: "Kenneth Braun"
*/
hgetResult = jedis.hget("customer:100", "name");
System.out.println("Command: hget customer:100 name | Result: " + hgetResult);
/**
* Try to apply HINCRBY on the name field
* We get an error, as the field has string value
*
* Command: hincrby customer:100 name 10
* Result: (error) ERR hash value is not an integer
*/
try {
hincrbyResult = jedis.hincrBy("customer:100", "name", 10);
System.out.println("Command: hincrby customer:100 name 10 | Result: " + hincrbyResult);
} catch (Exception e) {
System.out.println("Command: hincrby customer:100 name 10 | Error: " + e.getMessage());
}
/**
* Set a filed of a hash to a larg integer value
*
* Command: hset bigboxhash max_test_field 9223372036854775806
* Result: (integer) 1
*/
hsetResult = jedis.hset("bigboxhash", "max_test_field", "9223372036854775806");
System.out.println("Command: hset bigboxhash max_test_field 9223372036854775806 | Result: " + hsetResult);
/**
* Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
* So if we try to increment max_test_field by 10 then it excedes the max integer limit
* We get an error related to max value overflow
*
* Command: hincrby bigboxhash max_test_field 10
* Result: (error) ERR increment or decrement would overflow
*/
try {
hincrbyResult = jedis.hincrBy("bigboxhash", "max_test_field", 10);
System.out.println("Command: hincrby bigboxhash max_test_field 10 | Result: " + hincrbyResult);
} catch (Exception e) {
System.out.println("Command: hincrby bigboxhash max_test_field 10 | Error: " + e.getMessage());
}
/**
* Set field value of a has to large negative nubmer
*
* Command: hset bigboxhash max_test_field -9223372036854775709
* Result: (integer) 0
*/
hsetResult = jedis.hset("bigboxhash", "max_test_field", "-9223372036854775709");
System.out.println("Command: hset bigboxhash max_test_field -9223372036854775709 | Result: " + hsetResult);
/**
* Check the value, we se the negative value is set
* as it is withing the limit of 64-bit signed integer
*
* Command: hget bigboxhash max_test_field
* Result: "-9223372036854775709"
*/
hgetResult = jedis.hget("bigboxhash", "max_test_field");
System.out.println("Command: hget bigboxhash max_test_field | Result: " + hgetResult);
/**
* Min value allowed as 64-bit int is -9,223,372,036,854,775,808
* Try to decrease the value by 10
* We get an error as the target value goes beyond the minimum integer value
*
* Command: hincrby bigboxhash max_test_field -100
* Result: (error) ERR increment or decrement would overflow
*/
try {
hincrbyResult = jedis.hincrBy("bigboxhash", "max_test_field", -100);
System.out.println("Command: hincrby bigboxhash max_test_field -100 | Result: " + hincrbyResult);
} catch (Exception e) {
System.out.println("Command: hincrby bigboxhash max_test_field -100 | Error: " + e.getMessage());
}
}
jedisPool.close();
}
}
Output:
Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1 | Result: 4
Command: hgetall customer:100 | Result: {gender=male, order_count=1, name=Kenneth Braun, age=42}
Command: hincrby customer:100 order_count 2 | Result: 3
Command: hget customer:100 order_count | Result: 3
Command: hget bigboxhash firstfield | Result: null
Command: hincrby bigboxhash firstfield 100 | Result: 100
Command: hget bigboxhash firstfield | Result: 100
Command: hget bigboxhash secondfield | Result: null
Command: hincrby bigboxhash secondfield 5 | Result: 5
Command: hget bigboxhash secondfield | Result: 5
Command: hincrby bigboxhash secondfield -3 | Result: 2
Command: hget bigboxhash secondfield | Result: 2
Command: hincrby bigboxhash secondfield -5 | Result: -3
Command: hget bigboxhash secondfield | Result: -3
Command: set bigboxstr "some str value here" | Result: OK
Command: hincrby bigboxstr field1 10 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: hget customer:100 name | Result: Kenneth Braun
Command: hincrby customer:100 name 10 | Error: ERR hash value is not an integer
Command: hset bigboxhash max_test_field 9223372036854775806 | Result: 1
Command: hincrby bigboxhash max_test_field 10 | Error: ERR increment or decrement would overflow
Command: hset bigboxhash max_test_field -9223372036854775709 | Result: 0
Command: hget bigboxhash max_test_field | Result: -9223372036854775709
Command: hincrby bigboxhash max_test_field -100 | Error: ERR increment or decrement would overflow
Notes
- Use method “
hincrby
” from Jedis package. - The signature of the method is-
public long hincrBy(final String key, final String field, final long value)
// Redis HINCRBY command examples in C#
using StackExchange.Redis;
namespace HIncrBy
{
internal class Program
{
static void Main(string[] args)
{
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
IDatabase rdb = redis.GetDatabase();
/**
* Set hash fields
*
* Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1
* Result: (integer) 4
*/
HashEntry[] hashData = new HashEntry[] {
new HashEntry("name", "Kenneth Braun"),
new HashEntry("gender", "male"),
new HashEntry("state", "California"),
new HashEntry("age", "42"),
new HashEntry("order_count", "1"),
};
rdb.HashSet("customer:100", hashData);
Console.WriteLine("Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1");
/**
* Check hash fields
*
* Command: hgetall customer:100
* Result:
* 1) "name"
* 2) "Kenneth Braun"
* 3) "gender"
* 4) "male"
* 5) "age"
* 6) "42"
* 7) "order_count"
* 8) "1"
*/
HashEntry[] hgetAllResult = rdb.HashGetAll("customer:100");
Console.WriteLine("Command: hgetall customer:100 | Result: ", String.Join(",", hgetAllResult));
/**
* Increament order_count field by 2
*
* Command: hincrby customer:100 order_count 2
* Result: (integer) 3
*/
long hincrbyResult = rdb.HashIncrement("customer:100", "order_count", 2);
Console.WriteLine("Command: hincrby customer:100 order_count 2 | Result: " + hincrbyResult);
/**
* Check the order_count field
*
* Command: hget customer:100 order_count
* Result: "3"
*/
RedisValue hgetResult = rdb.HashGet("customer:100", "order_count");
Console.WriteLine("Command: hget customer:100 order_count | Result: " + hgetResult);
/**
* bigboxhash does not exist
Check field of a non existing hash
*
* Command: hget bigboxhash firstfield
* Result: (nil)
*/
hgetResult = rdb.HashGet("bigboxhash", "firstfield");
Console.WriteLine("Command: hget bigboxhash firstfield | Result: " + hgetResult);
/**
* Try to apply HINCRBY on a hash that does not exist
*
* Command: hincrby bigboxhash firstfield 100
* Result: (integer) 100
*/
hincrbyResult = rdb.HashIncrement("bigboxhash", "firstfield", 100);
Console.WriteLine("Command: hincrby bigboxhash firstfield 100 | Result: " + hincrbyResult);
/**
* Increament firstfield of bigboxhash
* We see the increased value
*
* Command: hget bigboxhash firstfield
* Result: "100"
*/
hgetResult = rdb.HashGet("bigboxhash", "firstfield");
Console.WriteLine("Command: hget bigboxhash firstfield | Result: " + hgetResult);
/**
* Check a non existing field, of a hash that exists
*
* Command: hget bigboxhash secondfield
* Result: (nil)
*/
hgetResult = rdb.HashGet("bigboxhash", "secondfield");
Console.WriteLine("Command: hget bigboxhash secondfield | Result: " + hgetResult);
/**
* Implement HINCRBY on a non existing field
*
* Command: hincrby bigboxhash secondfield 5
* Result: (integer) 5
*/
hincrbyResult = rdb.HashIncrement("bigboxhash", "secondfield", 5);
Console.WriteLine("Command: hincrby bigboxhash secondfield 5 | Result: " + hincrbyResult);
/**
* Check the secondfield
*
* Command: hget bigboxhash secondfield
* Result: "5"
*/
hgetResult = rdb.HashGet("bigboxhash", "secondfield");
Console.WriteLine("Command: hget bigboxhash secondfield | Result: " + hgetResult);
/**
* Use a negative value with HINCRBY
* That will decrease the existing value
*
* Command: hincrby bigboxhash secondfield -3
* Result: (integer) 2
*/
hincrbyResult = rdb.HashIncrement("bigboxhash", "secondfield", -3);
Console.WriteLine("Command: hincrby bigboxhash secondfield -3 | Result: " + hincrbyResult);
/**
* Check secondfield value
*
* Command: hget bigboxhash secondfield
* Result: "2"
*/
hgetResult = rdb.HashGet("bigboxhash", "secondfield");
Console.WriteLine("Command: hget bigboxhash secondfield | Result: " + hgetResult);
/**
* Decreament of the hash field by -5
*
* Command: hincrby bigboxhash secondfield -5
* Result: (integer) -3
*/
hincrbyResult = rdb.HashIncrement("bigboxhash", "secondfield", -5);
Console.WriteLine("Command: hincrby bigboxhash secondfield -5 | Result: " + hincrbyResult);
/**
* Check the secondfield value
*
* Command: hget bigboxhash secondfield
* Result: "-3"
*/
hgetResult = rdb.HashGet("bigboxhash", "secondfield");
Console.WriteLine("Command: hget bigboxhash secondfield | Result: " + hgetResult);
/**
* Set a string key
*
* Command: set bigboxstr "some str value here"
* Result: OK
*/
bool setResult = rdb.StringSet("bigboxstr", "some str value here");
Console.WriteLine("Command: set bigboxstr "some str value here" | Result: " + setResult);
/**
* Try to use HINCRBY on the string
* We get an error as command is applied to a wrong data type
*
* Command: hincrby bigboxstr field1 10
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try
{
hincrbyResult = rdb.HashIncrement("bigboxstr", "field1", 10);
Console.WriteLine("Command: hincrby bigboxstr field1 10 | Result: " + hincrbyResult);
}
catch (Exception e)
{
Console.WriteLine("Command: hincrby bigboxstr field1 10 | Error: " + e.Message);
}
/**
* Check cutsomer name
*
* Command: hget customer:100 name
* Result: "Kenneth Braun"
*/
hgetResult = rdb.HashGet("customer:100", "name");
Console.WriteLine("Command: hget customer:100 name | Result: " + hgetResult);
/**
* Try to apply HINCRBY on the name field
* We get an error, as the field has string value
*
* Command: hincrby customer:100 name 10
* Result: (error) ERR hash value is not an integer
*/
try
{
hincrbyResult = rdb.HashIncrement("customer:100", "name", 10);
Console.WriteLine("Command: hincrby customer:100 name 10 | Result: " + hincrbyResult);
}
catch (Exception e)
{
Console.WriteLine("Command: hincrby customer:100 name 10 | Error: " + e.Message);
}
/**
* Set a filed of a hash to a larg integer value
*
* Command: hset bigboxhash max_test_field 9223372036854775806
* Result: (integer) 1
*/
bool hsetResult = rdb.HashSet("bigboxhash", "max_test_field", "9223372036854775806");
Console.WriteLine("Command: hset bigboxhash max_test_field 9223372036854775806 | Result: " + hsetResult);
/**
* Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
* So if we try to increment max_test_field by 10 then it excedes the max integer limit
* We get an error related to max value overflow
*
* Command: hincrby bigboxhash max_test_field 10
* Result: (error) ERR increment or decrement would overflow
*/
try
{
hincrbyResult = rdb.HashIncrement("bigboxhash", "max_test_field", 10);
Console.WriteLine("Command: hincrby bigboxhash max_test_field 10 | Result: " + hincrbyResult);
}
catch (Exception e)
{
Console.WriteLine("Command: hincrby bigboxhash max_test_field 10 | Error: " + e.Message);
}
/**
* Set field value of a has to large negative nubmer
*
* Command: hset bigboxhash max_test_field -9223372036854775709
* Result: (integer) 0
*/
hsetResult = rdb.HashSet("bigboxhash", "max_test_field", "-9223372036854775709");
Console.WriteLine("Command: hset bigboxhash max_test_field -9223372036854775709 | Result: " + hsetResult);
/**
* Check the value, we se the negative value is set
* as it is withing the limit of 64-bit signed integer
*
* Command: hget bigboxhash max_test_field
* Result: "-9223372036854775709"
*/
hgetResult = rdb.HashGet("bigboxhash", "max_test_field");
Console.WriteLine("Command: hget bigboxhash max_test_field | Result: " + hgetResult);
/**
* Min value allowed as 64-bit int is -9,223,372,036,854,775,808
* Try to decrease the value by 10
* We get an error as the target value goes beyond the minimum integer value
*
* Command: hincrby bigboxhash max_test_field -100
* Result: (error) ERR increment or decrement would overflow
*/
try
{
hincrbyResult = rdb.HashIncrement("bigboxhash", "max_test_field", -100);
Console.WriteLine("Command: hincrby bigboxhash max_test_field -100 | Result: " + hincrbyResult);
}
catch (Exception e)
{
Console.WriteLine("Command: hincrby bigboxhash max_test_field -100 | Error: " + e.Message);
}
}
}
}
Output:
Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1
Command: hgetall customer:100 | Result:
Command: hincrby customer:100 order_count 2 | Result: 3
Command: hget customer:100 order_count | Result: 3
Command: hget bigboxhash firstfield | Result:
Command: hincrby bigboxhash firstfield 100 | Result: 100
Command: hget bigboxhash firstfield | Result: 100
Command: hget bigboxhash secondfield | Result:
Command: hincrby bigboxhash secondfield 5 | Result: 5
Command: hget bigboxhash secondfield | Result: 5
Command: hincrby bigboxhash secondfield -3 | Result: 2
Command: hget bigboxhash secondfield | Result: 2
Command: hincrby bigboxhash secondfield -5 | Result: -3
Command: hget bigboxhash secondfield | Result: -3
Command: set bigboxstr "some str value here" | Result: True
Command: hincrby bigboxstr field1 10 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: hget customer:100 name | Result: Kenneth Braun
Command: hincrby customer:100 name 10 | Error: ERR hash value is not an integer
Command: hset bigboxhash max_test_field 9223372036854775806 | Result: True
Command: hincrby bigboxhash max_test_field 10 | Error: ERR increment or decrement would overflow
Command: hset bigboxhash max_test_field -9223372036854775709 | Result: False
Command: hget bigboxhash max_test_field | Result: -9223372036854775709
Command: hincrby bigboxhash max_test_field -100 | Error: ERR increment or decrement would overflow
Notes
- Use the method “
” from StackExchange.Redis.HashIncrement
- Signatures of the method are-
long HashIncrement(RedisKey key, RedisValue hashField, long value = 1, CommandFlags flags = CommandFlags.None)
double HashIncrement(RedisKey key, RedisValue hashField, double value, CommandFlags flags = CommandFlags.None)
<?php
// Redis HINCRBY command example in PHP
require 'vendor/autoload.php';
// Connect to Redis
$redisClient = new PredisClient([
'scheme' => 'tcp',
'host' => 'localhost',
'port' => 6379,
]);
/**
* Set hash fields
*
* Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1
* Result: (integer) 4
*/
$commandResult = $redisClient->hset("customer:100",
"name", "Kenneth Braun",
"gender", "male",
"age", "42",
"order_count", "1"
);
echo "Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1 | Result: " . $commandResult . "n";
/**
* Check hash fields
*
* Command: hgetall customer:100
* Result:
* 1) "name"
* 2) "Kenneth Braun"
* 3) "gender"
* 4) "male"
* 5) "age"
* 6) "42"
* 7) "order_count"
* 8) "1"
*/
$commandResult = $redisClient->hgetall("customer:100");
echo "Command: hgetall customer:100 | Result: ";
print_r($commandResult);
/**
* Increament order_count field by 2
*
* Command: hincrby customer:100 order_count 2
* Result: (integer) 3
*/
$commandResult = $redisClient->hincrby("customer:100", "order_count", 2);
echo "Command: hincrby customer:100 order_count 2 | Result: " . $commandResult . "n";
/**
* Check the order_count field
*
* Command: hget customer:100 order_count
* Result: "3"
*/
$commandResult = $redisClient->hget("customer:100", "order_count");
echo "Command: hget customer:100 order_count | Result: " . $commandResult . "n";
/**
* bigboxhash does not exist
Check field of a non existing hash
*
* Command: hget bigboxhash firstfield
* Result: (nil)
*/
$commandResult = $redisClient->hget("bigboxhash", "firstfield");
echo "Command: hget bigboxhash firstfield | Result: " . $commandResult . "n";
/**
* Try to apply HINCRBY on a hash that does not exist
*
* Command: hincrby bigboxhash firstfield 100
* Result: (integer) 100
*/
$commandResult = $redisClient->hincrby("bigboxhash", "firstfield", 100);
echo "Command: hincrby bigboxhash firstfield 100 | Result: " . $commandResult . "n";
/**
* Increament firstfield of bigboxhash
* We see the increased value
*
* Command: hget bigboxhash firstfield
* Result: "100"
*/
$commandResult = $redisClient->hget("bigboxhash", "firstfield");
echo "Command: hget bigboxhash firstfield | Result: " . $commandResult . "n";
/**
* Check a non existing field, of a hash that exists
*
* Command: hget bigboxhash secondfield
* Result: (nil)
*/
$commandResult = $redisClient->hget("bigboxhash", "secondfield");
echo "Command: hget bigboxhash secondfield | Result: " . $commandResult . "n";
/**
* Implement HINCRBY on a non existing field
*
* Command: hincrby bigboxhash secondfield 5
* Result: (integer) 5
*/
$commandResult = $redisClient->hincrby("bigboxhash", "secondfield", 5);
echo "Command: hincrby bigboxhash secondfield 5 | Result: " . $commandResult . "n";
/**
* Check the secondfield
*
* Command: hget bigboxhash secondfield
* Result: "5"
*/
$commandResult = $redisClient->hget("bigboxhash", "secondfield");
echo "Command: hget bigboxhash secondfield | Result: " . $commandResult . "n";
/**
* Use a negative value with HINCRBY
* That will decrease the existing value
*
* Command: hincrby bigboxhash secondfield -3
* Result: (integer) 2
*/
$commandResult = $redisClient->hincrby("bigboxhash", "secondfield", -3);
echo "Command: hincrby bigboxhash secondfield -3 | Result: " . $commandResult . "n";
/**
* Check secondfield value
*
* Command: hget bigboxhash secondfield
* Result: "2"
*/
$commandResult = $redisClient->hget("bigboxhash", "secondfield");
echo "Command: hget bigboxhash secondfield | Result: " . $commandResult . "n";
/**
* Decreament of the hash field by -5
*
* Command: hincrby bigboxhash secondfield -5
* Result: (integer) -3
*/
$commandResult = $redisClient->hincrby("bigboxhash", "secondfield", -5);
echo "Command: hincrby bigboxhash secondfield -5 | Result: " . $commandResult . "n";
/**
* Check the secondfield value
*
* Command: hget bigboxhash secondfield
* Result: "-3"
*/
$commandResult = $redisClient->hget("bigboxhash", "secondfield");
echo "Command: hget bigboxhash secondfield | Result: " . $commandResult . "n";
/**
* Set a string key
*
* Command: set bigboxstr "some str value here"
* Result: OK
*/
$commandResult = $redisClient->set("bigboxstr", "some str value here");
echo "Command: set bigboxstr "some str value here" | Result: " . $commandResult . "n";
/**
* Try to use HINCRBY on the string
* We get an error as command is applied to a wrong data type
*
* Command: hincrby bigboxstr field1 10
* Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
*/
try {
$commandResult = $redisClient->hincrby("bigboxstr", "field1", 10);
echo "Command: hincrby bigboxstr field1 10 | Result: " . $commandResult . "n";
} catch (Exception $e) {
echo "Command: hincrby bigboxstr field1 10 | Error: " . $e->getMessage() . "n";
}
/**
* Check cutsomer name
*
* Command: hget customer:100 name
* Result: "Kenneth Braun"
*/
$commandResult = $redisClient->hget("customer:100", "name");
echo "Command: hget customer:100 name | Result: " . $commandResult . "n";
/**
* Try to apply HINCRBY on the name field
* We get an error, as the field has string value
*
* Command: hincrby customer:100 name 10
* Result: (error) ERR hash value is not an integer
*/
try {
$commandResult = $redisClient->hincrby("customer:100", "name", 10);
echo "Command: hincrby customer:100 name 10 | Result: " . $commandResult . "n";
} catch (Exception $e) {
echo "Command: hincrby customer:100 name 10 | Error: " . $e->getMessage() . "n";
}
/**
* Set a filed of a hash to a larg integer value
*
* Command: hset bigboxhash max_test_field 9223372036854775806
* Result: (integer) 1
*/
$commandResult = $redisClient->hset("bigboxhash", "max_test_field", "9223372036854775806");
echo "Command: hset bigboxhash max_test_field 9223372036854775806 | Result: " . $commandResult . "n";
/**
* Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
* So if we try to increment max_test_field by 10 then it excedes the max integer limit
* We get an error related to max value overflow
*
* Command: hincrby bigboxhash max_test_field 10
* Result: (error) ERR increment or decrement would overflow
*/
try {
$commandResult = $redisClient->hincrby("bigboxhash", "max_test_field", 10);
echo "Command: hincrby bigboxhash max_test_field 10 | Result: " . $commandResult . "n";
} catch (Exception $e) {
echo "Command: hincrby bigboxhash max_test_field 10 | Error: " . $e->getMessage() . "n";
}
/**
* Set field value of a has to large negative nubmer
*
* Command: hset bigboxhash max_test_field -9223372036854775709
* Result: (integer) 0
*/
$commandResult = $redisClient->hset("bigboxhash", "max_test_field", "-9223372036854775709");
echo "Command: hset bigboxhash max_test_field -9223372036854775709 | Result: " . $commandResult . "n";
/**
* Check the value, we se the negative value is set
* as it is withing the limit of 64-bit signed integer
*
* Command: hget bigboxhash max_test_field
* Result: "-9223372036854775709"
*/
$commandResult = $redisClient->hget("bigboxhash", "max_test_field");
echo "Command: hget bigboxhash max_test_field | Result: " . $commandResult . "n";
/**
* Min value allowed as 64-bit int is -9,223,372,036,854,775,808
* Try to decrease the value by 10
* We get an error as the target value goes beyond the minimum integer value
*
* Command: hincrby bigboxhash max_test_field -100
* Result: (error) ERR increment or decrement would overflow
*/
try {
$commandResult = $redisClient->hincrby("bigboxhash", "max_test_field", -100);
echo "Command: hincrby bigboxhash max_test_field -100 | Result: " . $commandResult . "n";
} catch (Exception $e) {
echo "Command: hincrby bigboxhash max_test_field -100 | Error: " . $e->getMessage() . "n";
}
Output:
Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1 | Result: 4
Command: hgetall customer:100 | Result: Array
(
[name] => Kenneth Braun
[gender] => male
[age] => 42
[order_count] => 1
)
Command: hincrby customer:100 order_count 2 | Result: 3
Command: hget customer:100 order_count | Result: 3
Command: hget bigboxhash firstfield | Result:
Command: hincrby bigboxhash firstfield 100 | Result: 100
Command: hget bigboxhash firstfield | Result: 100
Command: hget bigboxhash secondfield | Result:
Command: hincrby bigboxhash secondfield 5 | Result: 5
Command: hget bigboxhash secondfield | Result: 5
Command: hincrby bigboxhash secondfield -3 | Result: 2
Command: hget bigboxhash secondfield | Result: 2
Command: hincrby bigboxhash secondfield -5 | Result: -3
Command: hget bigboxhash secondfield | Result: -3
Command: set bigboxstr "some str value here" | Result: OK
Command: hincrby bigboxstr field1 10 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: hget customer:100 name | Result: Kenneth Braun
Command: hincrby customer:100 name 10 | Error: ERR hash value is not an integer
Command: hset bigboxhash max_test_field 9223372036854775806 | Result: 1
Command: hincrby bigboxhash max_test_field 10 | Error: ERR increment or decrement would overflow
Command: hset bigboxhash max_test_field -9223372036854775709 | Result: 0
Command: hget bigboxhash max_test_field | Result: -9223372036854775709
Command: hincrby bigboxhash max_test_field -100 | Error: ERR increment or decrement would overflow
Notes
- Use the method “
hincrby
” of predis. - Signature of the method is-
hincrby(string $key, string $field, int $increment): int
# Redis HINCRBY 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 hash fields
# Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1
# Result: (integer) 4
commandResult = redisClient.hset(
"customer:100",
mapping={
"name": "Kenneth Braun",
"gender": "male",
"age": "42",
"order_count": "1",
},
)
print(
'Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1 | Result: {}'.format(
commandResult
)
)
# Check hash fields
# Command: hgetall customer:100
# Result:
# 1) "name"
# 2) "Kenneth Braun"
# 3) "gender"
# 4) "male"
# 5) "age"
# 6) "42"
# 7) "order_count"
# 8) "1"
commandResult = redisClient.hgetall("customer:100")
print("Command: hgetall customer:100 | Result: {}".format(commandResult))
# Increament order_count field by 2
# Command: hincrby customer:100 order_count 2
# Result: (integer) 3
commandResult = redisClient.hincrby("customer:100", "order_count", 2)
print("Command: hincrby customer:100 order_count 2 | Result: {}".format(commandResult))
# Check the order_count field
# Command: hget customer:100 order_count
# Result: "3"
commandResult = redisClient.hget("customer:100", "order_count")
print("Command: hget customer:100 order_count | Result: {}".format(commandResult))
# bigboxhash does not exist
# Check field of a non existing hash
# Command: hget bigboxhash firstfield
# Result: (nil)
commandResult = redisClient.hget("bigboxhash", "firstfield")
print("Command: hget bigboxhash firstfield | Result: {}".format(commandResult))
# Try to apply HINCRBY on a hash that does not exist
# Command: hincrby bigboxhash firstfield 100
# Result: (integer) 100
commandResult = redisClient.hincrby("bigboxhash", "firstfield", 100)
print("Command: hincrby bigboxhash firstfield 100 | Result: {}".format(commandResult))
# Increament firstfield of bigboxhash
# We see the increased value
# Command: hget bigboxhash firstfield
# Result: "100"
commandResult = redisClient.hget("bigboxhash", "firstfield")
print("Command: hget bigboxhash firstfield | Result: {}".format(commandResult))
# Check a non existing field, of a hash that exists
# Command: hget bigboxhash secondfield
# Result: (nil)
commandResult = redisClient.hget("bigboxhash", "secondfield")
print("Command: hget bigboxhash secondfield | Result: {}".format(commandResult))
# Implement HINCRBY on a non existing field
# Command: hincrby bigboxhash secondfield 5
# Result: (integer) 5
commandResult = redisClient.hincrby("bigboxhash", "secondfield", 5)
print("Command: hincrby bigboxhash secondfield 5 | Result: {}".format(commandResult))
# Check the secondfield
# Command: hget bigboxhash secondfield
# Result: "5"
commandResult = redisClient.hget("bigboxhash", "secondfield")
print("Command: hget bigboxhash secondfield | Result: {}".format(commandResult))
# Use a negative value with HINCRBY
# That will decrease the existing value
# Command: hincrby bigboxhash secondfield -3
# Result: (integer) 2
commandResult = redisClient.hincrby("bigboxhash", "secondfield", -3)
print("Command: hincrby bigboxhash secondfield -3 | Result: {}".format(commandResult))
# Check secondfield value
# Command: hget bigboxhash secondfield
# Result: "2"
commandResult = redisClient.hget("bigboxhash", "secondfield")
print("Command: hget bigboxhash secondfield | Result: {}".format(commandResult))
# Decreament of the hash field by -5
# Command: hincrby bigboxhash secondfield -5
# Result: (integer) -3
commandResult = redisClient.hincrby("bigboxhash", "secondfield", -5)
print("Command: hincrby bigboxhash secondfield -5 | Result: {}".format(commandResult))
# Check the secondfield value
# Command: hget bigboxhash secondfield
# Result: "-3"
commandResult = redisClient.hget("bigboxhash", "secondfield")
print("Command: hget bigboxhash secondfield | Result: {}".format(commandResult))
# Set a string key
# Command: set bigboxstr "some str value here"
# Result: OK
commandResult = redisClient.set("bigboxstr", "some str value here")
print('Command: set bigboxstr "some str value here" | Result: {}'.format(commandResult))
# Try to use HINCRBY on the string
# We get an error as command is applied to a wrong data type
# Command: hincrby bigboxstr field1 10
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
try:
commandResult = redisClient.hincrby("bigboxstr", "field1", 10)
print("Command: hincrby bigboxstr field1 10 | Result: {}".format(commandResult))
except Exception as error:
print("Command: hincrby bigboxstr field1 10 | Error: ", error)
# Check cutsomer name
# Command: hget customer:100 name
# Result: "Kenneth Braun"
commandResult = redisClient.hget("customer:100", "name")
print("Command: hget customer:100 name | Result: {}".format(commandResult))
# Try to apply HINCRBY on the name field
# We get an error, as the field has string value
# Command: hincrby customer:100 name 10
# Result: (error) ERR hash value is not an integer
try:
commandResult = redisClient.hincrby("customer:100", "name", 10)
print("Command: hincrby customer:100 name 10 | Result: {}".format(commandResult))
except Exception as error:
print("Command: hincrby customer:100 name 10 | Error: ", error)
# Set a filed of a hash to a larg integer value
# Command: hset bigboxhash max_test_field 9223372036854775806
# Result: (integer) 1
commandResult = redisClient.hset("bigboxhash", "max_test_field", "9223372036854775806")
print(
"Command: hset bigboxhash max_test_field 9223372036854775806 | Result: {}".format(
commandResult
)
)
# Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
# So if we try to increment max_test_field by 10 then it excedes the max integer limit
# We get an error related to max value overflow
# Command: hincrby bigboxhash max_test_field 10
# Result: (error) ERR increment or decrement would overflow
try:
commandResult = redisClient.hincrby("bigboxhash", "max_test_field", 10)
print(
"Command: hincrby bigboxhash max_test_field 10 | Result: {}".format(
commandResult
)
)
except Exception as error:
print("Command: hincrby bigboxhash max_test_field 10 | Error: ", error)
# Set field value of a has to large negative nubmer
# Command: hset bigboxhash max_test_field -9223372036854775709
# Result: (integer) 0
commandResult = redisClient.hset("bigboxhash", "max_test_field", "-9223372036854775709")
print(
"Command: hset bigboxhash max_test_field -9223372036854775709 | Result: {}".format(
commandResult
)
)
# Check the value, we se the negative value is set
# as it is withing the limit of 64-bit signed integer
# Command: hget bigboxhash max_test_field
# Result: "-9223372036854775709"
commandResult = redisClient.hget("bigboxhash", "max_test_field")
print("Command: hget bigboxhash max_test_field | Result: {}".format(commandResult))
# Min value allowed as 64-bit int is -9,223,372,036,854,775,808
# Try to decrease the value by 10
# We get an error as the target value goes beyond the minimum integer value
# Command: hincrby bigboxhash max_test_field -100
# Result: (error) ERR increment or decrement would overflow
try:
commandResult = redisClient.hincrby("bigboxhash", "max_test_field", -100)
print(
"Command: hincrby bigboxhash max_test_field -100 | Result: {}".format(
commandResult
)
)
except Exception as error:
print("Command: hincrby bigboxhash max_test_field -100 | Error: ", error)
Output:
Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1 | Result: 4
Command: hgetall customer:100 | Result: {'name': 'Kenneth Braun', 'gender': 'male', 'age': '42', 'order_count': '1'}
Command: hincrby customer:100 order_count 2 | Result: 3
Command: hget customer:100 order_count | Result: 3
Command: hget bigboxhash firstfield | Result: None
Command: hincrby bigboxhash firstfield 100 | Result: 100
Command: hget bigboxhash firstfield | Result: 100
Command: hget bigboxhash secondfield | Result: None
Command: hincrby bigboxhash secondfield 5 | Result: 5
Command: hget bigboxhash secondfield | Result: 5
Command: hincrby bigboxhash secondfield -3 | Result: 2
Command: hget bigboxhash secondfield | Result: 2
Command: hincrby bigboxhash secondfield -5 | Result: -3
Command: hget bigboxhash secondfield | Result: -3
Command: set bigboxstr "some str value here" | Result: True
Command: hincrby bigboxstr field1 10 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: hget customer:100 name | Result: Kenneth Braun
Command: hincrby customer:100 name 10 | Error: hash value is not an integer
Command: hset bigboxhash max_test_field 9223372036854775806 | Result: 1
Command: hincrby bigboxhash max_test_field 10 | Error: increment or decrement would overflow
Command: hset bigboxhash max_test_field -9223372036854775709 | Result: 0
Command: hget bigboxhash max_test_field | Result: -9223372036854775709
Command: hincrby bigboxhash max_test_field -100 | Error: increment or decrement would overflow
Notes
- Use method “
hincrby
” from redis-py. - Signature of the method is –
def hincrby(self, name: str, key: str, amount: int = 1) -> Union[Awaitable[int], int]
# Redis HINCRBY command example in Ruby
require 'redis'
redis = Redis.new(host: "localhost", port: 6379)
# Set hash fields
# Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1
# Result: (integer) 4
commandResult = redis.hset(
"customer:100",
{
"name" => "Kenneth Braun",
"gender" => "male",
"age" => "42",
"order_count" => "1",
},
)
print("Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1 | Result: ", commandResult, "n")
# Check hash fields
# Command: hgetall customer:100
# Result:
# 1) "name"
# 2) "Kenneth Braun"
# 3) "gender"
# 4) "male"
# 5) "age"
# 6) "42"
# 7) "order_count"
# 8) "1"
commandResult = redis.hgetall("customer:100")
print("Command: hgetall customer:100 | Result: ", commandResult, "n")
# Increament order_count field by 2
# Command: hincrby customer:100 order_count 2
# Result: (integer) 3
commandResult = redis.hincrby("customer:100", "order_count", 2)
print("Command: hincrby customer:100 order_count 2 | Result: ", commandResult, "n")
# Check the order_count field
# Command: hget customer:100 order_count
# Result: "3"
commandResult = redis.hget("customer:100", "order_count")
print("Command: hget customer:100 order_count | Result: ", commandResult, "n")
# bigboxhash does not exist
# Check field of a non existing hash
# Command: hget bigboxhash firstfield
# Result: (nil)
commandResult = redis.hget("bigboxhash", "firstfield")
print("Command: hget bigboxhash firstfield | Result: ", commandResult, "n")
# Try to apply HINCRBY on a hash that does not exist
# Command: hincrby bigboxhash firstfield 100
# Result: (integer) 100
commandResult = redis.hincrby("bigboxhash", "firstfield", 100)
print("Command: hincrby bigboxhash firstfield 100 | Result: ", commandResult, "n")
# Increament firstfield of bigboxhash
# We see the increased value
# Command: hget bigboxhash firstfield
# Result: "100"
commandResult = redis.hget("bigboxhash", "firstfield")
print("Command: hget bigboxhash firstfield | Result: ", commandResult, "n")
# Check a non existing field, of a hash that exists
# Command: hget bigboxhash secondfield
# Result: (nil)
commandResult = redis.hget("bigboxhash", "secondfield")
print("Command: hget bigboxhash secondfield | Result: ", commandResult, "n")
# Implement HINCRBY on a non existing field
# Command: hincrby bigboxhash secondfield 5
# Result: (integer) 5
commandResult = redis.hincrby("bigboxhash", "secondfield", 5)
print("Command: hincrby bigboxhash secondfield 5 | Result: ", commandResult, "n")
# Check the secondfield
# Command: hget bigboxhash secondfield
# Result: "5"
commandResult = redis.hget("bigboxhash", "secondfield")
print("Command: hget bigboxhash secondfield | Result: ", commandResult, "n")
# Use a negative value with HINCRBY
# That will decrease the existing value
# Command: hincrby bigboxhash secondfield -3
# Result: (integer) 2
commandResult = redis.hincrby("bigboxhash", "secondfield", -3)
print("Command: hincrby bigboxhash secondfield -3 | Result: ", commandResult, "n")
# Check secondfield value
# Command: hget bigboxhash secondfield
# Result: "2"
commandResult = redis.hget("bigboxhash", "secondfield")
print("Command: hget bigboxhash secondfield | Result: ", commandResult, "n")
# Decreament of the hash field by -5
# Command: hincrby bigboxhash secondfield -5
# Result: (integer) -3
commandResult = redis.hincrby("bigboxhash", "secondfield", -5)
print("Command: hincrby bigboxhash secondfield -5 | Result: ", commandResult, "n")
# Check the secondfield value
# Command: hget bigboxhash secondfield
# Result: "-3"
commandResult = redis.hget("bigboxhash", "secondfield")
print("Command: hget bigboxhash secondfield | Result: ", commandResult, "n")
# Set a string key
# Command: set bigboxstr "some str value here"
# Result: OK
commandResult = redis.set("bigboxstr", "some str value here")
print("Command: set bigboxstr "some str value here" | Result: ", commandResult, "n")
# Try to use HINCRBY on the string
# We get an error as command is applied to a wrong data type
# Command: hincrby bigboxstr field1 10
# Result: (error) WRONGTYPE Operation against a key holding the wrong kind of value
begin
commandResult = redis.hincrby("bigboxstr", "field1", 10)
print("Command: hincrby bigboxstr field1 10 | Result: ", commandResult, "n")
rescue => e
print("Command: hincrby bigboxstr field1 10 | Error: ", e, "n")
end
# Check cutsomer name
# Command: hget customer:100 name
# Result: "Kenneth Braun"
commandResult = redis.hget("customer:100", "name")
print("Command: hget customer:100 name | Result: ", commandResult, "n")
# Try to apply HINCRBY on the name field
# We get an error, as the field has string value
# Command: hincrby customer:100 name 10
# Result: (error) ERR hash value is not an integer
begin
commandResult = redis.hincrby("customer:100", "name", 10)
print("Command: hincrby customer:100 name 10 | Result: ", commandResult, "n")
rescue => e
print("Command: hincrby customer:100 name 10 | Error: ", e, "n")
end
# Set a filed of a hash to a larg integer value
# Command: hset bigboxhash max_test_field 9223372036854775806
# Result: (integer) 1
commandResult = redis.hset("bigboxhash", "max_test_field", "9223372036854775806")
print("Command: hset bigboxhash max_test_field 9223372036854775806 | Result: ", commandResult, "n")
# Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
# So if we try to increment max_test_field by 10 then it excedes the max integer limit
# We get an error related to max value overflow
# Command: hincrby bigboxhash max_test_field 10
# Result: (error) ERR increment or decrement would overflow
begin
commandResult = redis.hincrby("bigboxhash", "max_test_field", 10)
print("Command: hincrby bigboxhash max_test_field 10 | Result: ", commandResult, "n")
rescue => e
print("Command: hincrby bigboxhash max_test_field 10 | Error: ", e, "n")
end
# Set field value of a has to large negative nubmer
# Command: hset bigboxhash max_test_field -9223372036854775709
# Result: (integer) 0
commandResult = redis.hset("bigboxhash", "max_test_field", "-9223372036854775709")
print("Command: hset bigboxhash max_test_field -9223372036854775709 | Result: ", commandResult, "n")
# Check the value, we se the negative value is set
# as it is withing the limit of 64-bit signed integer
# Command: hget bigboxhash max_test_field
# Result: "-9223372036854775709"
commandResult = redis.hget("bigboxhash", "max_test_field")
print("Command: hget bigboxhash max_test_field | Result: ", commandResult, "n")
# Min value allowed as 64-bit int is -9,223,372,036,854,775,808
# Try to decrease the value by 10
# We get an error as the target value goes beyond the minimum integer value
# Command: hincrby bigboxhash max_test_field -100
# Result: (error) ERR increment or decrement would overflow
begin
commandResult = redis.hincrby("bigboxhash", "max_test_field", -100)
print("Command: hincrby bigboxhash max_test_field -100 | Result: ", commandResult, "n")
rescue => e
print("Command: hincrby bigboxhash max_test_field -100 | Error: ", e, "n")
end
Output:
Command: hset customer:100 name "Kenneth Braun" gender male age 42 order_count 1 | Result: 4
Command: hgetall customer:100 | Result: {"name"=>"Kenneth Braun", "gender"=>"male", "age"=>"42", "order_count"=>"1"}
Command: hincrby customer:100 order_count 2 | Result: 3
Command: hget customer:100 order_count | Result: 3
Command: hget bigboxhash firstfield | Result:
Command: hincrby bigboxhash firstfield 100 | Result: 100
Command: hget bigboxhash firstfield | Result: 100
Command: hget bigboxhash secondfield | Result:
Command: hincrby bigboxhash secondfield 5 | Result: 5
Command: hget bigboxhash secondfield | Result: 5
Command: hincrby bigboxhash secondfield -3 | Result: 2
Command: hget bigboxhash secondfield | Result: 2
Command: hincrby bigboxhash secondfield -5 | Result: -3
Command: hget bigboxhash secondfield | Result: -3
Command: set bigboxstr "some str value here" | Result: OK
Command: hincrby bigboxstr field1 10 | Error: WRONGTYPE Operation against a key holding the wrong kind of value
Command: hget customer:100 name | Result: Kenneth Braun
Command: hincrby customer:100 name 10 | Error: ERR hash value is not an integer
Command: hset bigboxhash max_test_field 9223372036854775806 | Result: 1
Command: hincrby bigboxhash max_test_field 10 | Error: ERR increment or decrement would overflow
Command: hset bigboxhash max_test_field -9223372036854775709 | Result: 0
Command: hget bigboxhash max_test_field | Result: -9223372036854775709
Command: hincrby bigboxhash max_test_field -100 | Error: ERR increment or decrement would overflow
Notes
- Use method “
hincrby
” from the redis-rb. - Signature of the method is-
# @param [String] key
# @param [String] field
# @param [Integer] increment
# @return [Integer] value of the field after incrementing it
def hincrby(key, field, increment)
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 |
---|---|
HSETNX | Command Details |
HMGET | Command Details |
HGETALL | Command Details |
HSET | Command Details |