0
votes

I'm currently trying to monitor memory used by an embedded Redis in my Spring Boot app. I found the equivalent of the "info memory" command using RedisTemplate, giving general memory usage information. However, it didn't find a way to have more specific memory usage information related to a key which is the equivalent of using the command MEMORY USGAE with redis-cli. Is there a way to get this information programmatically using RedisTemplate ?

There is no redis installed locally on my desktop, and I don't have the rights to install it myself, that's why I have to use an embedded redis.

Best Regards,

1

1 Answers

1
votes

RedisTemplate does not have this API, you can use the EVAL command to get the output of this.

Key and Script

String script = "return redis.pcall('MEMORY', 'USAGE', KEYS[1])";
String key = "test";

Simple Using RedisConnectionUtils

    RedisConnection redisConnection =
        RedisConnectionUtils.getConnection(redisTemplate.getConnectionFactory());
    
    System.out.println(
        (Long)
            (redisConnection.eval(
                script.getBytes(StandardCharsets.UTF_8),
                ReturnType.INTEGER,
                1,
                key.getBytes(StandardCharsets.UTF_8))));
     RedisConnectionUtils.releaseConnection(
        redisConnection, redisTemplate.getConnectionFactory());

Using pipeline to get the same result.

System.out.println(
        redisTemplate
            .executePipelined(
                (RedisCallback<Object>)
                    connection -> {
                      connection.eval(
                          script.getBytes(StandardCharsets.UTF_8),
                          ReturnType.INTEGER,
                          1,
                          jobsKey.getBytes(StandardCharsets.UTF_8));
                      return null;
                    })
            .get(0));