0

I am trying to set an expiration time on a Redis key and have tried several approaches without success. I use "redis": "^4.6.15". In e.g. this answer they said to do it like this:

await redisClient.set(key , value, {EX: 60*60*4})

I tried this but no matter the approach I get the error "[ErrorReply: ERR value is not an integer or out of range]". I initiate the Redis client like this:

const redisClient = createClient({
  password: process.env.RedisCachePass,
  socket: {
      host: process.env.RedisCacheUrl,
      port: process.env.RedisCachePort,
      tls: true
  }
})
redisClient.on('error', (err) => {
  console.error('Could not connect to Redis', err);
});

redisClient.on('connect', () => {
  console.log('Connected to Redis ');
  console.log(redisClient);
});

Which gives me this object:

Commander {
  _events: [Object: null prototype] {
    error: [Function (anonymous)],
    connect: [Function (anonymous)]
  },
  _eventsCount: 2,
  _maxListeners: undefined,
  commandOptions: [Function: commandOptions],
  select: [AsyncFunction: SELECT],
  subscribe: [Function: SUBSCRIBE],
  unsubscribe: [Function: UNSUBSCRIBE],
  pSubscribe: [Function: PSUBSCRIBE],
  pUnsubscribe: [Function: PUNSUBSCRIBE],
  sSubscribe: [Function: SSUBSCRIBE],
  sUnsubscribe: [Function: SUNSUBSCRIBE],
  quit: [Function: QUIT],
  multi: [Function: MULTI],
  bf: {},
  cms: {},
  cf: {},
  tDigest: {},
  topK: {},
  graph: {},
  json: {},
  ft: {},
  ts: {},
  [Symbol(shapeMode)]: false,
  [Symbol(kCapture)]: false
}

Then I create an express server with redisClient like this:

 server.use(session({
    resave: false,
    saveUninitialized: false,
    secret: 'secret-key',
    cookie: {
      secure: 'auto',
      httpOnly: true,
      ephemeral: true
    },
    store: new RedisStore({
      client: redisClient
    })
  }))

Here is then how I use the redisClient at another place in the code, which is where I get the error:

post: async(req, res) => {
    console.log('#### REDIS POST (SET) ####')
    try {
      req.body = camelcaseKeys(req.body, { deep: true })
      const redisClient = req.sessionStore.client
      if (redisClient) {
        const { key, ...data } = req.body
        if (typeof data.jwtRequired === 'boolean' && data.jwtRequired) {
            data.jwt = req.session.jwt
        }
        const value = JSON.stringify(data)

        console.log(redisClient)
        await redisClient.set(key , value, {EX: 60*60*4})

        console.log('#### key: ', key)
        console.log('#### REDIS POST (SET) END ####')
        res.status(200).json()
      } else {
        throw new Error(REDIS_NOT_CONNECTED_ERROR)
      }
    } catch (error) {
      console.error(error)
      res.status(500).json()
    }
  },

Which gives me output:

#### REDIS POST (SET) ####
{
  get: [Function: get],
  set: [Function: set],
  del: [Function: del],
  expire: [Function: expire],
  mget: [Function: mget],
  scanIterator: [Function: scanIterator]
}
[ErrorReply: ERR value is not an integer or out of range]

Why can't I set expire on my Redis key? And why does my Redis client don't seem to have funcitons like ttl like others seems to have when I Google it?

3
  • What Node.js package are you using for session management? Is it connect-redis?
    – Guy Royse
    Commented Jul 9 at 13:29
  • Also, I don't see an asynchronous call to connect on the Redis client. If you are using connect-redis, that would be required.
    – Guy Royse
    Commented Jul 9 at 13:30
  • @GuyRoyse, yes to connect-redis. How would the async call look like?
    – eligolf
    Commented Jul 9 at 13:33

1 Answer 1

0

I suspect the problem here is that you need to connect the client to Redis before handing it to connect-redis. Or at least that is a problem that you will run into eventually.

To connect to Redis after the client is connected, just do the following:

await redisClient.connect()

Let me know if that helps or not and if it doesn't I can amend this answer.

Not the answer you're looking for? Browse other questions tagged or ask your own question.