命中缓存,更新 responseId

This commit is contained in:
wangxiu 2026-07-10 23:44:58 +08:00
parent 8eaaa501bc
commit ba38fbf0cc
1 changed files with 21 additions and 1 deletions

View File

@ -1,9 +1,13 @@
package com.lyxh.api.docking.service; package com.lyxh.api.docking.service;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.lyxh.api.docking.client.ApiCallResult; import com.lyxh.api.docking.client.ApiCallResult;
import com.lyxh.api.docking.client.GenericApiClient; import com.lyxh.api.docking.client.GenericApiClient;
import com.lyxh.api.docking.entity.ApiCacheData; import com.lyxh.api.docking.entity.ApiCacheData;
import com.lyxh.api.docking.entity.ThirdPartyConfig; import com.lyxh.api.docking.entity.ThirdPartyConfig;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -11,6 +15,7 @@ import org.springframework.stereotype.Service;
import java.time.Duration; import java.time.Duration;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.UUID;
import java.util.function.Predicate; import java.util.function.Predicate;
/** /**
@ -32,6 +37,9 @@ public class GenericApiService {
@Autowired @Autowired
private ApiInvokeLogService invokeLogService; private ApiInvokeLogService invokeLogService;
@Autowired
private ObjectMapper objectMapper;
/** /**
* 执行第三方 API 调用(含缓存、日志),返回响应体原始 JSON * 执行第三方 API 调用(含缓存、日志),返回响应体原始 JSON
* *
@ -65,7 +73,19 @@ public class GenericApiService {
ApiCacheData cached = cacheDataService.getNonExpiredCache(apiCode, cacheKey, config.getId()); ApiCacheData cached = cacheDataService.getNonExpiredCache(apiCode, cacheKey, config.getId());
if (cached != null) { if (cached != null) {
log.info("Cache hit: weOrderNo={} apiCode={} cacheKey={}", weOrderNo, apiCode, cacheKey); log.info("Cache hit: weOrderNo={} apiCode={} cacheKey={}", weOrderNo, apiCode, cacheKey);
return cached.getCacheValue(); // 命中缓存,更新 responseId
String cacheValue = cached.getCacheValue();
try {
JsonNode root = objectMapper.readTree(cacheValue);
if (root.has("responseId")) {
String newResponseId = DigestUtils.md5Hex(UUID.randomUUID().toString());
((ObjectNode) root).put("responseId", newResponseId);
return objectMapper.writeValueAsString(root);
}
} catch (Exception e) {
log.warn("Failed to replace responseId in cached response: weOrderNo={}", weOrderNo, e);
}
return cacheValue;
} }
log.info("Cache miss: weOrderNo={} apiCode={} cacheKey={}", weOrderNo, apiCode, cacheKey); log.info("Cache miss: weOrderNo={} apiCode={} cacheKey={}", weOrderNo, apiCode, cacheKey);
} }