Getting values with jedis pipeline

不问归期 提交于 2019-12-24 15:15:28

问题


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();

回答1:


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();
}


来源:https://stackoverflow.com/questions/30538085/getting-values-with-jedis-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!