Working with Numbers in Redis Using C++
Working with Numbers in Redis Using C++
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 ensure you're comfortable with establishing a connection to a Redis server.
What You'll Learn
In this lesson, you will learn how to:
- Set numeric values in Redis.
- Retrieve and print numeric values.
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 headers and establish a connection to the Redis server using
hiredis. - The
SETRedis command is used to store numeric values:countwith a value of5andcompletion_ratewith a value of95.5. These commands are sent usingredisCommand. - We retrieve these values with the
GETRedis command. The response is fetched into aredisReplyobject, and we check if the return type is a string before printing the value. Note that numeric values are retrieved as strings, so if you need to perform calculations, you will need to convert them into numeric types using appropriate conversion functions. freeReplyObjectis used to free the memory previously occupied by theredisReplyobject after each command execution.
