首页 > 其他 > 详细

redis中stringRedisTemplate中opsForList中方法的简单应用

时间:2019-11-22 16:54:33      阅读:829      评论:0      收藏:0      [点我收藏+]

opsForList操作List集合

push操作分为leftpush和rightpush,其中leftpush是在list的左侧添加,即列表的头部,right是在list的左侧添加,即在列表的尾部。可以根据业务酌情选择。

pop操作也分为left和right,意思和push一样。pop是获取一个元素,并且删除这个元素。

如果只想要查看某个元素。可以使用range,他有三个参数,第一个参数是key,后面是搜索范围,全集合搜索可以用(key,0,-1);

 

 

有时候希望给添加的缓存设置生命时间,到期后自动删除该缓存。可以使用

stringRedisTemplate.expire(key, seconds, TimeUnit.SECONDS);

key是键,需要是已经存在的,seconds是时间,单位是long类型,最后一个参数单位。

时间可以使用java8的语法。

Date currentDate = new Date();
LocalDateTime midnight = LocalDateTime.ofInstant(currentDate.toInstant(),ZoneId.systemDefault()).plusHours(2);
LocalDateTime currentDateTime =LocalDateTime.ofInstant(currentDate.toInstant(),ZoneId.systemDefault());
long seconds = ChronoUnit.SECONDS.between(currentDateTime, midnight);
LocalDateTime中的plusHours是指当前时候多少小时之后,同类型的还有plusSeconds,plusMinutes(minutes)等等;
别的还有withHours是直接赋值时间。

redis中stringRedisTemplate中opsForList中方法的简单应用

原文:https://www.cnblogs.com/jinsheng1027/p/11912085.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!