This commit is contained in:
雷欧(林平凡)
2025-09-09 11:39:09 +08:00
parent c1fbe3bb4b
commit 473e305bb7

View File

@@ -592,7 +592,7 @@ public class JDScheduleJob {
* 清理三个月前的Redis hash数据
* 修复了时间解析异常的问题
*/
@Scheduled(cron = "0 20 11 * * ?") // 每月1日的凌晨3点执行
@Scheduled(cron = "0 45 11 * * ?") // 每月1日的凌晨3点执行
public void cleanOldRedisHashData() {
try {
// 获取三个月前的时间
@@ -604,25 +604,30 @@ public void cleanOldRedisHashData() {
if (keys != null && !keys.isEmpty()) {
for (String key : keys) {
try {
// 提取时间部分,格式为 yyyy-MM-dd HH:mm:ss 或 HH:mm:ss
String timePart = key.substring(key.lastIndexOf(":") + 1);
// 提取时间部分,处理两种格式:
// 1. jd:refresh:tag:hash值:2025-02-02 16:00:00
// 2. jd:refresh:tag:2024-11-30 09:26:00
String timePart;
// 检查是否包含hash值32位十六进制字符串
if (key.matches("jd:refresh:tag:[a-f0-9]{32}:[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}")) {
// 格式jd:refresh:tag:hash值:时间
timePart = key.substring(key.lastIndexOf(":") + 1);
} else if (key.matches("jd:refresh:tag:[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}")) {
// 格式jd:refresh:tag:时间
timePart = key.substring("jd:refresh:tag:".length());
} else {
logger.warn("无法识别Redis键格式{}", key);
continue;
}
LocalDateTime time;
try {
// 尝试解析为完整的时间格式
time = LocalDateTime.parse(timePart);
// 解析为完整的时间格式 yyyy-MM-dd HH:mm:ss
time = LocalDateTime.parse(timePart, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
} catch (DateTimeParseException e) {
// 如果解析失败,尝试解析为时间部分
if (timePart.contains(":")) {
// 假设是HH:mm:ss格式
LocalTime localTime = LocalTime.parse(timePart);
// 使用当前日期和解析的时间创建LocalDateTime
time = LocalDateTime.of(LocalDate.now(), localTime);
} else {
// 如果无法解析,跳过这个键
logger.warn("无法解析Redis键时间{}", key);
continue;
}
logger.warn("无法解析Redis键时间{},时间部分:{}", key, timePart);
continue;
}
// 检查是否在三个月前