Lesson 3
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:

  1. Increment and decrement numeric values.
  2. Modify numeric values using operations such as increments by a floating point.

Here's the code snippet that we'll be working with:

C++
1#include <iostream> 2#include <hiredis/hiredis.h> 3 4int main() { 5 // Connect to Redis 6 redisContext* context = redisConnect("127.0.0.1", 6379); 7 if (context == nullptr || context->err) { 8 if (context) { 9 std::cerr << "Error: " << context->errstr << std::endl; 10 redisFree(context); 11 } else { 12 std::cerr << "Can't allocate redis context" << std::endl; 13 } 14 return 1; 15 } 16 17 // Setting initial values 18 redisCommand(context, "SET count %d", 5); 19 redisCommand(context, "SET completion_rate %f", 95.5); 20 redisCommand(context, "SET duration %d", 0); 21 22 // Perform operations 23 redisCommand(context, "DECR count"); 24 redisCommand(context, "INCRBYFLOAT completion_rate 1.5"); 25 redisCommand(context, "INCR duration"); 26 27 // Retrieve and print values 28 redisReply* reply; 29 30 reply = (redisReply*)redisCommand(context, "GET count"); 31 std::cout << "Course count: " << reply->str << std::endl; 32 freeReplyObject(reply); 33 34 reply = (redisReply*)redisCommand(context, "GET completion_rate"); 35 std::cout << "Completion rate: " << reply->str << std::endl; 36 freeReplyObject(reply); 37 38 reply = (redisReply*)redisCommand(context, "GET duration"); 39 std::cout << "Duration: " << reply->str << std::endl; 40 freeReplyObject(reply); 41 42 // Cleanup 43 redisFree(context); 44 return 0; 45} 46// Expected Output: 47// Course count: 4 48// Completion rate: 97 49// Duration: 1

Let's break it down:

  • After setting initial values for count, completion_rate, and duration, we perform various operations:

    • The command DECR count decreases the value of count by 1. You can also provide a second argument to decrement by a specific value: DECRBY count 2 will decrement count by 2. DECR commands are used only for integer values.
    • The command INCRBYFLOAT completion_rate 1.5 increments completion_rate by 1.5. This method is versatile for both integer and floating-point values.
    • The command INCR duration increases duration by 1. This can be modified by a specific increment value: INCRBY duration 5 will increase duration by 5. INCR commands should also be used on integer values.
  • Finally, we retrieve the updated values using GET commands and print them to show the current stage.

Why It Matters

Understanding how to perform operations with numbers in Redis is essential for real-world applications. Imagine you're building a learning management system: you would track user progress, completion rates, and time spent on courses. Redis makes it fast and easy to update these numbers in real-time.

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!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.