Lesson 1
Managing User Data with Expiration in C++ Using Redis
Building a Redis-Based Backend with C++: A Recap

Now that you've gained valuable insights from previous lessons, it's time to put that knowledge into practice by building a robust Redis-based backend system using C++. In this unit, we'll walk you through the essential steps for implementation, starting with setting up the connection to the Redis server. We'll cover connecting to the server, handling potential errors, and go further to include setting a key with an expiration and retrieving that key.

What You’ll Implement

Utilize your experience to effectively manage user data through these essential operations:

  • Adding user data with an expiration time: Ensure that user data is stored temporarily, leveraging the expiration feature to automate cleanup.

  • Retrieving user data: Use this operation to access the stored user data when needed, maintaining efficiency and reliability in your backend system.

Here’s a quick example of how we will structure these operations using C++ and hiredis:

Example Code
C++
1#include <hiredis/hiredis.h> 2#include <iostream> 3#include <string> 4 5int main() { 6 // Connect to Redis server 7 redisContext* context = redisConnect("127.0.0.1", 6379); 8 if (context == NULL || context->err) { 9 if (context) { 10 std::cerr << "Error: " << context->errstr << std::endl; 11 redisFree(context); 12 } else { 13 std::cerr << "Can't allocate redis context" << std::endl; 14 } 15 return 1; 16 } 17 18 // Data to store 19 std::string userKey = "user:1"; 20 std::string email = "user1@example.com"; 21 22 // Add user data with expiration (1 day = 86400 seconds) 23 redisReply* reply = (redisReply*) redisCommand(context, "SETEX %s %d %s", userKey.c_str(), 86400, email.c_str()); 24 if (reply == NULL) { 25 std::cerr << "Command execution failed" << std::endl; 26 redisFree(context); 27 return 1; 28 } 29 freeReplyObject(reply); 30 31 // Retrieve user data 32 reply = (redisReply*) redisCommand(context, "GET %s", userKey.c_str()); 33 if (reply->type == REDIS_REPLY_STRING) { 34 std::cout << "Retrieved user data: " << reply->str << std::endl; 35 // Output: Retrieved user data: user1@example.com 36 } else { 37 std::cout << "No data found or an error occurred" << std::endl; 38 // Output when key expired: No data found or an error occurred 39 } 40 freeReplyObject(reply); 41 42 // Free the Redis context 43 redisFree(context); 44 45 return 0; 46}

In this example, we used hiredis, a minimalistic C client library for Redis, to connect to a Redis server, set user data with an expiration time, and retrieve it. The SETEX command is used to set a key with an expiration time in seconds.

Now that you have a clear idea of what you'll be building, let's start the practice section and implement this logic. This will solidify your understanding and prepare you for more complex tasks in the upcoming units.

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