Redis HINCRBYFLOAT command is used to increment the float value of a hash field by the given amount. In a Node.js context, it's commonly utilized in applications where you need to adjust numeric values within a hash, such as:
The node_redis
library provides the hincrbyfloat
function to perform this operation.
Here's a simple example of incrementing a user's score:
const redis = require("redis"); const client = redis.createClient(); client.hset("user_scores", "bob", 10, redis.print); client.hincrbyfloat("user_scores", "bob", 2.1, (err, res) => { if(err) console.error(err); else console.log(`Bob's new score: ${res}`); }); client.quit();
In this code:
hset
.hincrbyfloat
. The new score is logged to console.hincrbyfloat
with fields that contain numeric data. If the field does not exist or holds non-numeric data, an error will occur.client.quit()
.hincrbyfloat
. If the key or field doesn't exist, Redis will assume it's 0 and perform the operation.Q: What happens if the hash or field doesn't exist?
A: If either the hash or field do not exist, Redis creates them for you. The initial value is assumed to be 0, so hincrbyfloat
increments from 0.
Q: What precision does HINCRBYFLOAT use?
A: Redis HINCRBYFLOAT uses a double precision floating point number, which provides very high precision and large range of magnitudes.
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.