|
@@ -1,7 +1,6 @@
|
|
|
package cn.iocoder.yudao.module.infra.service.logger;
|
|
|
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
-import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
|
|
|
import cn.iocoder.yudao.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO;
|
|
|
import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogExportReqVO;
|
|
|
import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogPageReqVO;
|
|
@@ -13,6 +12,7 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
@@ -25,8 +25,6 @@ import java.util.List;
|
|
|
@Validated
|
|
|
public class ApiAccessLogServiceImpl implements ApiAccessLogService {
|
|
|
|
|
|
- private static final Integer DELETE_LIMIT = 100;
|
|
|
-
|
|
|
@Resource
|
|
|
private ApiAccessLogMapper apiAccessLogMapper;
|
|
|
|
|
@@ -47,22 +45,20 @@ public class ApiAccessLogServiceImpl implements ApiAccessLogService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- // TODO j-sentinel:类似 JobLogServiceImpl 的建议
|
|
|
- public void jobCleanAccessLog(Integer accessLogJobDay) {
|
|
|
- TenantUtils.executeIgnore(() -> {
|
|
|
- Integer result = null;
|
|
|
- int count = 0;
|
|
|
- while (result == null || DELETE_LIMIT.equals(result)) {
|
|
|
- result = apiAccessLogMapper.jobCleanAccessLog(accessLogJobDay);
|
|
|
- count += result;
|
|
|
- }
|
|
|
- if (count > 0) {
|
|
|
- // ALTER TABLE...FORCE 会导致表重建发生,这会根据主键索引对表空间中的物理页进行排序。
|
|
|
- // 它将行压缩到页面上并消除可用空间,同时确保数据处于主键查找的最佳顺序。
|
|
|
- apiAccessLogMapper.optimizeTable();
|
|
|
+ @SuppressWarnings("DuplicatedCode")
|
|
|
+ public Integer cleanAccessLog(Integer exceedDay, Integer deleteLimit) {
|
|
|
+ int count = 0;
|
|
|
+ LocalDateTime expireDate = LocalDateTime.now().minusDays(exceedDay);
|
|
|
+ // 循环删除,直到没有满足条件的数据
|
|
|
+ for (int i = 0; i < Short.MAX_VALUE; i++) {
|
|
|
+ int deleteCount = apiAccessLogMapper.deleteByCreateTimeLt(expireDate, deleteLimit);
|
|
|
+ count += deleteCount;
|
|
|
+ // 达到删除预期条数,说明到底了
|
|
|
+ if (deleteCount < deleteLimit) {
|
|
|
+ break;
|
|
|
}
|
|
|
- log.info("定时执行清理访问日志数量({})个", count);
|
|
|
- });
|
|
|
+ }
|
|
|
+ return count;
|
|
|
}
|
|
|
|
|
|
}
|