Use pipeline instead of eval

This commit is contained in:
Adria Navarro 2024-03-06 12:22:20 +01:00
parent edca6f6eca
commit 4baadadaa8
1 changed files with 7 additions and 14 deletions

View File

@ -291,23 +291,16 @@ class RedisWrapper {
return acc return acc
}, {} as Record<string, any>) }, {} as Record<string, any>)
const luaScript = ` const pipeline = client.pipeline()
for i, key in ipairs(KEYS) do pipeline.mset(dataToStore)
redis.call('MSET', key, ARGV[i])
${
expirySeconds !== null
? `redis.call('EXPIRE', key, ARGV[#ARGV])`
: ""
}
end
`
const keys = Object.keys(dataToStore)
const values = Object.values(dataToStore)
if (expirySeconds !== null) { if (expirySeconds !== null) {
values.push(expirySeconds) for (const key of Object.keys(dataToStore)) {
pipeline.expire(key, expirySeconds)
}
} }
await client.eval(luaScript, keys.length, ...keys, ...values) await pipeline.exec()
} }
async getTTL(key: string) { async getTTL(key: string) {