Akilesh
Akilesh

Reputation: 41

How to expire redis key-value 24 hours after pushing?

So I'm using redis to store a list and I need it to expire after 24 hrs, how do I do this?

I tried using expire command but I am able to input only seconds after... what if I want to it to expire on a specific date? I currently have:

r.rpush(r_id,d_id,l_id,ib_st) r.expire(resident_id, 10)

Upvotes: 2

Views: 2122

Answers (1)

Eran Friedman
Eran Friedman

Reputation: 658

As mentioned in the comment, you can convert 24 hours to seconds, which is 24 * 60 * 60 = 86400 and use the EXPIRE command:

r.expire(resident_id, 86400)

Alternatively, to set expiration to a specific date, you can use the EXPIREAT command which accepts absolute time in the form of unix time. For example, if you use redis-py client, expireat()'s second parameter is either an int (unix time) or datetime, so you can set the expiration to, e.g., 2023/2/2 10:0:0 by doing:

d = datetime.datetime(2023, 2, 2, 10)
r.expireat("a", resident_id)

Upvotes: 2

Related Questions