Reputation: 3942
I am trying to auto expire the key in redis
when=10
unit='seconds'
redisClient.expireat(key,moment().add(when,unit));
On redis cli Keys * I can see the key I have set
> TTL 472962f1-10ad8-4c4d-bc5e-2e38b632f36
(integer) -1
> Exists 472962f1-10ad8-4c4d-bc5e-2e38b632f36
(integer) 1
and after 10 second it is not getting deleted !
I am using moment to get the time as I have to share the info in response that this key will not working after this.
var whenKeyExpire = moment().add(when,unit).format('MM-DD-YYYY hh:mm:ss A')
Upvotes: 2
Views: 606
Reputation: 379
In your case, you're passing wrong value into the parameter of expireat
function. For this function you have to check here. In this function you have to pass unixtime.
So for your scenario you can do something like this :
var when=10;
var unit="seconds";
redisClient.expireat(key, +new Date(moment().add(when,unit)), function (err, didSetExpiry){
if(err){
console.log("Error occured: " + err);
} else {
console.log("Redis Key Operation is successful");
}
});
Hope that it will help.
Upvotes: 2
Reputation: 3305
Ttl returns -1 if the key exists but has no associated expire.
I'm not famaliar with the node client, but I'm pretty sure that the expire time was not setted successfully.
Upvotes: 1