接口代码优化调整

This commit is contained in:
Achilles
2024-01-09 16:51:01 +08:00
parent 53b4223370
commit c24f3a86fb

View File

@@ -6,8 +6,11 @@ import cn.stock.market.domain.basic.repository.MoneyStockRepository;
import cn.stock.market.dto.StockHistoryRequest; import cn.stock.market.dto.StockHistoryRequest;
import cn.stock.market.dto.StockHistoryResponse; import cn.stock.market.dto.StockHistoryResponse;
import cn.stock.market.infrastructure.db.po.QMoneyStockPO; import cn.stock.market.infrastructure.db.po.QMoneyStockPO;
import cn.stock.market.utils.RequestCacheUtils;
import cn.stock.market.utils.ServerResponse; import cn.stock.market.utils.ServerResponse;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
@@ -36,6 +39,7 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@@ -499,11 +503,17 @@ public class MoneyApiController {
@ResponseBody @ResponseBody
public List<MoneyStockSuggestDTO> getTopGainer(@RequestParam String stockType) { public List<MoneyStockSuggestDTO> getTopGainer(@RequestParam String stockType) {
List<MoneyStockSuggestDTO> moneyStockSuggestDTOS = null; List<MoneyStockSuggestDTO> moneyStockSuggestDTOS = null;
// 尝试从缓存中获取结果
moneyStockSuggestDTOS = gainerStockSuggestCache.getIfPresent(stockType);
if (moneyStockSuggestDTOS == null) {
// 缓存未命中,执行业务查询
if (StringUtils.equals(stockType, "nse")) { if (StringUtils.equals(stockType, "nse")) {
moneyStockSuggestDTOS = nseGainer(); moneyStockSuggestDTOS = nseGainer();
} else if (StringUtils.equals(stockType, "bse")) { } else if (StringUtils.equals(stockType, "bse")) {
moneyStockSuggestDTOS = bseGainer(); moneyStockSuggestDTOS = bseGainer();
} }
moneyStockSuggestDTOS = moneyStockSuggestDTOS.stream().filter(f->StringUtils.isNotBlank(f.getStockName())).collect(Collectors.toList()); moneyStockSuggestDTOS = moneyStockSuggestDTOS.stream().filter(f->StringUtils.isNotBlank(f.getStockName())).collect(Collectors.toList());
if(CollectionUtils.isNotEmpty(moneyStockSuggestDTOS)){ if(CollectionUtils.isNotEmpty(moneyStockSuggestDTOS)){
List<String> selfUlrList = moneyStockSuggestDTOS.stream().map(MoneyStockSuggestDTO::getStockName).collect(Collectors.toList()); List<String> selfUlrList = moneyStockSuggestDTOS.stream().map(MoneyStockSuggestDTO::getStockName).collect(Collectors.toList());
@@ -514,6 +524,9 @@ public class MoneyApiController {
.forEach(f->f.setScId(all.stream().filter(s->s.getStockName().equals(f.getStockName())).findFirst().orElse(null).getMoneyScId())); .forEach(f->f.setScId(all.stream().filter(s->s.getStockName().equals(f.getStockName())).findFirst().orElse(null).getMoneyScId()));
} }
} }
gainerStockSuggestCache.put(stockType, moneyStockSuggestDTOS);
}
// 将结果放入缓存
} }
return moneyStockSuggestDTOS; return moneyStockSuggestDTOS;
} }
@@ -531,6 +544,8 @@ public class MoneyApiController {
@ResponseBody @ResponseBody
public List<MoneyStockSuggestDTO> getTopLoser(@RequestParam String stockType) { public List<MoneyStockSuggestDTO> getTopLoser(@RequestParam String stockType) {
List<MoneyStockSuggestDTO> moneyStockSuggestDTOS = null; List<MoneyStockSuggestDTO> moneyStockSuggestDTOS = null;
moneyStockSuggestDTOS = loserStockSuggestCache.getIfPresent(stockType);
if(null==moneyStockSuggestDTOS){
if(StringUtils.equals(stockType,"nse")){ if(StringUtils.equals(stockType,"nse")){
moneyStockSuggestDTOS = nseTopLoser(); moneyStockSuggestDTOS = nseTopLoser();
}else if(StringUtils.equals(stockType,"bse")){ }else if(StringUtils.equals(stockType,"bse")){
@@ -556,10 +571,9 @@ public class MoneyApiController {
} }
} }
} }
loserStockSuggestCache.put(stockType, moneyStockSuggestDTOS);
}
} }
return moneyStockSuggestDTOS; return moneyStockSuggestDTOS;
} }
@@ -578,6 +592,8 @@ public class MoneyApiController {
@ResponseBody @ResponseBody
public List<MoneyStockSuggestDTO> getTopActive(@RequestParam String stockType) { public List<MoneyStockSuggestDTO> getTopActive(@RequestParam String stockType) {
List<MoneyStockSuggestDTO> moneyStockSuggestDTOS = null; List<MoneyStockSuggestDTO> moneyStockSuggestDTOS = null;
moneyStockSuggestDTOS = activesStockSuggestCache.getIfPresent(stockType);
if(moneyStockSuggestDTOS ==null){
if(StringUtils.equals(stockType,"nse")){ if(StringUtils.equals(stockType,"nse")){
moneyStockSuggestDTOS = nseActives(); moneyStockSuggestDTOS = nseActives();
}else if(StringUtils.equals(stockType,"bse")){ }else if(StringUtils.equals(stockType,"bse")){
@@ -603,6 +619,8 @@ public class MoneyApiController {
} }
} }
} }
activesStockSuggestCache.put(stockType, moneyStockSuggestDTOS);
}
} }
return moneyStockSuggestDTOS; return moneyStockSuggestDTOS;
@@ -724,4 +742,19 @@ public class MoneyApiController {
bseTopLoser(); bseTopLoser();
} }
private Cache<String, List<MoneyStockSuggestDTO>> gainerStockSuggestCache = CacheBuilder.newBuilder()
.maximumSize(100) // 设置缓存的最大大小
.expireAfterWrite(1, TimeUnit.HOURS) // 设置缓存条目的过期时间
.build();
private Cache<String, List<MoneyStockSuggestDTO>> loserStockSuggestCache = CacheBuilder.newBuilder()
.maximumSize(100) // 设置缓存的最大大小
.expireAfterWrite(1, TimeUnit.HOURS) // 设置缓存条目的过期时间
.build();
private Cache<String, List<MoneyStockSuggestDTO>> activesStockSuggestCache = CacheBuilder.newBuilder()
.maximumSize(100) // 设置缓存的最大大小
.expireAfterWrite(1, TimeUnit.MINUTES) // 设置缓存条目的过期时间
.build();
} }