From 63769df44b9baf8339f6e0e151f4b6908386087c Mon Sep 17 00:00:00 2001
From: dd <gitluke@outlook.com>
Date: Fri, 26 Dec 2025 11:53:32 +0800
Subject: [PATCH] 1
---
src/main/java/com/nq/service/impl/PriceServicesImpl.java | 84 +++++++++++++++++++++++++++--------------
1 files changed, 55 insertions(+), 29 deletions(-)
diff --git a/src/main/java/com/nq/service/impl/PriceServicesImpl.java b/src/main/java/com/nq/service/impl/PriceServicesImpl.java
index 9dda805..b289c34 100644
--- a/src/main/java/com/nq/service/impl/PriceServicesImpl.java
+++ b/src/main/java/com/nq/service/impl/PriceServicesImpl.java
@@ -31,6 +31,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
@@ -110,39 +112,65 @@
}
}
- private static String lastResult = null;
- private static long lastExecuteTime = 0;
+ // 缓存条目类
+ private static class CacheEntry {
+ String result;
+ long lastExecuteTime;
+
+ CacheEntry(String result, long lastExecuteTime) {
+ this.result = result;
+ this.lastExecuteTime = lastExecuteTime;
+ }
+ }
+
+ // 按pid存储缓存条目
+ private static final Map<String, CacheEntry> cacheMap = new ConcurrentHashMap<>();
+
+ // 按pid存储锁对象,实现细粒度锁
+ private static final Map<String, Object> lockMap = new ConcurrentHashMap<>();
+
private static final long MIN_INTERVAL_MS = 3000;
- private static final Object cacheLock = new Object();
public static String doPost(String pid) {
long currentTime = System.currentTimeMillis();
- // 第一次快速检查(不加锁)
- if (lastResult != null && (currentTime - lastExecuteTime) < MIN_INTERVAL_MS) {
- return lastResult;
+ // 1. 快速检查缓存(无锁)
+ CacheEntry cached = cacheMap.get(pid);
+ if (cached != null && (currentTime - cached.lastExecuteTime) < MIN_INTERVAL_MS) {
+ return cached.result;
}
- // 同步块内再次检查并执行
- synchronized (cacheLock) {
+ // 2. 获取该pid对应的锁对象
+ Object pidLock = lockMap.computeIfAbsent(pid, k -> new Object());
+
+ // 3. 同步块内再次检查
+ synchronized (pidLock) {
currentTime = System.currentTimeMillis();
- if (lastResult != null && (currentTime - lastExecuteTime) < MIN_INTERVAL_MS) {
- return lastResult;
+ cached = cacheMap.get(pid);
+
+ if (cached != null && (currentTime - cached.lastExecuteTime) < MIN_INTERVAL_MS) {
+ return cached.result;
}
- // 执行POST请求
+ // 4. 执行POST请求
String newResult = doActualPost(pid);
- // 更新缓存
- lastResult = newResult;
- lastExecuteTime = System.currentTimeMillis();
+ // 5. 更新该pid的缓存
+ if (newResult != null) {
+ cacheMap.put(pid, new CacheEntry(newResult, System.currentTimeMillis()));
+ return newResult;
+ } else if (cached != null) {
+ // 请求失败,返回旧缓存
+ return cached.result;
+ }
- return newResult;
+ return "{\"error\":\"请求失败且无缓存数据\"}";
}
}
private static String doActualPost(String pid) {
- String apiUrl = PropertiesUtil.getProperty("JS_IN_HTTP_URL") + "stock?key=" + PropertiesUtil.getProperty("JS_IN_KEY");
+ String apiUrl = PropertiesUtil.getProperty("JS_IN_HTTP_URL") +
+ "stock?key=" + PropertiesUtil.getProperty("JS_IN_KEY");
try {
URL url = new URL(apiUrl);
@@ -162,24 +190,22 @@
}
// 读取响应
- BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- String inputLine;
- StringBuilder response = new StringBuilder();
+ try (BufferedReader in = new BufferedReader(
+ new InputStreamReader(connection.getInputStream()))) {
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
+ StringBuilder response = new StringBuilder();
+ String inputLine;
+ while ((inputLine = in.readLine()) != null) {
+ response.append(inputLine);
+ }
+
+ return response.toString();
}
- in.close();
-
- return response.toString();
} catch (Exception e) {
e.printStackTrace();
- // 返回缓存或错误信息
- if (lastResult != null) {
- return lastResult;
- }
- return "{\"error\":\"请求失败:" + e.getMessage() + "\"}";
+ // 不再直接返回旧缓存,由上层处理
+ return null;
}
}
--
Gitblit v1.9.3