Skip to content
代码片段 群组 项目

[Feature#]解决泛型类型强制转换报错

已合并 韦秉芮请求将feat/weibingrui合并到main
1 文件
+ 4
15
比较变更
  • 并排
  • 内联
@@ -4,7 +4,6 @@ import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
@@ -49,7 +48,7 @@ public class RedisUtils {
* @param list the list to cache
*/
public <T> void setList(String key, List<T> list) {
redisTemplate.opsForList().rightPushAll(key, list);
redisTemplate.opsForList().rightPushAll(key, list.toArray());
}
/**
@@ -64,19 +63,9 @@ public class RedisUtils {
if (rawList == null) {
return Collections.emptyList();
}
List<T> resultList = new ArrayList<>(rawList.size());
for (Object o : rawList) {
if (clazz.isInstance(o)) {
resultList.add(clazz.cast(o));
} else {
// Handle the case where the object is not of type T.
// Depending on your application logic, you might want to throw an exception,
// log a warning, or skip the item.
throw new IllegalArgumentException("Cached list contains elements not of type " + clazz.getName());
}
}
return resultList;
return rawList.stream().filter(clazz::isInstance)
.map(clazz::cast)
.toList();
}
// 检查Key是否存在
加载中