Welcome back to our Redis course! Now that you know how to connect to a Redis server using Java, 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 with establishing a connection to a Redis server using the Lettuce API.
In this lesson, you will learn how to:
- Set numeric values in Redis using Java.
- Retrieve numeric values using the Lettuce API.
It's important to note that in Redis, numbers are stored and handled as strings because Redis inherently treats all data as string values. While this allows Redis to store numeric and non-numeric data uniformly, it also means numeric values need to be explicitly parsed into their appropriate types (e.g., int
, double
) in your application code if you want to perform arithmetic or logical operations.
Here's the code snippet that we'll be working with:
Java1import io.lettuce.core.RedisClient; 2import io.lettuce.core.api.StatefulRedisConnection; 3import io.lettuce.core.api.sync.RedisCommands; 4 5public class RedisNumericExample { 6 public static void main(String[] args) { 7 // Connect to Redis 8 RedisClient redisClient = RedisClient.create("redis://localhost:6379/0"); 9 StatefulRedisConnection<String, String> connection = redisClient.connect(); 10 RedisCommands<String, String> syncCommands = connection.sync(); 11 12 // Setting and getting string values 13 syncCommands.set("count", "5"); 14 syncCommands.set("completion_rate", "95.5"); 15 16 String count = syncCommands.get("count"); 17 String completionRate = syncCommands.get("completion_rate"); 18 19 System.out.println("Course count: " + count); 20 System.out.println("Completion rate: " + completionRate); 21 22 // Closing the connection 23 connection.close(); 24 redisClient.shutdown(); 25 } 26}
Let's break down the code:
- We use the Lettuce API to import necessary classes such as
RedisClient
,StatefulRedisConnection
, andRedisCommands
. - We establish a connection to the Redis server with
RedisClient.create
using the URL formatredis://localhost:6379/0
, where0
represents the database number. - Through this connection, we obtain a synchronous command API,
syncCommands
, to interact with the Redis server. - We use the
set
method to store the numeric values:count
with a value of"5"
(as a string) andcompletion_rate
with a value of"95.5"
(also as a string). - We retrieve these values using the
get
method. Since Redis stores and returns strings, numbers are also handled as strings in Redis. We print the retrieved values directly. - Finally, we close the connection and shut down the client using
connection.close()
andredisClient.shutdown()
to ensure that resources are released properly.
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 using Java!