当要做操作日志跟踪时,实时一条条日志数据保存到数据库很消耗数据库的性能,
于是想到把日志缓存起来,积累到一定数量或过了一定时间 (就算数量不足),批量插入到数据库。
maven 配置
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1-jre</version>
</dependency>
java 代码
@Service
public class ActionLogServiceImpl extends BaseServiceImpl<ActionLog> implements ActionLogService {
volatile BlockingQueue<ActionLog> queue = new LinkedBlockingQueue<ActionLog>();
volatile boolean isRunning = true;
@PostConstruct
public void postConstruct() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (isRunning) {
try {
List<ActionLog> list = new ArrayList<>(1005);
Queues.drain(queue, list, 1000, 60 * 5, TimeUnit.SECONDS);
batchSave(list);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
});
thread.start();
}
@PreDestroy
public void destroy() {
isRunning = false;
List<ActionLog> list = new ArrayList<>(1005);
queue.drainTo(list);
batchSave(list);
}
public void batchSave(List<ActionLog> list) {
}
@Override
public void addActionLog(ActionLog actionLog) {
queue.add(actionLog);
}
}