Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting multiple key values in single call form Jedis

Tags:

java

redis

jedis

I am trying to pass a list of keys to jedis and get their values in return.
mget operation does this but it does not return key value pair it gives all values as a list.
Is there any way we can know the key value pair in this query. OR it is confirmed the values returned in list are in same order as the keys.

List<String> lt =jedis.mget(mapArray);
            int j = 0;
            for( String key : mapArray) {
                System.out.println(key+" : "+lt.get(j));
                j++;
            }

Thanks

like image 202
viren Avatar asked Sep 03 '25 09:09

viren


1 Answers

There is no way to return the key:value pairs list, since Redis MGET command just returns a list of values.

But it is confirmed that the values are returned in the same order as you specified the list of keys, so you know that the first element returned in the list is the value for the first element you passed in the list, same for the second and so on.

You can check it not only in Redis MGET doc here:

https://redis.io/commands/mget#examples

but also in Jedis repository by taking a look at mget tests here:

https://github.com/xetorthio/jedis/blob/710ec9c824c6c333809dc7650e6b2084b2c24796/src/test/java/redis/clients/jedis/tests/commands/StringValuesCommandsTest.java#L35

like image 65
Averias Avatar answered Sep 04 '25 21:09

Averias