Introduction to Redis Lists Using C++

Introduction to Redis Lists Using C++

Welcome back! In the previous lessons, we discussed connecting to Redis and performing operations with numbers using C++. Now, let's explore another essential Redis data structure: lists. Lists in Redis are an excellent way to store ordered collections of items, such as names, messages, or even tasks.

What You'll Learn

By the end of this lesson, you'll know how to:

  1. Use the RPUSH command to add items to a Redis list.
  2. Retrieve list items using the LRANGE command.

Here's a quick look at how you'll be working with lists in Redis using C++:

#include <iostream>
#include <hiredis/hiredis.h>
#include <vector>

int main() {
    // Connect to Redis
    redisContext* context = redisConnect("127.0.0.1", 6379);
    if (context == NULL || context->err) {
        if (context) {
            std::cerr << "Error: " << context->errstr << std::endl;
            redisFree(context);
        } else {
            std::cerr << "Can't allocate redis context" << std::endl;
        }
        return 1;
    }

    // Use RPUSH to add items to a Redis list
    redisReply* reply = static_cast<redisReply*>(redisCommand(context, "RPUSH students %s %s %s", "Alice", "Bob", "Charlie"));
    if (reply == NULL) {
        redisFree(context);
        return 1;
    }
    freeReplyObject(reply);

    // Retrieve list items using LRANGE
    reply = static_cast<redisReply*>(redisCommand(context, "LRANGE students 0 -1"));
    if (reply == NULL || reply->type != REDIS_REPLY_ARRAY) {
        redisFree(context);
        return 1;
    }

    // Print list items
    for (size_t i = 0; i < reply->elements; ++i) {
        std::cout << reply->element[i]->str << " ";
    }

    std::cout << std::endl;

    freeReplyObject(reply);
    redisFree(context);

    return 0;
}
// Expected Output:
// Alice Bob Charlie 

In this example:

  • The RPUSH command adds the names Alice, Bob, and Charlie to the list named students. The first argument is the list name, followed by the items to add.
    • Note that since Redis is a key-value store, if you run the same code multiple times, the list will keep growing with the same elements, as lists in Redis allow duplicates.
  • The LRANGE command retrieves all elements in the students list, and we print them out.
    • The LRANGE command takes the list name, a starting index, and an ending index as arguments. Here, we use 0 to indicate the first element and -1 to indicate the last element.
  • After retrieving the list items with the LRANGE command, we print each item in the list using a loop. The reply->elements gives us the total number of elements in the reply, and reply->element[i]->str accesses the string value of each element. This loop iterates through all elements, printing each one followed by a space. This is a straightforward way to display all items stored in the Redis list.
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