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?