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是直接赋值时间。
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐