redis存取list集合
一 、存取为list类型@RestController@RequestMapping("/test")@Slf4jpublicclassTestController@Autowiredprivate RedisTemplate redisTemplate;@ApiOperation("redis-savelist")@PostMapping("/redis/save/list")publ...
一 、存取为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 用redisTemplate 的 Operations存取list集合
更多推荐
所有评论(0)