This commit is contained in:
雷欧(林平凡)
2025-09-09 11:16:52 +08:00
parent 41083d4519
commit c1fbe3bb4b
3 changed files with 94 additions and 102 deletions

View File

@@ -146,7 +146,7 @@ public class JDInnerController {
* ✅ 新增逻辑:先尝试从淘宝获取评论,但前提是 productTypeMapTB 存在对应映射
*/
String taobaoProductIdMap = tbMap.getOrDefault(productId, null);
if (taobaoProductIdMap != null && !taobaoProductIdMap.isEmpty()) {
logger.info("发现淘宝映射ID尝试从淘宝获取评论");
Comment taobaoComment = generateTaobaoComment(productType);
@@ -351,6 +351,7 @@ public class JDInnerController {
}
}
private static JSONObject error(String msg) {
JSONObject o = new JSONObject();
o.put("error", msg);

View File

@@ -28,10 +28,12 @@ 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;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
@@ -586,54 +588,56 @@ public class JDScheduleJob {
}
/**
* 清理三个月前的Redis hash数据
* 清理JD订单拉取标记避免Redis中积累过多无用数据
*/
@Scheduled(cron = "0 0 11 * * ?") // 每月1日的凌晨3点执行
public void cleanOldRedisHashData() {
try {
// 计算三个月前的时间
LocalDateTime threeMonthsAgo = LocalDateTime.now().minusMonths(3);
/**
* 清理三个月前的Redis hash数据
* 修复了时间解析异常的问题
*/
@Scheduled(cron = "0 20 11 * * ?") // 每月1日的凌晨3点执行
public void cleanOldRedisHashData() {
try {
// 获取三个月前的时间
LocalDateTime threeMonthsAgo = LocalDateTime.now().minusMonths(3);
// 获取所有以JD_REFRESH_TAG开头的键
Set<String> keys = redisTemplate.keys(JD_REFRESH_TAG + "*");
// 获取所有以JD_REFRESH_TAG开头的键
Set<String> keys = redisTemplate.keys(JD_REFRESH_TAG + "*");
if (keys == null || keys.isEmpty()) {
logger.info("没有找到需要清理的Redis hash键");
return;
}
int cleanedCount = 0;
// 遍历所有匹配的键
if (keys != null && !keys.isEmpty()) {
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);
// 提取时间部分,格式为 yyyy-MM-dd HH:mm:ss 或 HH:mm:ss
String timePart = key.substring(key.lastIndexOf(":") + 1);
// 如果键的时间在三个月前,删除这个键
if (keyTime.isBefore(threeMonthsAgo)) {
redisTemplate.delete(key);
cleanedCount++;
logger.info("已清理过期的Redis hash键: {}", key);
LocalDateTime time;
try {
// 尝试解析为完整的时间格式
time = LocalDateTime.parse(timePart);
} 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;
}
}
// 检查是否在三个月前
if (time.isBefore(threeMonthsAgo)) {
redisTemplate.delete(key);
logger.info("已删除过期的Redis键{}", key);
}
} catch (Exception e) {
logger.warn("解析Redis键时间失败: {}", key, e);
logger.warn("解析Redis键时间失败{}", key, e);
}
}
logger.info("Redis hash数据清理完成共清理 {} 个过期键", cleanedCount);
} catch (Exception e) {
logger.error("清理三个月前的Redis hash数据时发生错误", e);
}
} catch (Exception e) {
logger.error("清理Redis hash数据时发生错误", e);
}
}
}