expire redis cache key at particular hours rather

2019-07-25 16:40发布

问题:

My code is as shown below:

client.set(name, data, 'EX', 23 * 60 * 60, (err, reply) => {

        });

What it does is , it stores the key-value for 23 hours. But is there any way , through which I can set the key to expire at 11:59 p.m. in the night?

回答1:

Redis itself doesn't provide this functionality. But you can calculate the value of seconds until midnight.

var nd = new Date().setHours(23,59,59);
var expire = Math.floor((nd-Date.now())/1000);


回答2:

Despite the accepted answer, you could use EXPIREAT for that. However, since SET does not support this as an argument, you'll need two calls in your code.



标签: node.js redis