欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

解决redisTemplate中leftPushAll隐性bug的问题

时间:2021-03-03 10:05:26|栏目:JAVA代码|点击:

前言

请看下面代码:

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);

上一篇:解决Java Redis删除HashMap中的key踩到的坑

栏    目:JAVA代码

下一篇:java 多线程饥饿现象的问题解决方法

本文标题:解决redisTemplate中leftPushAll隐性bug的问题

本文地址:http://www.codeinn.net/misctech/73148.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有