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:

  1. Increment and decrement numeric values.
  2. Modify numeric values using operations such as increments by a floating point.
Example Code: Performing Numeric Operations in Redis with C++ and Boost.Redis

Here is a C++ code snippet that demonstrates how to perform these operations using Boost.Redis:

Breaking Down the Code
  • Setting Initial Values:
    The code first sets initial values for count, completion_rate, and duration using the SET command.

  • Performing Numeric Operations:

    • DECR count decreases the value of count by 1 and returns the new value as std::int64_t.
      You can also decrement by a specific value using DECRBY count 2 to decrease it by 2.
    • INCRBYFLOAT completion_rate 1.5 increases completion_rate by 1.5 and returns the new value as double.
      This command works with both integer and floating-point values.
    • INCR duration increases the value of duration by 1 and returns the new value as std::int64_t.
      You can increment by a specific value using INCRBY duration 5 to increase it by 5.
  • Retrieving Updated Values: The code then retrieves the updated values for , , and using the command.

Summary

Understanding how to perform operations with numbers in Redis is essential for real-world applications. For example, if you are building a learning management system, you might need to track user progress, completion rates, and time spent on courses. Redis makes it fast and easy to update these numbers in real time.

By using type-safe decoders for numeric operations, you leverage Boost.Redis's compile-time type checking, ensuring that your code handles integers and floating-point values correctly without manual string conversions.

By the end of this lesson, you'll be comfortable with basic numeric operations in Redis, preparing you for more advanced tasks. Ready to get started? Let's dive into the practice section and enhance your Redis skills!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal