Redis - How to expire key daily
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));
You can set value and expiry together.
//here key will expire after 24 hours client.setex(key, 24*60*60, value, function(err, result) { //check for success/failure here }); //here key will expire at end of the day client.setex(key, parseInt((new Date().setHours(23, 59, 59, 999)-new Date())/1000), value, function(err, result) { //check for success/failure here });