通过Jedis Pipeline获得价值 [英] Getting values with jedis pipeline

查看:468
本文介绍了通过Jedis Pipeline获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ID列表,我想使用它们使用Java客户端jedis从Redis服务器中检索哈希.如文档中所述,Jedis提供了一种通过声明Response对象来使用管道的方法,然后同步管道以获取值:

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

但是,我的列表的长度可变,每隔几分钟就会变化一次.因此,我无法预测需要声明的Response对象的数量.有没有办法解决这个问题,像这样:

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

推荐答案

我想您要达到的目标是这样完成的.

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

这篇关于通过Jedis Pipeline获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆