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:

import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;

public class RedisNumberOperations {

    public static void main(String[] args) {
        // Connect to Redis
        RedisClient redisClient = RedisClient.create("redis://localhost:6379/0");
        StatefulRedisConnection<String, String> connection = redisClient.connect();
        RedisCommands<String, String> commands = connection.sync();

        // Setting and getting string values
        commands.set("count", "5");
        commands.set("completion_rate", "95.5");
        commands.set("duration", "0");  // Ensure 'duration' is set initially

        commands.decr("count");
        commands.incrbyfloat("completion_rate", 1.5);
        commands.incr("duration");

        String count = commands.get("count");
        String completionRate = commands.get("completion_rate");
        String duration = commands.get("duration");

        System.out.println("Course count: " + count);
        System.out.println("Completion rate: " + completionRate);
        System.out.println("Duration: " + duration);

        // Closing the connection
        connection.close();
        redisClient.shutdown();
    }
}
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.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal