expire redis cache key at particular hours rather

2019-07-25 16:39发布

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?

标签: node.js redis
2条回答
三岁会撩人
2楼-- · 2019-07-25 16:43

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);
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-07-25 17:09

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.

查看更多
登录 后发表回答