Search code examples
javaredispipelinejedis

Getting values with jedis pipeline


I have a list of ids that I want to use to retrieve hashes from a Redis server using the java client jedis. As mentioned in the documentation, Jedis provides a way to use the pipeline by declaring Response objects and then sync the pipeline to get values:

Pipeline p = jedis.pipelined();
p.set("fool", "bar"); 
p.zadd("foo", 1, "barowitch");  p.zadd("foo", 0, "barinsky"); p.zadd("foo", 0, "barikoviev");
Response<String> pipeString = p.get("fool");
Response<Set<String>> sose = p.zrange("foo", 0, -1);
p.sync(); 

However, my list has a variable length that keeps changing every few minutes. Thus, I am not able to predict the number of Response objects that I need to declare. Is there a way to get around that, something like:

Pipeline p = jedis.pipelined();
Response<List<List<Map<String,String>>> records;
for (int id: ids)
    records.add(p.hgetAll(id))
p.sync();

Solution

  • I guess what you want to achive is done like this.

    List<Response> responses = new ArrayList<>();
    
    Pipeline p = jedis.pipelined();
    for (int id: ids) {
    responses .add(p.get(id));
    }
    p.sync();
    
    for(Reponse response : responses){
    Object o = response.get();
    }