Working with Numbers

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:

#include <boost/redis.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/consign.hpp>
#include <optional>
#include <iostream>

#include <boost/redis/src.hpp>

namespace net = boost::asio;
using boost::redis::connection;
using boost::redis::config;
using boost::redis::logger;
using boost::redis::request;
using boost::redis::response;

int main() {
    try {
        net::io_context ioc;

        // Create a shared connection object
        auto conn = std::make_shared<connection>(ioc.get_executor());

        config cfg; // defaults: 127.0.0.1:6379
        conn->async_run(cfg, logger{logger::level::disabled},
                        net::consign(net::detached, conn));

        // Prepare Redis commands to set and get numeric values
        request req;
        req.push("SET", "count", "5");
        req.push("SET", "completion_rate", "95.5");
        req.push("GET", "count");
        req.push("GET", "completion_rate");

        // SET returns "OK", GET returns bulk string (may be nil)
        response<std::string, std::string, std::optional<std::string>, std::optional<std::string>> resp;

        conn->async_exec(req, resp,
            [conn, &resp](boost::system::error_code ec, std::size_t) {
                if (!ec) {
                    std::cout << "SET count Response: " << std::get<0>(resp).value() << "\n";
                    std::cout << "SET completion_rate Response: " << std::get<1>(resp).value() << "\n";

                    // Retrieve and print the numeric values as strings
                    if (auto& count = std::get<2>(resp); count && count.value()) {
                        std::cout << "Course count: " << *count.value() << "\n";
                    } else {
                        std::cout << "Course count: (nil)\n";
                    }

                    if (auto& rate = std::get<3>(resp); rate && rate.value()) {
                        std::cout << "Completion rate: " << *rate.value() << "\n";
                    } else {
                        std::cout << "Completion rate: (nil)\n";
                    }
                } else {
                    std::cerr << "Error executing Redis commands: "
                              << ec.message() << "\n";
                }
                conn->cancel(); // stop background ops
            });

        ioc.run();
    } catch (const std::exception& e) {
        std::cerr << "Exception: " << e.what() << "\n";
        return 1;
    }
    return 0;
}
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