1
This commit is contained in:
@@ -586,4 +586,54 @@ public class JDScheduleJob {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清理三个月前的Redis hash数据
|
||||||
|
* 清理JD订单拉取标记,避免Redis中积累过多无用数据
|
||||||
|
*/
|
||||||
|
@Scheduled(cron = "0 0 11 * * ?") // 每月1日的凌晨3点执行
|
||||||
|
public void cleanOldRedisHashData() {
|
||||||
|
try {
|
||||||
|
// 计算三个月前的时间点
|
||||||
|
LocalDateTime threeMonthsAgo = LocalDateTime.now().minusMonths(3);
|
||||||
|
|
||||||
|
// 获取所有以JD_REFRESH_TAG开头的键
|
||||||
|
Set<String> keys = redisTemplate.keys(JD_REFRESH_TAG + "*");
|
||||||
|
|
||||||
|
if (keys == null || keys.isEmpty()) {
|
||||||
|
logger.info("没有找到需要清理的Redis hash键");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int cleanedCount = 0;
|
||||||
|
|
||||||
|
// 遍历所有匹配的键
|
||||||
|
for (String key : keys) {
|
||||||
|
// 从键名中提取时间信息
|
||||||
|
// 键名格式类似于: jd:refresh:tag:appKey:2025-06-09 10:00:00
|
||||||
|
try {
|
||||||
|
String[] parts = key.split(":");
|
||||||
|
if (parts.length >= 6) {
|
||||||
|
// 重新组合时间部分
|
||||||
|
String timeString = parts[parts.length - 2] + ":" + parts[parts.length - 1];
|
||||||
|
LocalDateTime keyTime = LocalDateTime.parse(timeString, DATE_TIME_FORMATTER);
|
||||||
|
|
||||||
|
// 如果键的时间在三个月前,删除这个键
|
||||||
|
if (keyTime.isBefore(threeMonthsAgo)) {
|
||||||
|
redisTemplate.delete(key);
|
||||||
|
cleanedCount++;
|
||||||
|
logger.info("已清理过期的Redis hash键: {}", key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.warn("解析Redis键时间失败: {}", key, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("Redis hash数据清理完成,共清理 {} 个过期键", cleanedCount);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("清理三个月前的Redis hash数据时发生错误", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user