Working with Numbers

Welcome back to our Redis course! Now that you know how to connect to a Redis server, it's time to move forward and explore how to work with numbers in Redis. This unit builds on our previous lesson, so make sure you're comfortable with establishing a connection to a Redis server.

What You'll Learn

In this lesson, you will learn how to:

  1. Store numeric values in Redis using C++ and Boost.Redis.
  2. Retrieve numeric values from Redis and handle them as strings in C++.

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

Let's Break Down the Code
  • As in the previous lesson, we first include the necessary Boost.Redis and Boost.Asio headers and set up the connection to the Redis server.
  • We use the request object to queue up Redis commands. Here, we use "SET" to store the numeric values "5" and "95.5" under the keys "count" and "completion_rate", respectively. Note that values are sent as strings, which is how Redis stores all data.
  • We then use "GET" to retrieve the values for both keys.
  • The response object is set up to receive the responses from Redis. The results of the "GET" commands are stored as std::optional<std::string>, since the key might not exist.
  • In the callback, we check for errors, then print the responses. If the value exists, we print it; otherwise, we print (nil).
Summary

Working with numbers in Redis is crucial because many real-world applications involve numeric data. From tracking user statistics to monitoring system performance, managing numbers in Redis allows you to perform a variety of useful operations efficiently. By mastering these basic operations with numbers, you'll be well prepared to tackle more complex tasks and optimize your applications.

Ready to dive in? Let's move on to the practice section and get hands-on experience working with numbers in Redis!

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