Operations with Numbers in Redis Using C++
Moving On to Operations with Numbers
Welcome back! Now that you've learned how to work with numbers in Redis, it's time to build on that knowledge and explore some basic operations with these numbers. This lesson will show you how to perform operations like incrementing, decrementing, and modifying numeric values directly in Redis.
What You'll Learn
In this lesson, you will learn how to:
- Increment and decrement numeric values.
- Modify numeric values using operations such as increments by a floating point.
Here's the code snippet that we'll be working with:
Let's break it down:
-
After setting initial values for
count,completion_rate, andduration, we perform various operations:- The command
DECR countdecreases the value ofcountby 1. You can also provide a second argument to decrement by a specific value:DECRBY count 2will decrementcountby 2.DECRcommands are used only for integer values. - The command
INCRBYFLOAT completion_rate 1.5incrementscompletion_rateby 1.5. This method is versatile for both integer and floating-point values. - The command
INCR durationincreasesdurationby 1. This can be modified by a specific increment value:INCRBY duration 5will increasedurationby 5.INCRcommands should also be used on integer values.
- The command
-
Finally, we retrieve the updated values using
GETcommands and print them to show the current stage.
