redis expireat 失效
发布于 8 年前 作者 jiangliqin 6065 次浏览 来自 问答

采用“expireat key 时间戳”的形式为key设置有效期貌似不成功, const moment = require(‘moment’); const time = moment().add(3,‘minutes’).valueOf()

set test ceshi expireat test time 上面这种设置时间有效期的方式貌似没问题吧,但是不起作用,test在redis中并没有过3分钟失效。。。

各位大侠,如果非要用expireat方式设置有效期,该怎么设置呢?

9 回复

time是什么格式? 字符串还是时间戳? expireat 貌似只能接受时间戳!

请看官方文档说明: EXPIREAT has the same effect and semantic as EXPIRE, but instead of specifying the number of seconds representing the TTL (time to live),it takes an absolute Unix timestamp(seconds since January 1, 1970).Please for the specific semantics of the command refer to the documentation of EXPIRE.

@imhered time就是时间戳格式,我也看了文档说明,你试了吗?有效吗

@imhered 但是expire方式有效

@jiangliqin expire是接收一个整形数字,单位是秒,你给多少就是多少。 例如 expire key 30,那么key的生存时间就是30秒。 expireat就不一样了,它是接收一个时间戳,时间戳是从1970.1.1开始计算的。即官方文档里的这句seconds since January 1, 1970,如果你给的时间戳小于1970.1.1的话,那肯定认为是一个不合格的数字,redis就会自动把这个key的有效时间转化为-1(即永不过期)。

文档在看仔细点吧。

@imhered 恩,问题中我贴出的moment().add(3,‘minutes’).valueOf()就是:当前时间+3分的时间戳,肯定是大于1970.1.1,应该是合法的

PEXPIREATEXPIREAT 的时间单位分别是 ms 和 s,Date#valueOf() 的单位是 ms

@luinlee Date#valueOf()和Date#getTime()都是ms啊,都表示时间戳

是的,EXPIREAT 的单位是 s

回到顶部