Managing User Expiration

Managing User Data with Expiration

Welcome to the first step in building our Redis-based backend system. In this unit, we will focus on how to manage user data with expiration using Redis. This is a fundamental part of our project that will set the stage for more advanced features in later units.

What You'll Build

Let's take a quick look at what you'll be building in this unit. We will focus on two key operations for managing user data:

  1. Adding user data with an expiration time: This will ensure that user data is stored for a limited period and is automatically deleted afterward.
  2. Retrieving user data: This operation will help us fetch the user data we previously stored.

Here's a quick example of how we will structure these operations:

C++
#include <boost/redis.hpp>
#include <boost/redis/src.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/consign.hpp>
#include <nlohmann/json.hpp>
#include <iostream>
#include <memory>

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

int main() {
    net::io_context ioc;
    auto conn = std::make_shared<connection>(ioc);

    // Start Redis connection
    conn->async_run(
        config{},
        logger{logger::level::disabled},
        net::consign(net::detached, conn)
    );

    // Create user data as JSON
    json data;
    data["email"] = "user1@example.com";

    // SET user:1 <json> EX 86400 (1 day)
    auto set_req  = std::make_shared<request>();
    auto set_resp = std::make_shared<response<std::string>>();
    set_req->push("SET", "user:1", data.dump(), "EX", "86400");

    conn->async_exec(
        *set_req, *set_resp,
        [conn, set_req, set_resp](boost::system::error_code ec, std::size_t) {
            if (ec) {
                std::cerr << "SET error: " << ec.message() << "\n";
                conn->cancel();
                return;
            }

            // After SET completes, run GET user:1
            auto get_req  = std::make_shared<request>();
            auto get_resp = std::make_shared<response<std::string>>();
            get_req->push("GET", "user:1");

            conn->async_exec(
                *get_req, *get_resp,
                [conn, get_req, get_resp](boost::system::error_code ec, std::size_t) {
                    if (!ec) {
                        auto& res = std::get<0>(*get_resp); // result<std::string, error>
                        if (res) {
                            std::cout << res.value() << std::endl;
                        }
                    } else {
                        std::cerr << "GET error: " << ec.message() << "\n";
                    }
                    conn->cancel();
                }
            );
        }
    );

    ioc.run();
    return 0;
}

You might remember the EX parameter from the previous lessons. It allows us to set an expiration time for the data we store in Redis. In this case, we are setting the expiration to 86400 seconds (one day) by passing "EX", "86400" to the SET command.

Notice how we chain the operations: first we execute the SET command with expiration, and once it completes successfully, we execute the GET command to retrieve the data. The ioc.run() call at the end processes all asynchronous operations until they complete.

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