mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-06-21 07:39:55 +08:00
bugfix redis获取数据报错
This commit is contained in:
@@ -619,11 +619,16 @@ public class RedisCache {
|
|||||||
Map<String, T> result = new HashMap<>();
|
Map<String, T> result = new HashMap<>();
|
||||||
for (String key : keys) {
|
for (String key : keys) {
|
||||||
if (StringUtils.isNotBlank(key)) {
|
if (StringUtils.isNotBlank(key)) {
|
||||||
|
try {
|
||||||
// 使用 index -1 获取 List 的最后一个元素
|
// 使用 index -1 获取 List 的最后一个元素
|
||||||
T lastValue = (T) redisTemplate.opsForHash().get(key, -1);
|
T lastValue = (T) redisTemplate.opsForList().index(key, -1);
|
||||||
if (lastValue != null) {
|
if (lastValue != null) {
|
||||||
result.put(key, lastValue);
|
result.put(key, lastValue);
|
||||||
}
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 记录错误日志,方便排查问题
|
||||||
|
logger.error("获取Redis List失败,key={}, 可能key的类型不是List", key, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@@ -640,8 +645,41 @@ public class RedisCache {
|
|||||||
if (StringUtils.isBlank(key)) {
|
if (StringUtils.isBlank(key)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
// 使用 index -1 获取 List 的最后一个元素
|
// 使用 index -1 获取 List 的最后一个元素
|
||||||
return (T) redisTemplate.opsForList().index(key, -1);
|
return (T) redisTemplate.opsForList().index(key, -1);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 记录错误日志,方便排查问题
|
||||||
|
logger.error("获取Redis List失败,key={}, 可能key的类型不是List", key, e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量获取 Redis List 中每个 key 对应的最后一条数据
|
||||||
|
*
|
||||||
|
* @param keys Redis 键集合
|
||||||
|
* @return Map,key 为入参的 key 值,value 为对应 List 的最后一条数据
|
||||||
|
*/
|
||||||
|
public Map<String, Object> multiGetLastListValueList(final List<String> keys) {
|
||||||
|
if (keys == null || keys.isEmpty()) {
|
||||||
|
return new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, Object> result = new HashMap<>();
|
||||||
|
for (String key : keys) {
|
||||||
|
if (StringUtils.isNotBlank(key)) {
|
||||||
|
try {
|
||||||
|
// 使用 index -1 获取 List 的最后一个元素
|
||||||
|
Object lastValue = redisTemplate.opsForList().index(key, -1);
|
||||||
|
result.put(key, lastValue);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 记录错误日志,方便排查问题
|
||||||
|
logger.error("获取Redis List失败,key={}, 可能key的类型不是List", key, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6412,10 +6412,24 @@ public class OrderBasicInfoServiceImpl implements OrderBasicInfoService {
|
|||||||
redisKeys.add(CacheConstants.PILE_REAL_TIME_MONITOR_DATA + pileConnectorCode + "_" + transactionCode);
|
redisKeys.add(CacheConstants.PILE_REAL_TIME_MONITOR_DATA + pileConnectorCode + "_" + transactionCode);
|
||||||
}
|
}
|
||||||
// 批量查询多个key值的数据 key: redisKey, value: 最后一条实时数据
|
// 批量查询多个key值的数据 key: redisKey, value: 最后一条实时数据
|
||||||
Map<String, RealTimeMonitorData> map = redisCache.multiGetLastListValue(redisKeys);
|
// 注意:数据在Redis中以Hash类型存储,field为时间字符串,需要获取所有field后取最新的一条
|
||||||
// 循环该map
|
for (String redisKey : redisKeys) {
|
||||||
for (Map.Entry<String, RealTimeMonitorData> entry : map.entrySet()) {
|
try {
|
||||||
resultList.add(entry.getValue());
|
Map<String, String> hashData = redisCache.getCacheMap(redisKey);
|
||||||
|
if (hashData != null && !hashData.isEmpty()) {
|
||||||
|
// 按时间排序,获取最新的一条数据
|
||||||
|
String latestJson = hashData.entrySet().stream()
|
||||||
|
.max(Map.Entry.comparingByKey())
|
||||||
|
.map(Map.Entry::getValue)
|
||||||
|
.orElse(null);
|
||||||
|
if (latestJson != null) {
|
||||||
|
RealTimeMonitorData data = JSON.parseObject(latestJson, RealTimeMonitorData.class);
|
||||||
|
resultList.add(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("获取实时监控数据失败,redisKey={}", redisKey, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return resultList;
|
return resultList;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user