bless1204 bless1204 - 1 year ago 142
Node.js Question

Redis - How to expire key daily

I know that EXPIREAT in Redis is used to specify when a key will expire. My problem though is that it takes an absolute UNIX timestamp. I'm finding a hard time thinking about what I should set as an argument if I want the key to expire at the end of the day.

This is how I set my key:

client.set(key, body);

So to set the expire at:

client.expireat(key, ???);

Any ideas? I'm using this with nodejs and sailsjs

Answer Source

If you want to expire it 24 hrs later

client.expireat(key, parseInt((+new Date)/1000) + 86400);

Or if you want it to expire exactly at the end of today, you can use .setHours on a new Date() object to get the time at the end of the day, and use that.

var todayEnd = new Date().setHours(23, 59, 59, 999);
client.expireat(key, parseInt(todayEnd/1000));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download