bugfix redis获取数据报错

This commit is contained in:
Lemon
2026-01-07 14:59:51 +08:00
parent 9fa6f1fcb8
commit 84140e94a5
2 changed files with 62 additions and 10 deletions

View File

@@ -619,10 +619,15 @@ public class RedisCache {
Map<String, T> result = new HashMap<>();
for (String key : keys) {
if (StringUtils.isNotBlank(key)) {
// 使用 index -1 获取 List 的最后一个元素
T lastValue = (T) redisTemplate.opsForHash().get(key, -1);
if (lastValue != null) {
result.put(key, lastValue);
try {
// 使用 index -1 获取 List 的最后一个元素
T lastValue = (T) redisTemplate.opsForList().index(key, -1);
if (lastValue != null) {
result.put(key, lastValue);
}
} catch (Exception e) {
// 记录错误日志,方便排查问题
logger.error("获取Redis List失败key={}, 可能key的类型不是List", key, e);
}
}
}
@@ -640,8 +645,41 @@ public class RedisCache {
if (StringUtils.isBlank(key)) {
return null;
}
// 使用 index -1 获取 List 的最后一个元素
return (T) redisTemplate.opsForList().index(key, -1);
try {
// 使用 index -1 获取 List 的最后一个元素
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 Mapkey 为入参的 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;
}
}