Redis 批量查询 Pipeline


/**
* 批量获取redis中的信息(比如要获取名称)
* @param user
* @return
*/
public List<Map<String, Object>> getCachedUserInfo(List<Map<String, Object>> user){
CachedUserInfo userInfo = new CachedUserInfo();
try (Jedis jedis = RedisUtil.getInstance().getJedis();) {
Pipeline pipelined = jedis.pipelined();
HashMap<String, Response<String>> intrmMap = new HashMap<String, Response<String>>();
if (user != null && user.size() != 0){
for (int i = 0; i <user.size() ; i++) {
String key ="UserInfo:"+user.get(i).get("ruid").toString();
intrmMap.put(key,pipelined.get(key));//将你要批量传的key(比如uid)批量放到Pipeline管道中
}
}
pipelined.sync();//pipelined.syncAndReturnAll() 返回list<Object>
user.clear();
for (Map.Entry<String, Response<String>> entry :intrmMap.entrySet()) {
Response<String> sResponse = (Response<String>)entry.getValue();
// String key = new String(entry.getKey());
// String value = sResponse.get();
// System.out.println(value);
userInfo = JSONObject.parseObject(sResponse.get(), CachedUserInfo.class);
if (userInfo != null){
Map<String, Object> map = new HashMap<String, Object>();
map.put("name",userInfo.getName());
user.add(map);
}
}
}catch (Exception e){
e.printStackTrace();
}
return user;
}

批量插入同理,就不举例了根据自己业务需求修改,初学者有不足之处欢迎指正。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM