欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

解決redisTemplate中l(wèi)eftPushAll隱性bug的問(wèn)題

 更新時(shí)間:2021年02月13日 11:39:22   作者:碼農(nóng)下的天橋  
這篇文章主要介紹了解決redisTemplate中l(wèi)eftPushAll隱性bug的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

前言

請(qǐng)看下面代碼:

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

其中,那么,請(qǐng)猜測(cè)一下各個(gè)key里面的內(nèi)容,

下面開(kāi)獎(jiǎng)了:

結(jié)論

leftPushAll可以傳 Object… 數(shù)組,也可以傳 Collection進(jìn)去。

然后實(shí)際上,我這邊傳 ArrayList這些數(shù)組是不行的,必須轉(zhuǎn)換為 [] 這種數(shù)組—就是說(shuō),api里面的leftPushAll(Collection list)

用不了,具體原因還在查。。。

不過(guò)網(wǎng)上資料太少了。。

補(bǔ)充: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");
    }
    //存儲(chǔ)
    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轉(zhuǎn)為json對(duì)象存取

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

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

最新評(píng)論