簡體   English   中英

Redis-如何在月末使用節點js和時刻使密鑰過期

[英]Redis - How to expire key at the end of the month, with node js and moment

問題是:在月末使Redis密鑰過期,使用從Node js連接到Redis的連接客戶端,並使用moment處理日期。

解決方案:使用moment獲取直到一個月的最后一天為止的剩余毫秒數,然后將其傳遞給秒(毫秒/ 1000),並與redis客戶端一起,將過期時間分配給密鑰。

...
let dateTo = moment().endOf('month').valueOf();
let expire = parseInt(dateTo/1000);
client.expireat('redisKeys', expire);
client.exec(function (_error, _result) {
   if (_error) {
      return cb(_error);
   }
   return cb(null, "done!");
});
...

然后可以從redis-cli咨詢TTL命令:

redis_6379:0>TTL redisKey
21882

結果是您將獲得一個數字,該數字表示該密鑰的剩余壽命(以秒為單位)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM