Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis - How to expire key daily

Tags:

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!

like image 438
bless1204 Avatar asked Jun 01 '15 04:06

bless1204


People also ask

Does Redis automatically delete expired keys?

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.

How do I change the expiration date on Redis cache?

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.

What is the default expiry of Redis?

There is no default TTL. By default, keys are set to live forever.


1 Answers

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)); 
like image 176
laggingreflex Avatar answered Oct 08 '22 18:10

laggingreflex