diff --git a/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/PileConnectorInfoServiceImpl.java b/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/PileConnectorInfoServiceImpl.java index 7e39e7826..691fc0e6f 100644 --- a/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/PileConnectorInfoServiceImpl.java +++ b/jsowell-pile/src/main/java/com/jsowell/pile/service/impl/PileConnectorInfoServiceImpl.java @@ -494,20 +494,28 @@ public class PileConnectorInfoServiceImpl implements PileConnectorInfoService { @Override public List batchSelectConnectorList(List stationIds){ List resultList = new ArrayList<>(); - Map> map = new LinkedHashMap<>(); + Map> map; String baseRedisKey = CacheConstants.GET_UNIAPP_CONNECTOR_LIST_BY_STATION_ID; - // 先查询缓存数据 - for (String stationId : stationIds) { - String redisKey = baseRedisKey + stationId; - List list = redisCache.getCacheList(redisKey); - if (CollectionUtils.isNotEmpty(list)) { - map.put(stationId, list); - // 重新设置缓存 - redisCache.deleteObject(redisKey); - redisCache.setCacheList(redisKey, list); - redisCache.expire(redisKey, CacheConstants.cache_expire_time_1h); + // 先批量查缓存 + List redisKeys = new ArrayList<>(); + stationIds.forEach(stationId -> + redisKeys.add(CacheConstants.GET_UNIAPP_CONNECTOR_LIST_BY_STATION_ID + stationId)); + List redisObjects = redisCache.multiGet(redisKeys); + List list = new ArrayList<>(); + for (Object redisObject : redisObjects) { + if (redisObject == null) { + continue; + } + JSONArray array = JSONArray.parseArray(JSON.toJSONString(redisObject)); + for (Object o : array) { + ConnectorInfoVO connectorInfoVO = JSONObject.parseObject(JSON.toJSONString(o), ConnectorInfoVO.class); + list.add(connectorInfoVO); } } + // 按照stationId分组 + map = list.stream() + .collect(Collectors.groupingBy(ConnectorInfoVO::getStationId)); + // 先将已经有数据的stationId进行收集 List hasDataStationIds = new ArrayList<>(map.keySet()); List> values = new ArrayList<>(map.values()); @@ -528,15 +536,19 @@ public class PileConnectorInfoServiceImpl implements PileConnectorInfoService { // 先将list根据站点id分组 Map> collect = newConnectorInfoList.stream() .collect(Collectors.groupingBy(ConnectorInfoVO::getStationId)); - // 循环map并设置缓存 + // 循环map并设置缓存key值 + Map> redisMap = new LinkedHashMap<>(); for (Map.Entry> entry : collect.entrySet()) { String stationId = entry.getKey(); + String redisKey = baseRedisKey + stationId; + List voList = entry.getValue(); - String redisKey = baseRedisKey + stationId; - redisCache.setCacheList(redisKey, voList); - redisCache.expire(redisKey, CacheConstants.cache_expire_time_1h); + redisMap.put(redisKey, voList); } + // 批量设置缓存 + redisCache.batchSetCacheList(redisMap, CacheConstants.cache_expire_time_1h, TimeUnit.SECONDS); + // redisCache.multiSave(redisMap, CacheConstants.cache_expire_time_1h); } // 最终将 values 中的所有 ConnectorInfoVO 元素收集到 resultList