How to Set expiry time of hsetnx (https://redis.io/commands/hsetnx/) to be 1 hour. Currently i don’t see a param where i can set expiry time to it.
const IoRedis = require("ioredis");
const redis = new IoRedis();
var message = {
"jobdid": "JCLT",
"email": "[email protected]"
}
checkForDuplicate(message);
async function checkForDuplicate(message){
const email = message.email.toLowerCase();
const jobdid = message.jobdid.toLowerCase();
const resp = await redis.hsetnx(`jobs:${email}`, jobdid, +new Date());
console.log(resp);
}
2
Answers
Not possible. You’ll need to call EXPIRE on the key separately.
ADDING ATOMICITY
As requested, here is an example of using ioredis with a transaction. Not that I have not tested this code, just writing it from memory.
If you don’t need to enumerate the jobs separately, then you don’t really need a hash; you can just se a sequence of setnx + expire. You don’t need a MULTI since setnx will only return 1 exactly once, so a second concurrent caller will never get to the expire.