9

I am using RedisTemplate in my spring boot application and I am able to read using singleKey.

String valueJson = (String) redisTemplate.opsForValue().get(setKey(someId));

I have now a List of "someId" like "List someIds" and I want to get the data of all Ids. Of course I can iterate on the list and hit redis with indivisual keys, but I dont want that, instead I want provide the whole list to get the response in one go.

Please help.

1
  • Depending on the underlying data type, there are bulk commands like MGET, HMGET to read multiple keys (strings) or entries from a hash. Otherwise, like Thomas wrote, use pipelining to issue multiple commands and synchronize at the end. Commented Feb 1, 2017 at 15:53

1 Answer 1

12

You need to use pipelining: https://redis.io/topics/pipelining

List<Object> results = redisTemplate.executePipelined(
  new RedisCallback<Object>() {
    public Object doInRedis(RedisConnection connection) throws DataAccessException {
      StringRedisConnection stringRedisConn = (StringRedisConnection)connection;
      for(String id:someIds) 
        stringRedisConn.get(id);

    return null;
  }
});

Or in Java 8:

List<Object> results = redisTemplate.executePipelined((RedisCallback<Object>) connection -> {
    StringRedisConnection stringRedisConn = (StringRedisConnection) connection;

    someIds.forEach(id -> {
        stringRedisConn.get(id);
    });

    return null;
});

The results List will contain all that you want.

Sign up to request clarification or add additional context in comments.

1 Comment

Worth to not that, at the moment (ver 2.9.0), while trying to execute pipelined with a cluster, Jedis will throw: UnsupportedOperationException: Pipeline is currently not supported for JedisClusterConnection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.