package com.nq.utils.redis; import com.alibaba.fastjson2.JSONObject; 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.stock.sina.StockApi; import org.apache.http.util.TextUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.math.BigDecimal; public class RedisKeyUtil { private static final Logger log = LoggerFactory.getLogger(RedisKeyUtil.class); /** * 缓存股票数据源到redis * */ public static void setCaCheKeyBaseStock(EStockType eStockType, DataStockBean dataStockBean){ RedisShardedPoolUtils.set(RedisKeyConstant.RK_BASE_STOCK+":"+eStockType.getCode()+":"+dataStockBean.getId(), new Gson().toJson(dataStockBean)); } /** * 获取股票数据 * */ public static DataStockBean getCacheBaseStock(Stock stock){ String cacheBaseData = RedisShardedPoolUtils.get(RedisKeyConstant.RK_BASE_STOCK+":"+stock.getStockType()+":"+stock.getStockCode()); return new Gson().fromJson(cacheBaseData, DataStockBean.class); } /** * 保存实时数据到redis * */ public static void setCacheRealTimeStock(EStockType eStockType, StockRealTimeBean stockRealTimeBean){ RedisShardedPoolUtils.set(RedisKeyConstant.RK_REAL_TIME_STOCK+":"+eStockType.getCode()+":"+stockRealTimeBean.getPid(), new Gson().toJson(stockRealTimeBean)); } public static StockRealTimeBean getCacheRealTimeStock(Stock stock){ StockRealTimeBean stockRealTimeBean = null; String cacheBaseData = RedisShardedPoolUtils.get(RedisKeyConstant.RK_REAL_TIME_STOCK+":"+stock.getStockType()+":"+stock.getStockCode()); if(!TextUtils.isEmpty(cacheBaseData)){ stockRealTimeBean = new Gson().fromJson(cacheBaseData, StockRealTimeBean.class); } if(stockRealTimeBean == null){ 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(stockRealTimeBean.getPcp().replace("%","")); return stockRealTimeBean; } public static void setCacheCompanies(Stock stock,String companiesInfo){ RedisShardedPoolUtils.set(RedisKeyConstant.RK_REAL_TIME_STOCK+":"+stock.getStockType()+":"+stock.getStockCode(), new Gson().toJson(companiesInfo)); } public static JSONObject getCacheCompanies(Stock stock){ String companiesInfo = RedisShardedPoolUtils.get(RedisKeyConstant.RK_REAL_TIME_STOCK+":"+stock.getStockType()+":"+stock.getStockCode()); if(companiesInfo.isEmpty()){ return null; } return JSONObject.parseObject(companiesInfo); } }