pipeline通道大幅度提升redis的处理速度
@Autowired
StringRedisTemplate stringRedisTemplate; //操作k-v都是字符串的
public void saveFaceLocationInfo(List<FaceLocation> faceLocationList) {
if (faceLocationList == null || faceLocationList.size() <= 0) {
return;
}
String currentTime = String.valueOf(System.currentTimeMillis());
//pipeline通道大幅度提升redis的处理速度
stringRedisTemplate.executePipelined(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
for (FaceLocation faceLocation : faceLocationList) {
String rKey = "new_"+faceLocation.getVnumber();
String flagValue = String.valueOf(connection.hGet(rKey.getBytes(), "flag".getBytes()));
//设备第一次上线、或者之前下线了,则对endtime更新
if(flagValue==null||"0".equals(flagValue)){
connection.hSet(rKey.getBytes(),"endtime".getBytes(),faceLocation.getTime().getBytes());
}
connection.hSet(rKey.getBytes(), "info".getBytes(), JSON.toJSONString(faceLocation).getBytes());
connection.hSet(rKey.getBytes(), "flag".getBytes(),"1".getBytes());
connection.hSet(rKey.getBytes(), "intime".getBytes(),currentTime.getBytes());
}
return null;
}
});
}