解决redisTemplate中leftPushAll隐性bug的问题
这篇文章主要介绍了解决redisTemplate中leftPushAll隐性bug的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧前言
请看下面代码:
String key = String.format("test_key:%s", System.currentTimeMillis()/1000);
String key2=key+"_2";
String key3=key+"_3";
List<String> t1=new ArrayList<>();
t1.add("2");
t1.add("3");
t1.add("4");
t1.add("5");
t1.add("1");
redisTemplate.opsForList().leftPushAll(key, t1);
redisTemplate.opsForList().leftPushAll(key3, t1.toArray());
redisTemplate.opsForList().leftPushAll(key2,new String[]{"dfdg","dgdaasdf","gdadfdf"});其中,那么,请猜测一下各个key里面的内容,
下面开奖了:
结论
leftPushAll可以传 Object… 数组,也可以传 Collection进去。
然后实际上,我这边传 ArrayList这些数组是不行的,必须转换为 [] 这种数组―就是说,api里面的leftPushAll(Collection list)
用不了,具体原因还在查。。。
不过网上资料太少了。。
补充:java 用redisTemplate 的 Operations存取list集合
一 、存取为list类型
@RestController
@RequestMapping("/test")
@Slf4j
public class TestController {
@Autowired
private RedisTemplate redisTemplate;
@ApiOperation("redis-savelist")
@PostMapping("/redis/save/list")
public void redisSaveList() {
List<Person> list = getPersonList();
//清空
while (redisTemplate.opsForList().size("oowwoo") > 0){
redisTemplate.opsForList().leftPop("oowwoo");
}
//存储
redisTemplate.opsForList().rightPushAll("oowwoo", list);
//取出
List<Person> oowwoo = redisTemplate.opsForList().range("oowwoo", 0, -1);
log.info(">>>>>>>>>>>>>>>list = {}", oowwoo.toString());
Iterator<Person> it = oowwoo.iterator();
while(it.hasNext()){
Person p = it.next();
log.info("person = {}", p.toString());
}
}
private List<Person> getPersonList() {
Person p1 = new Person();
p1.setId(1L);
p1.setName("张一");
p1.setAge(11);
Person p2 = new Person();
p2.setId(2L);
p2.setName("张二");
p2.setAge(22);
Person p3 = new Person();
p3.setId(3L);
p3.setName("张三");
p3.setAge(33);
List<Person> list = new ArrayList<>();
list.add(p1);
list.add(p2);
list.add(p3);
return list;
}
}二 、将list转为json对象存取
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
@Autowired
private StringRedisTemplate stringRedisTemplate;
//存
List<Long> businessIdList = eeFreecarriageShopService.selectBusinessIdInPromotion();
stringRedisTemplate.opsForValue().set(RedisConstants.FREECARRIAGE_BUSINESSIDLIST, JSON.toJSON(businessIdList).toString());
//取
String businessJsonArray = stringRedisTemplate.opsForValue().get(RedisConstants.FREECARRIAGE_BUSINESSIDLIST);
List<Long> businessIdList = JSONObject.parseArray(businessJsonArray, Long.class);以上为个人经验,希望能给大家一个参考,也希望大家多多支持CodeAE代码之家。如有错误或未考虑完全的地方,望不吝赐教。
原文链接:https://blog.csdn.net/cdnight/article/details/88803869
http://www.zzvips.com/article/182199.html
页:
[1]