Does ioredis mset() accepts options to set expiry, like set operation does?

3.5k Views Asked by At

Here is my code:

const Redis = require('ioredis');
const client = new Redis();
// multi set
client.mset({'key1': 'value1'});

Question is does mset operation accept options to set TTL like set command does?

// sample for set

client.set(key, value, 'EX', 10);
1

There are 1 best solutions below

1
On

No it does not. MSET is used for just for setting multiple values for the keys. As you may can see here from the implementation, there is no option to set expiration for the keys. Another variation MSETNX also does not support optional expiration.

You need to execute EXPIRE command for each key you set in MSET. Another option could be executing them in transaction or completely discarding MSET but use SETEX(or SET with EX option) for each key.