From 9652bbc4a506f46986e298735b27f281ebf06124 Mon Sep 17 00:00:00 2001
From: dd <gitluke@outlook.com>
Date: Fri, 26 Dec 2025 11:39:16 +0800
Subject: [PATCH] 1
---
src/main/java/com/nq/service/impl/PriceServicesImpl.java | 105 ++++++++++++++++++++++++++++++++++++----------------
1 files changed, 73 insertions(+), 32 deletions(-)
diff --git a/src/main/java/com/nq/service/impl/PriceServicesImpl.java b/src/main/java/com/nq/service/impl/PriceServicesImpl.java
index f33d4db..9c3f47c 100644
--- a/src/main/java/com/nq/service/impl/PriceServicesImpl.java
+++ b/src/main/java/com/nq/service/impl/PriceServicesImpl.java
@@ -33,6 +33,8 @@
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
import com.fasterxml.jackson.databind.ObjectMapper;
@Service
@@ -109,58 +111,97 @@
}
}
- public String doGet(String pid){
- String apiUrl = PropertiesUtil.getProperty("JS_IN_HTTP_URL") + "stock?key=" + PropertiesUtil.getProperty("JS_IN_KEY");
- String result = null;
+ private static class CacheEntry {
+ String result;
+ long lastExecuteTime;
+ }
- try {
- // 添加请求间隔,避免频率过高
- Thread.sleep(1000); // 等待1秒再请求
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
+ private static final Map<String, CacheEntry> cacheMap = new ConcurrentHashMap<>();
+ private static final long MIN_INTERVAL_MS = 3000;
+ private static final Object globalLock = new Object();
+
+ public static String doGet(String pid) {
+ long currentTime = System.currentTimeMillis();
+
+ // 1. 先检查该pid是否有缓存
+ CacheEntry entry = cacheMap.get(pid);
+ if (entry != null && (currentTime - entry.lastExecuteTime) < MIN_INTERVAL_MS) {
+ return entry.result;
}
+
+ // 2. 使用双重检查锁定更新缓存
+ synchronized (getLockForPid(pid)) {
+ currentTime = System.currentTimeMillis();
+ entry = cacheMap.get(pid);
+
+ if (entry != null && (currentTime - entry.lastExecuteTime) < MIN_INTERVAL_MS) {
+ return entry.result;
+ }
+
+ // 3. 执行实际请求
+ String newResult = doActualPost(pid);
+
+ // 4. 更新该pid的缓存
+ CacheEntry newEntry = new CacheEntry();
+ newEntry.result = newResult;
+ newEntry.lastExecuteTime = System.currentTimeMillis();
+ cacheMap.put(pid, newEntry);
+
+ return newResult;
+ }
+ }
+
+ // 为每个pid提供独立的锁对象,避免不同pid间的阻塞
+ private static final Map<String, Object> pidLocks = new ConcurrentHashMap<>();
+
+ private static Object getLockForPid(String pid) {
+ return pidLocks.computeIfAbsent(pid, k -> new Object());
+ }
+
+
+ private static String doActualPost(String pid) {
+ String apiUrl = PropertiesUtil.getProperty("JS_IN_HTTP_URL") +
+ "stock?key=" + PropertiesUtil.getProperty("JS_IN_KEY");
try {
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- // 设置超时时间
- connection.setConnectTimeout(10000);
- connection.setReadTimeout(10000);
connection.setRequestMethod("POST");
- connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
+ connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
+ connection.setConnectTimeout(5000);
+ connection.setReadTimeout(10000);
- // JSON格式请求体
- String jsonInputString = "{\"pid\":\"" + pid + "\"}";
+ String postData = "pid=" + pid;
try (OutputStream os = connection.getOutputStream()) {
- byte[] input = jsonInputString.getBytes("utf-8");
+ byte[] input = postData.getBytes("utf-8");
os.write(input, 0, input.length);
}
- // 检查HTTP响应码
- int responseCode = connection.getResponseCode();
- if (responseCode == 429) {
- // 如果还是429,等待更长时间后重试
- Thread.sleep(3000);
- return doGet(pid); // 递归重试(注意设置最大重试次数)
- }
-
// 读取响应
- try (BufferedReader in = new BufferedReader(
- new InputStreamReader(connection.getInputStream()))) {
- String inputLine;
- StringBuilder response = new StringBuilder();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- result = response.toString();
+ BufferedReader in = new BufferedReader(
+ new InputStreamReader(connection.getInputStream()));
+ String inputLine;
+ StringBuilder response = new StringBuilder();
+
+ while ((inputLine = in.readLine()) != null) {
+ response.append(inputLine);
}
+ in.close();
+
+ return response.toString();
+
} catch (Exception e) {
e.printStackTrace();
+ // 如果请求失败,返回该pid的旧缓存(如果有)
+ CacheEntry oldEntry = cacheMap.get(pid);
+ if (oldEntry != null && oldEntry.result != null) {
+ return oldEntry.result;
+ }
+ return "{\"error\":\"请求失败:" + e.getMessage() + "\"}";
}
- return result;
}
@Override
--
Gitblit v1.9.3