Lesson 3
Operations with Numbers in Redis Using Java
Moving On to Operations with Numbers

Welcome back! Now that you've learned how to work with numbers in Redis using Java, it's time to build on that knowledge and explore some basic operations with these numbers. This lesson will show you how to perform operations like incrementing, decrementing, and modifying numeric values directly in Redis.

What You'll Learn

In this lesson, you will learn how to:

  1. Increment and decrement numeric values.
  2. Modify numeric values using operations such as increments by a floating point.
Code Snippet

Here's the code snippet that we'll be working with:

Java
1import io.lettuce.core.RedisClient; 2import io.lettuce.core.api.StatefulRedisConnection; 3import io.lettuce.core.api.sync.RedisCommands; 4 5public class RedisNumberOperations { 6 7 public static void main(String[] args) { 8 // Connect to Redis 9 RedisClient redisClient = RedisClient.create("redis://localhost:6379/0"); 10 StatefulRedisConnection<String, String> connection = redisClient.connect(); 11 RedisCommands<String, String> commands = connection.sync(); 12 13 // Setting and getting string values 14 commands.set("count", "5"); 15 commands.set("completion_rate", "95.5"); 16 commands.set("duration", "0"); // Ensure 'duration' is set initially 17 18 commands.decr("count"); 19 commands.incrbyfloat("completion_rate", 1.5); 20 commands.incr("duration"); 21 22 String count = commands.get("count"); 23 String completionRate = commands.get("completion_rate"); 24 String duration = commands.get("duration"); 25 26 System.out.println("Course count: " + count); 27 System.out.println("Completion rate: " + completionRate); 28 System.out.println("Duration: " + duration); 29 30 // Closing the connection 31 connection.close(); 32 redisClient.shutdown(); 33 } 34}
Let's Break It Down
  • After setting initial values for count, completion_rate, and duration, various operations are performed:

    • decr("count") decreases the value of count by 1. You can use decrby("count", 2) to decrement count by 2. Note that decr can only be used on integer values.
    • incrbyfloat("completion_rate", 1.5) increments completion_rate by 1.5. This method can be used on integers and floating-point values.
    • incr("duration") increases the duration by 1. You can use incrby("duration", 5) to increment duration by 5. Note that incr can only be used on integer values.
  • Finally, the values are retrieved to display the updated states with System.out.println.

It's important to note that the decr and incr operations are atomic, meaning they ensure thread-safe updates even when multiple clients modify the same key simultaneously. These operations can only be performed on keys containing valid integer values. If the key holds a non-integer value, Redis will throw an error. For incrbyfloat, while it supports floating-point arithmetic, be aware of potential precision issues common to floating-point operations due to how numbers are represented in memory.

Why It Matters

Understanding how to perform operations with numbers in Redis is essential for real-world applications. Imagine you're building a learning management system: you would track user progress, completion rates, and time spent on courses. Redis makes it fast and easy to update these numbers in real time.

By the end of this lesson, you'll be comfortable with basic numeric operations in Redis using Java, preparing you for more advanced tasks. Ready to get started? Let's dive into the practice section and enhance your Redis skills!

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