跳到主要内容
CNode

redis expireat 失效

问答
Jjiangliqin发布于 10 年前最后回复 10 年前
973100

采用“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)

L
luinlee#910 年前

是的,EXPIREAT 的单位是 s

J
jiangliqin#810 年前
引用 luinleePEXPIREAT 和 EXPIREAT 的时间单位分别是 ms 和 s,Date valueOf() 的单位是 ms

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

L
luinlee#710 年前

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

J
jiangliqin#610 年前
引用 im-here@jiangliqin `expire 是接收一个整形数字,单位是秒,你给多少就是多少。 例如 expire key 30,那么key的生存时间就是30秒。 expireat `就不一样了,它...

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

I
im-here#510 年前
引用 jiangliqin@imhered 但是expire方式有效

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

文档在看仔细点吧。

J
jiangliqin#310 年前
引用 im-here请看官方文档说明: EXPIREAT has the same effect and semantic as EXPIRE, but instead of specifying the num...

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

I
im-here#210 年前

请看官方文档说明: 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.

I
im-here#110 年前

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

参与回复

登录后即可参与回复。登录