skip to Main Content

I’ve been encountering this problem with ioredis where I have created a key and set expiry for that key. My code looks something like this

let temp1 = acct.limit;
let txn = array.length;
let cache = new ioredis(); // note that this is not the exact code snippet
let ttl = txn / temp1;
cache.set('key', true, Math.ceil(ttl));

The problem that I encountered is that sometimes the ttl is a positive number and sometimes a negative number. Thus, leading to the belief that maybe -1 is for an unlimited ttl. But upon further research and also trial and error anything less than or equal to 0 expires immediately. So my question really is, why some keys don’t expire even with a set ttl?

2

Answers


  1. Redis EXPIRE for a key is set in ttl seconds. So anything less than or equal to 0 for the ttl value will expire the keys immediately. The question is not clear because you have already explained that anything less than or equal to 0 will make the key expire immediately.

    Login or Signup to reply.
  2. TTL command returns three types of responses;

    • Returns the remaining time to live of a key that has a timeout in seconds.
    • Returns -2 if the key does not exist.
    • Returns -1 if the key exists but has no associated expire.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search