项目启动后加载预处理数据
@Component
public class DataInit implements ApplicationRunner {
@Autowired
private UserMapper userMapper;
@Autowired
private RedisTemplate redisTemplate;
private ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("thread-call-runner-%d").build();
/**
* 避免使用Executors.newCachedThreadPool() / Executors.XXXXXXX
* 的方式,避免内存溢出
*
*/
private ExecutorService taskExe = new ThreadPoolExecutor(10, 20,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(),
namedThreadFactory);
@Override
public void run(ApplicationArguments args) throws Exception {
List<User> users=userMapper.queryAll();
//假设有100万条数据,分十个线程池执行,每个线程执行十万条
// 计算循环次数
int listCount = users.size() / 100000;
// 计算出余数
int remaider = users.size() % listCount;
// 根据余数确定次数,如果余数大于0,加一次
if(remaider>0){
listCount+=1;
}
for(int i=0;i<listCount;i++){
List<User> value;
if(i==listCount-1){
value = users.subList(i * 100000, users.size());
}else{
value = users.subList(i * 100000, (i + 1) * 100000 );
}
taskExe.execute(()->{
value.forEach(user->{
redisTemplate.opsForValue().set("userId:"+user.getUserId(),user,Duration.ofDays(5));
});
});
}
taskExe.shutdown();
}