Redis管道儲存--簡單例子
阿新 • • 發佈:2019-01-25
首先獲取redis連線
//指定IP,埠,超時時間
Jedis j = new Jedis("127.0.0.1",6379,1000);
//選擇節點DB
j.select(1);
//開啟管道
Pipeline pipeline = j.pipelined();
/*List資料儲存,lpush key value value ... */
public void addRuleConditionToRedis(Pipeline pipeline) {
for(Map< String, Object> map : oldObjects){
pipeline.lpush("orule:" + "condi:" + map.get("ObjectRuleID").toString(),
map.get("ObjectRuleConditionID").toString());
}
//提交
pipeline.sync();
}
/*Hash資料儲存,hmset ket Map< String,String>*/
public void storeRuleCondition(List< Map< String, Object>> mapList) {
Pipeline pipeline = client.pipelined();
for(Map< String, Object> map : mapList){
Map< String,String> map2 = new HashMap<>();
map2.put("RuleDataType",map.get("RuleDataType").toString());
map2.put("LogicType",map.get("LogicType").toString());
pipeline. hmset("orule:condi-type:" + map.get("ObjectRuleConditionID").toString(),map2);
}
//提交
pipeline.sync();
}
/*Set資料儲存,sadd key value value ...*/
for(Map< String, Object> map : mapList){
pipeline.sadd("orule:condi_cont_dict:" +
map.get("ObjectRuleConditionID").toString(), dictId);
}
//提交
pipeline.sync();