Arijeet Saha
Arijeet Saha

Reputation: 1168

Redis ExpireAt not working as expected

As per the Redis docs, EXPIREAT accepts unixtime as the argument specifying the time at which the key gets deleted.

When I execute following commands in redis-cli:

127.0.0.1:6379> set bb bb
OK
127.0.0.1:6379> EXISTS bb
(integer) 1
127.0.0.1:6379> TTL bb
(integer) -1
127.0.0.1:6379> EXPIREAT bb 1462173420000 //executed at 1462173300000 ====>>> 1
(integer) 1
127.0.0.1:6379> EXISTS bb //executed after 1462173420000
(integer) 1 =============>>> 2
127.0.0.1:6379> TTL bb
(integer) 1460711246668
127.0.0.1:6379> TTL bb
(integer) 1460711246663
127.0.0.1:6379> EXPIREAT bb 100
(integer) 1 ===================>> 3
127.0.0.1:6379> EXISTS bb
(integer) 0 ===================>> 4

EXPIREAT executed on bb for time - 2 May, 2016 12:47 PM at 2 May, 2016 12:45 PM.

Even after 2 May, 2016 12:47PM, the key still exits. Why is this happening?

Why is EXPIREAT accepting invalid unixtime - 100??

Upvotes: 0

Views: 597

Answers (1)

Karthikeyan Gopall
Karthikeyan Gopall

Reputation: 5679

EXPIREAT takes values as seconds, not as milli seconds. For millseconds you have to use PEXPIREAT. you have given the value as milli seconds so the expire time is set to a higher value.

Small experiment done for your use case:

127.0.0.1:6379> set karthik 10
OK
127.0.0.1:6379> time //You can use this command to see redis time
1) "1462174316"
2) "692726"
127.0.0.1:6379> EXPIREAT karthik 1462174350 //setting the key to expire at 1462174350
(integer) 1
127.0.0.1:6379> get karthik 
"10"

127.0.0.1:6379> ttl karthik //use this to see the time left
(integer) 22
127.0.0.1:6379> time
1) "1462174336"
2) "316226"
127.0.0.1:6379> get karthik
"10"
127.0.0.1:6379> time
1) "1462174343"
2) "310511"
127.0.0.1:6379> ttl karthik
(integer) 1
127.0.0.1:6379> time //time crossed 1462174350
1) "1462174351"
2) "48589"
127.0.0.1:6379> get karthik //key has got expired
(nil)
127.0.0.1:6379> 

Upvotes: 2

Related Questions