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 Thanks!
After the timeout has expired, the key will automatically be deleted. A key with an associated timeout is often said to be volatile in Redis terminology. The timeout will only be cleared by commands that delete or overwrite the contents of the key, including DEL , SET , GETSET and all the *STORE commands.
To create a Redis with an expiration time, use the SET command and the EX option to set the expiration time. The EX option takes a number in seconds and sets the number of seconds the key is valid until expiration. You can also use PX to specify the expiration time in Milliseconds.
There is no default TTL. By default, keys are set to live forever.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With