Working with Numeric Operations in Redis Using Go
Working with Numeric Operations in Redis Using Go
Welcome back! Now that you've learned how to work with numbers in Redis using Go, it's time to expand that knowledge and explore some basic operations with these numbers. This lesson will show you how to perform operations such as incrementing, decrementing, and modifying numeric values directly in Redis using Go.
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:Decr(ctx, "count")decreases the value ofcountby 1. You can also use theDecrBymethod to decrement by a specific value:DecrBy(ctx, "count", 2)will decrementcountby 2. Note thatDecrcan only be used on integer values.IncrByFloat(ctx, "completion_rate", 1.5)incrementscompletion_rateby 1.5. Note that this function can be used on both integer and floating-point values.Incr(ctx, "duration")increases thedurationby 1. You can also use theIncrBymethod to increment by a specific value:IncrBy(ctx, "duration", 5)will incrementdurationby 5. Note thatIncrcan only be used on integer values.
- Finally, we retrieve the values to display the updated state.
Atomicity and Numeric Operations in Redis Using Go
