From 62cc493fa1da61a3c6cbd87c1826de15abc91743 Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Thu, 29 Jan 2026 18:28:40 +0800
Subject: [PATCH] 1
---
src/main/java/com/nq/utils/redis/RedisKeyUtil.java | 123 ++++++++++++++++++++++++++++++++++++++---
1 files changed, 114 insertions(+), 9 deletions(-)
diff --git a/src/main/java/com/nq/utils/redis/RedisKeyUtil.java b/src/main/java/com/nq/utils/redis/RedisKeyUtil.java
index 5920848..c20857f 100644
--- a/src/main/java/com/nq/utils/redis/RedisKeyUtil.java
+++ b/src/main/java/com/nq/utils/redis/RedisKeyUtil.java
@@ -1,17 +1,28 @@
package com.nq.utils.redis;
import com.alibaba.fastjson2.JSONObject;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.nq.enums.EStockType;
import com.nq.pojo.DataStockBean;
import com.nq.pojo.Stock;
import com.nq.pojo.StockRealTimeBean;
+import com.nq.utils.PropertiesUtil;
import com.nq.utils.stock.sina.StockApi;
import org.apache.http.util.TextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
import java.math.BigDecimal;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.Collections;
+import java.util.Map;
public class RedisKeyUtil {
@@ -28,8 +39,8 @@
/**
* 获取股票数据
* */
- public static DataStockBean getCacheBaseStock(Stock stock){
- String cacheBaseData = RedisShardedPoolUtils.get(RedisKeyConstant.RK_BASE_STOCK+":"+stock.getStockType()+":"+stock.getStockCode());
+ public static DataStockBean getCacheBaseStock(String stockType, String stockCode){
+ String cacheBaseData = RedisShardedPoolUtils.get(RedisKeyConstant.RK_BASE_STOCK+":"+stockType+":"+stockCode);
return new Gson().fromJson(cacheBaseData, DataStockBean.class);
}
@@ -51,18 +62,103 @@
stockRealTimeBean = new Gson().fromJson(cacheBaseData, StockRealTimeBean.class);
}
if(stockRealTimeBean == null){
+ String s = doPost(stock.getStockCode(), stock.getStockType());
+ Map<String, Object> stringObjectMap = jsonToMap(s);
stockRealTimeBean = new StockRealTimeBean();
- stockRealTimeBean.setPcp("0.00");
- stockRealTimeBean.setLast("0.00");
- stockRealTimeBean.setHigh("0.00");
- stockRealTimeBean.setLow("0.00");
- stockRealTimeBean.setBid("0.00");
- stockRealTimeBean.setPc("0.00");
- stockRealTimeBean.setAsk("0.00");
+ 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());
+ if (stringObjectMap.get("Ask") != null) {
+ stockRealTimeBean.setAsk(stringObjectMap.get("Ask").toString());
+ }
}
stockRealTimeBean.setPcp(stockRealTimeBean.getPcp().replace("%",""));
return stockRealTimeBean;
+ }
+
+ public static Map<String, Object> jsonToMap(String json) {
+ ObjectMapper objectMapper = new ObjectMapper();
+ try {
+ Object[] array = objectMapper.readValue(json, Object[].class);
+ Gson gson = new Gson();
+ String s = gson.toJson(array[0]);
+ Map<String, Object> map = objectMapper.readValue(s, Map.class);
+ return map;
+ } catch (JsonProcessingException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ // 首先查看你的RedisShardedPoolUtils有哪些方法
+ // 如果只有set和get,可以这样实现:
+
+ public static String doPost(String pid, String stockType) {
+ EStockType eStockType = EStockType.getEStockTypeByCode(stockType);
+ String apiUrl = eStockType.getStockUrl() + "stock?key=" + eStockType.getStockKey();
+ String result = null;
+
+ // 创建Redis限流的key
+ String redisKey = "limit:post:" + stockType + ":" + pid;
+
+ try {
+ // 尝试从Redis获取,如果存在则直接返回
+ String cachedResult = RedisShardedPoolUtils.get(redisKey);
+ if (cachedResult != null && !cachedResult.isEmpty() && !"ERROR".equals(cachedResult)) {
+ return cachedResult;
+ }
+
+ // 实际执行HTTP请求
+ result = executeHttpPost(apiUrl, pid);
+
+ // 将结果存入Redis
+ if (result != null && !result.isEmpty()) {
+ RedisShardedPoolUtils.set(redisKey, result);
+ RedisShardedPoolUtils.expire(redisKey, 5); // 设置1秒过期
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ // 异常处理
+ try {
+ RedisShardedPoolUtils.set(redisKey, "ERROR");
+ RedisShardedPoolUtils.expire(redisKey, 5);
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+ return result;
+ }
+
+ // HTTP请求执行方法
+ private static String executeHttpPost(String apiUrl, String pid) throws IOException {
+ URL url = new URL(apiUrl);
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+
+ connection.setRequestMethod("POST");
+ connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
+ connection.setDoOutput(true);
+
+ String postData = "pid=" + pid;
+
+ 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;
+ StringBuilder response = new StringBuilder();
+
+ while ((inputLine = in.readLine()) != null) {
+ response.append(inputLine);
+ }
+ in.close();
+
+ return response.toString();
}
@@ -80,6 +176,15 @@
return JSONObject.parseObject(companiesInfo);
}
+ /**
+ * 缓存K线数据源到redis
+ * */
+ public static void setCaCheStockKData(String stockType, String stockCode, Object kData){
+ RedisShardedPoolUtils.set(RedisKeyConstant.RK_STOCK_KDATA+":"+stockType+":"+stockCode, kData.toString());
+ }
+ public static Object getCaCheStockKData(String stockType, String stockCode){
+ return RedisShardedPoolUtils.get(RedisKeyConstant.RK_STOCK_KDATA+":"+stockType+":"+stockCode);
+ }
}
--
Gitblit v1.9.3