This commit is contained in:
2025-10-28 00:18:31 +08:00
parent 8905ce179c
commit 1c9c9cfa06
3 changed files with 203 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ package cn.van.business.controller.jd;
import cn.van.business.model.pl.TaobaoComment;
import cn.van.business.repository.TaobaoCommentRepository;
import cn.van.business.util.JDProductService;
import cn.van.business.util.JDScheduleJob;
import cn.van.business.util.JDUtil;
import cn.van.business.repository.CommentRepository;
import cn.van.business.model.pl.Comment;
@@ -32,13 +33,15 @@ public class JDInnerController {
private final JDProductService jdProductService;
private final JDUtil jdUtil;
private final JDScheduleJob jdScheduleJob;
private final CommentRepository commentRepository;
private final TaobaoCommentRepository taobaoCommentRepository;
@Autowired
public JDInnerController(JDProductService jdProductService, JDUtil jdUtil, CommentRepository commentRepository, TaobaoCommentRepository taobaoCommentRepository) {
public JDInnerController(JDProductService jdProductService, JDUtil jdUtil, JDScheduleJob jdScheduleJob, CommentRepository commentRepository, TaobaoCommentRepository taobaoCommentRepository) {
this.jdProductService = jdProductService;
this.jdUtil = jdUtil;
this.jdScheduleJob = jdScheduleJob;
this.commentRepository = commentRepository;
this.taobaoCommentRepository = taobaoCommentRepository;
}
@@ -381,6 +384,30 @@ public class JDInnerController {
}
}
/**
* 手动清理Redis中超过93天的旧数据
* 请求参数:{ skey }
* 返回:{ message, success }
*/
@PostMapping("/cleanRedisData")
public Object cleanRedisData(@RequestBody Map<String, Object> body) {
String skey = body.get("skey") != null ? String.valueOf(body.get("skey")) : null;
if (checkSkey(skey)) {
return error("invalid skey");
}
try {
logger.info("手动触发Redis清理任务");
jdScheduleJob.manualCleanOldRedisData();
JSONObject resp = new JSONObject();
resp.put("success", true);
resp.put("message", "Redis清理任务已执行完成详情请查看日志");
return resp;
} catch (Exception e) {
logger.error("cleanRedisData error", e);
return error("cleanRedisData failed: " + e.getMessage());
}
}
private static JSONObject error(String msg) {
JSONObject o = new JSONObject();

View File

@@ -28,7 +28,6 @@ import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
@@ -655,4 +654,74 @@ public void cleanOldRedisHashData() {
}
}
/**
* 清理tag:hash:时间 格式的Redis键按小时删除93天前的数据
* 可以手动调用或定时执行
*/
@Scheduled(cron = "0 0 3 * * ?") // 每天凌晨3点执行
public void cleanOldTagRedisData() {
try {
// 获取93天前的时间
LocalDateTime ninetyThreeDaysAgo = LocalDateTime.now().minusDays(93);
int deletedCount = 0;
logger.info("开始清理93天前的tag键数据截止时间{}", ninetyThreeDaysAgo.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
// 获取所有以"tag:"开头的键
Set<String> tagKeys = redisTemplate.keys("tag:*");
if (tagKeys != null && !tagKeys.isEmpty()) {
logger.info("找到 {} 个tag相关的键", tagKeys.size());
for (String key : tagKeys) {
try {
// 处理格式tag:hash值:YYYY-MM-DD HH
// 例如tag:01381d95e4936f1f3fe643bba2171894:2025-01-12 00
if (key.matches("tag:[a-f0-9]{32}:[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}")) {
// 提取时间部分(最后一个冒号之后)
String timePart = key.substring(key.lastIndexOf(":") + 1);
LocalDateTime time;
try {
// 解析为小时级别的时间格式 yyyy-MM-dd HH
time = LocalDateTime.parse(timePart + ":00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
} catch (DateTimeParseException e) {
logger.warn("无法解析Redis键时间{},时间部分:{}", key, timePart);
continue;
}
// 检查是否在93天前
if (time.isBefore(ninetyThreeDaysAgo)) {
redisTemplate.delete(key);
deletedCount++;
if (deletedCount % 100 == 0) {
logger.info("已删除 {} 个过期的tag键", deletedCount);
}
}
}
} catch (Exception e) {
logger.warn("解析Redis tag键时间失败{}", key, e);
}
}
logger.info("tag键清理完成共删除 {} 个过期键", deletedCount);
} else {
logger.info("未找到tag相关的键");
}
} catch (Exception e) {
logger.error("清理tag Redis数据时发生错误", e);
}
}
/**
* 手动执行清理方法(通过接口调用)
* 清理所有超过93天的tag键和jd:refresh:tag键
*/
public void manualCleanOldRedisData() {
logger.info("=== 手动触发Redis键清理 ===");
cleanOldTagRedisData();
cleanOldRedisHashData();
logger.info("=== Redis键清理完成 ===");
}
}