有个表的数据需初始化到redis, 目前18w+, 后期会达到千万
1. 使用redisTemplate批量存储数据
@Autowired
private RedisTemplate redisTemplate;
public void init(){
List<WxPois> list = SpringUtils.getBean(WxPoisMapper.class).selectWxPoisList(new WxPois());
if (CollectionUtils.isEmpty(list)) {
return;
}
redisTemplate.executePipelined(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
for (WxPois wxPois : list) {
redisTemplate.opsForValue().set("pois:"+wxPois.getId(),wxPois);
}
return null;
}
});
log.info("初始化客源成功");
}
执行结果
20:33:48.029 [restartedMain] DEBUG c.r.r.p.m.W.selectWxPoisList - [debug,137] - <== Total: 182935
20:34:13.498 [restartedMain] INFO c.r.r.init.InitData - [init,42] - 初始化客源成功
182935条数据耗时 25秒