Looks like you’re looking for setnx, not setex. I’d put each email’s jobs in a hash of their own. (This is assuming you’re using a promiseful Redis library such as ioredis.)
async function checkForDuplicate(message) {
const email = message.email.toLowerCase();
const jobdid = message.jobdid.toLowerCase();
const res = await redis.hsetnx(`jobs:${email}`, jobdid, +new Date());
if (res === 0) {
// duplicate jobdid
return null;
}
// jobdid was added to this email's jobs hash
return jobdid;
}
async function getJobdids(message) {
const email = message.email.toLowerCase();
return redis.hkeys(`jobs:${email}`); // Promise of strings
}
2
Answers
This looks incorrect – you are only ever storing the single job-id and overwriting previous ones.
It should be
Looks like you’re looking for
setnx
, notsetex
. I’d put each email’s jobs in a hash of their own. (This is assuming you’re using a promiseful Redis library such as ioredis.)