1
This commit is contained in:
@@ -592,7 +592,7 @@ public class JDScheduleJob {
|
|||||||
* 清理三个月前的Redis hash数据
|
* 清理三个月前的Redis hash数据
|
||||||
* 修复了时间解析异常的问题
|
* 修复了时间解析异常的问题
|
||||||
*/
|
*/
|
||||||
@Scheduled(cron = "0 20 11 * * ?") // 每月1日的凌晨3点执行
|
@Scheduled(cron = "0 45 11 * * ?") // 每月1日的凌晨3点执行
|
||||||
public void cleanOldRedisHashData() {
|
public void cleanOldRedisHashData() {
|
||||||
try {
|
try {
|
||||||
// 获取三个月前的时间
|
// 获取三个月前的时间
|
||||||
@@ -604,25 +604,30 @@ public void cleanOldRedisHashData() {
|
|||||||
if (keys != null && !keys.isEmpty()) {
|
if (keys != null && !keys.isEmpty()) {
|
||||||
for (String key : keys) {
|
for (String key : keys) {
|
||||||
try {
|
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;
|
LocalDateTime time;
|
||||||
try {
|
try {
|
||||||
// 尝试解析为完整的时间格式
|
// 解析为完整的时间格式 yyyy-MM-dd HH:mm:ss
|
||||||
time = LocalDateTime.parse(timePart);
|
time = LocalDateTime.parse(timePart, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||||
} catch (DateTimeParseException e) {
|
} catch (DateTimeParseException e) {
|
||||||
// 如果解析失败,尝试解析为时间部分
|
logger.warn("无法解析Redis键时间:{},时间部分:{}", key, timePart);
|
||||||
if (timePart.contains(":")) {
|
continue;
|
||||||
// 假设是HH:mm:ss格式
|
|
||||||
LocalTime localTime = LocalTime.parse(timePart);
|
|
||||||
// 使用当前日期和解析的时间创建LocalDateTime
|
|
||||||
time = LocalDateTime.of(LocalDate.now(), localTime);
|
|
||||||
} else {
|
|
||||||
// 如果无法解析,跳过这个键
|
|
||||||
logger.warn("无法解析Redis键时间:{}", key);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否在三个月前
|
// 检查是否在三个月前
|
||||||
|
|||||||
Reference in New Issue
Block a user