redis设置与获取过期时间一网打尽

末蓝、 2023-02-14 02:20 60阅读 0赞

#

      • redis设置过期时间命令
        • expire key seconds : 设置key的过期时间为seconds秒
        • pexpire key milliseconds : 设置key的过期时间为milliseconds毫秒
        • expireat key timestamp : 设置key的过期时间为timestamp对应的时间戳(秒)
        • pexpireat key millitimestamp : 设置key的过期时间为millitimestamp对应的时间戳(毫秒)
        • setex key seconds value : 同时设置key对应的value和过期时间(秒)
        • psetex key milliseconds value : 同时设置key对应的value和过期时间(毫秒)
      • 移除redis已设置过期时间命令
        • persist key : 将redis中对应的key的过期时间移除,使其永不过期
      • 获取redis过期时间命令
        • ttl key : 查看key的过期时间,返回过期时间的秒数,如果不存在过期时间则返回 -1, 如果key也不存在则返回 -2
        • pttl key : 查看key的过期时间,返回过期时间的毫秒数,如果不存在过期时间则返回 -1, 如果key也不存在则返回 -2
      • 获取某个日期距离当天结束的毫秒数

redis设置过期时间命令

expire key seconds : 设置key的过期时间为seconds秒

  1. //存储key为test的value为 haha
  2. 127.0.0.1:6379> set test haha
  3. OK
  4. //将key为test的过期时间设置为10秒后
  5. 127.0.0.1:6379> expire test 10
  6. (integer) 1

pexpire key milliseconds : 设置key的过期时间为milliseconds毫秒

  1. //存储key为test的value为 hehe
  2. 127.0.0.1:6379> set test hehe
  3. OK
  4. //将key为test的过期时间设置为20000毫秒后
  5. 127.0.0.1:6379> pexpire test 20000
  6. (integer) 1

expireat key timestamp : 设置key的过期时间为timestamp对应的时间戳(秒)

  1. //存储key为test的value为 gaga
  2. 127.0.0.1:6379> set test gaga
  3. OK
  4. //将key为test的过期时间设置为时间戳 1590852934
  5. 127.0.0.1:6379> expireat test 1590852934
  6. (integer) 1

pexpireat key millitimestamp : 设置key的过期时间为millitimestamp对应的时间戳(毫秒)

  1. //存储key为test的value为 gaga
  2. 127.0.0.1:6379> set test gaga
  3. OK
  4. //将key为test的过期时间设置为时间戳 1590853534000
  5. 127.0.0.1:6379> pexpireat test 1590853534000
  6. (integer) 1

setex key seconds value : 同时设置key对应的value和过期时间(秒)

  1. //设置key为test的value为haha,并且设置过期时间为100秒
  2. 127.0.0.1:6379> setex test 100 haha
  3. OK

psetex key milliseconds value : 同时设置key对应的value和过期时间(毫秒)

  1. //设置key为test的value为 haha,同时设置过期时间为6000毫秒
  2. 127.0.0.1:6379> psetex test 60000 haha
  3. OK
  4. //查看key为test的过期时间(秒)
  5. 127.0.0.1:6379> ttl test
  6. (integer) 35
  7. //查看key为test的过期时间(毫秒)
  8. 127.0.0.1:6379> pttl test
  9. (integer) 17933

移除redis已设置过期时间命令

persist key : 将redis中对应的key的过期时间移除,使其永不过期

  1. //存储key为test的value为 haha
  2. 127.0.0.1:6379> set test haha
  3. OK
  4. //移除未设置过期时间的key
  5. 127.0.0.1:6379> persist test
  6. (integer) 0
  7. //为test设置过期时间600秒
  8. 127.0.0.1:6379> expire test 600
  9. (integer) 1
  10. //查看key为test的过期时间为567秒
  11. 127.0.0.1:6379> ttl test
  12. (integer) 567
  13. //移除key为test的过期时间
  14. 127.0.0.1:6379> persist test
  15. (integer) 1
  16. //查看key为test的过期时间为不存在
  17. 127.0.0.1:6379> ttl test
  18. (integer) -1

获取redis过期时间命令

ttl key : 查看key的过期时间,返回过期时间的秒数,如果不存在过期时间则返回 -1, 如果key也不存在则返回 -2

  1. //查看key为test的过期时间为596秒
  2. 127.0.0.1:6379> ttl test
  3. (integer) 596

pttl key : 查看key的过期时间,返回过期时间的毫秒数,如果不存在过期时间则返回 -1, 如果key也不存在则返回 -2

  1. //查看不存在key为test的过期时间(单位为毫秒)
  2. 127.0.0.1:6379> pttl test
  3. (integer) -2
  4. //设置test的value为haha
  5. 127.0.0.1:6379> set test haha
  6. OK
  7. //查看不存在key为test的过期时间(单位为毫秒)
  8. 127.0.0.1:6379> pttl test
  9. (integer) -1
  10. //设置test过期时间为100秒
  11. 127.0.0.1:6379> expire test 100
  12. (integer) 1
  13. //查看key为test的过期时间为58860毫秒
  14. 127.0.0.1:6379> pttl test
  15. (integer) 58860
  16. //查看key为test的过期时间为40秒
  17. 127.0.0.1:6379> ttl test
  18. (integer) 40

获取某个日期距离当天结束的毫秒数

如果我们在给一个key设置过期时间的时候,经常会设置一个到当天结束为止的key的过期时间,接下来我们就可以通过下面的方法来获取:

  1. public static long getRemainMillisecondsOneDay(Date date) {
  2. LocalDateTime dateEnd = LocalDateTime.ofInstant(date.toInstant(),
  3. ZoneId.systemDefault()).plusDays(1).withHour(0).withMinute(0)
  4. .withSecond(0).withNano(0);
  5. LocalDateTime dateLocalDateTime = LocalDateTime.ofInstant(date.toInstant(),
  6. ZoneId.systemDefault());
  7. return ChronoUnit.SECONDS.between(dateLocalDateTime, dateEnd);
  8. }

发表评论

表情:
评论列表 (有 0 条评论,60人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Redis过期时间

    上次开发用了redis后效果不错,后来在进阶的开发中查询到,如果redis一直不释放也会占用内存,于是就想着给redis加上个过期时间。 具体操作很简单,只要在设置redis