From ed02adce53d9f51287e14764815006dd4d040daf Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Thu, 18 Jun 2026 21:25:46 +0800
Subject: [PATCH] 1

---
 src/main/java/com/nq/utils/redis/RedisKeyUtil.java |  168 +++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 124 insertions(+), 44 deletions(-)

diff --git a/src/main/java/com/nq/utils/redis/RedisKeyUtil.java b/src/main/java/com/nq/utils/redis/RedisKeyUtil.java
index 7e53315..04b3038 100644
--- a/src/main/java/com/nq/utils/redis/RedisKeyUtil.java
+++ b/src/main/java/com/nq/utils/redis/RedisKeyUtil.java
@@ -10,6 +10,7 @@
 import com.nq.pojo.StockRealTimeBean;
 import com.nq.utils.PropertiesUtil;
 import com.nq.utils.stock.sina.StockApi;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.http.util.TextUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -20,7 +21,10 @@
 import java.math.BigDecimal;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReentrantLock;
 
 
 public class RedisKeyUtil {
@@ -61,19 +65,44 @@
         }
         if(stockRealTimeBean == null){
             String s = doPost(stock.getStockCode());
-            Map<String, Object> stringObjectMap = jsonToMap(s);
-            stockRealTimeBean = new StockRealTimeBean();
-            stockRealTimeBean.setPcp(stringObjectMap.get("ChgPct").toString());
-            stockRealTimeBean.setLast(stringObjectMap.get("Last").toString());
-            stockRealTimeBean.setHigh(stringObjectMap.get("High").toString());
-            stockRealTimeBean.setLow(stringObjectMap.get("Low").toString());
-            stockRealTimeBean.setBid(stringObjectMap.get("Id").toString());
-            stockRealTimeBean.setPc(stringObjectMap.get("PrevClose").toString());
-            stockRealTimeBean.setAsk(stringObjectMap.get("Ask").toString());
+            if (StringUtils.isBlank(s)) {
+                stockRealTimeBean = buildFallbackRealTime(stock);
+            } else {
+                try {
+                    Map<String, Object> stringObjectMap = jsonToMap(s);
+                    stockRealTimeBean = new StockRealTimeBean();
+                    stockRealTimeBean.setPcp(stringObjectMap.get("ChgPct").toString());
+                    stockRealTimeBean.setLast(stringObjectMap.get("Last").toString());
+                    stockRealTimeBean.setHigh(stringObjectMap.get("High").toString());
+                    stockRealTimeBean.setLow(stringObjectMap.get("Low").toString());
+                    stockRealTimeBean.setBid(stringObjectMap.get("Id").toString());
+                    stockRealTimeBean.setPc(stringObjectMap.get("PrevClose").toString());
+                    stockRealTimeBean.setAsk(stringObjectMap.get("Ask").toString());
+                    stockRealTimeBean.setPid(stock.getStockCode());
+                } catch (Exception e) {
+                    log.warn("解析实时行情失败, code={}, err={}", stock.getStockCode(), e.getMessage());
+                    stockRealTimeBean = buildFallbackRealTime(stock);
+                }
+            }
         }
-        stockRealTimeBean.setPcp(stockRealTimeBean.getPcp().replace("%",""));
+        if (stockRealTimeBean.getPcp() != null) {
+            stockRealTimeBean.setPcp(stockRealTimeBean.getPcp().replace("%",""));
+        }
         return  stockRealTimeBean;
 
+    }
+
+    private static StockRealTimeBean buildFallbackRealTime(Stock stock) {
+        StockRealTimeBean bean = new StockRealTimeBean();
+        bean.setPid(stock.getStockCode());
+        bean.setLast("0");
+        bean.setHigh("0");
+        bean.setLow("0");
+        bean.setBid("0");
+        bean.setAsk("0");
+        bean.setPc("0");
+        bean.setPcp("0");
+        return bean;
     }
 
     public static Map<String, Object> jsonToMap(String json) {
@@ -89,60 +118,111 @@
         }
     }
 
+    // 缓存条目类
+    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, ReentrantLock> lockMap = new ConcurrentHashMap<>();
+
+    private static final long MIN_INTERVAL_MS = 3000;
+
     public static String doPost(String pid) {
-        // 从配置中获取 API URL,并拼接 key
-        String apiUrl = PropertiesUtil.getProperty("JS_IN_HTTP_URL") + "stock?key=" + PropertiesUtil.getProperty("JS_IN_KEY");
-        String result = null;
+        // 1. 快速检查缓存
+        long currentTime = System.currentTimeMillis();
+        CacheEntry cached = cacheMap.get(pid);
+
+        if (cached != null && (currentTime - cached.lastExecuteTime) < MIN_INTERVAL_MS) {
+            return cached.result;
+        }
+
+        // 2. 获取该pid对应的锁
+        ReentrantLock pidLock = lockMap.computeIfAbsent(pid, k -> new ReentrantLock());
+
+        // 3. 加锁执行
+        pidLock.lock();
+        try {
+            // 4. 双重检查
+            currentTime = System.currentTimeMillis();
+            cached = cacheMap.get(pid);
+
+            if (cached != null && (currentTime - cached.lastExecuteTime) < MIN_INTERVAL_MS) {
+                return cached.result;
+            }
+
+            // 5. 执行实际请求
+            String result = executePostRequest(pid);
+
+            // 6. 更新缓存
+            if (result != null) {
+                cacheMap.put(pid, new CacheEntry(result, System.currentTimeMillis()));
+            } else if (cached != null) {
+                // 请求失败,返回旧缓存
+                return cached.result;
+            }
+
+            return result;
+
+        } finally {
+            pidLock.unlock();
+        }
+    }
+
+    private static String executePostRequest(String pid) {
+        String apiUrl = PropertiesUtil.getProperty("JS_IN_HTTP_URL") +
+                "stock?key=" + PropertiesUtil.getProperty("JS_IN_KEY");
+        HttpURLConnection connection = null;
         try {
             URL url = new URL(apiUrl);
-            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+            connection = (HttpURLConnection) url.openConnection();
 
-            // 设置请求方法为 POST
             connection.setRequestMethod("POST");
-            // 设置请求头
             connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
-            connection.setDoOutput(true); // 允许向连接输出
+            connection.setDoOutput(true);
+            connection.setConnectTimeout(3000);
+            connection.setReadTimeout(5000);
 
-            // 构建 POST 数据
             String postData = "pid=" + pid;
 
-            // 发送 POST 请求
             try (OutputStream os = connection.getOutputStream()) {
                 byte[] input = postData.getBytes("utf-8");
                 os.write(input, 0, input.length);
             }
 
-            // 读取响应
-            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
-            String inputLine;
-            StringBuffer response = new StringBuffer();
+            try (BufferedReader in = new BufferedReader(
+                    new InputStreamReader(connection.getInputStream(), "utf-8"))) {
 
-            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();
-            result = response.toString();
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        return result;
-    }
 
+        } catch (Exception e) {
+            log.warn("拉取实时行情失败 pid={}, err={}", pid, e.getMessage());
+            return null;
+        } finally {
+            if (connection != null) {
+                connection.disconnect();
+            }
+        }
+    }
 
     public  static  void setCacheCompanies(Stock stock,String companiesInfo){
         RedisShardedPoolUtils.set(RedisKeyConstant.RK_COMPANY_INFO+":"+stock.getStockType()+":"+stock.getStockCode(),
                 new Gson().toJson(companiesInfo));
     }
-
-
-    public static JSONObject getCacheCompanies(Stock stock){
-       String  companiesInfo =  RedisShardedPoolUtils.get(RedisKeyConstant.RK_COMPANY_INFO+":"+stock.getStockType()+":"+stock.getStockCode());
-       if(companiesInfo.isEmpty()){
-           return  null;
-       }
-       return  JSONObject.parseObject(companiesInfo);
-    }
-
-
-
 }

--
Gitblit v1.9.3