Achieve atomic updates with node-redis zIncrBy for multiple members
unscrew opened this issue · 2 comments
unscrew commented
Description
What's the best practice to achieve atomic updates with node-redis client for multiple members from nodejs?
https://github.com/redis/node-redis/blob/master/packages/client/lib/commands/ZINCRBY.ts
Also, do you have a doc stating request/response examples for clients?
leibale commented
ZINCRBY only supports incrementing 1 key at a time, but you can wrap multiple of them with MULTI & EXEC:
await client.multi()
.zIncrBy('key', 1, '1')
.zIncrBy('key', 2, '2')
.exec()regarding "request/response" docs:
- you have some examples in the examples folder
- you can use redis.io, we try to match the names and order of arguments as much as possible.
- you can use typescript autocomplete/types
- unfortunately we don't have autogenerated examples based on typescript yet
unscrew commented
Thanks @leibale. If we don't know now the number of members, would auto-pipelineing be the recommended approach like below? or do recommend something else?
const promises = [];
for (const member of members) {
promises.push(nodeRedisClient.zIncrBy(key, val, member));
}
const allResults = await Promise.allSettled(promises);
// check allResults