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 establishing a connection to a Redis server.
In this lesson, you will learn how to:
- Set numeric values in Redis.
- Retrieve and log numeric values.
Here's the code snippet that we'll be working with:
Let's break down the code:
- We use modern ES6
import
syntax to import thecreateClient
function from theredis
library. - The Redis client is created with
createClient
and connected to the Redis server at'redis://localhost:6379'
. - The client is set to log any connection errors.
- The connection to the Redis server is established using
await client.connect()
. - We set numeric values using the
set
method:count
with a value of5
andcompletion_rate
with a value of95.5
. - We retrieve these values using the
get
method. Note that in JavaScript, the return type of theget
method is a string, so there's no need to decode it. - Finally, the client disconnects from the Redis server using
await client.disconnect()
.
Working with numbers in Redis is crucial because many real-world applications involve numeric data. From tracking user statistics to monitoring system performance, managing numbers in Redis allows you to perform a variety of useful operations efficiently. By mastering these basic operations with numbers, you'll be well-prepared to tackle more complex tasks and optimize your applications.
Ready to dive in? Let's move on to the practice section and get hands-on experience working with numbers in Redis!
