node-redis icon indicating copy to clipboard operation
node-redis copied to clipboard

sRandMember seems to not support the count argument

Open atuttle opened this issue 1 year ago • 2 comments

Description

I feel like I'm going crazy! How can this not work for such a widely used project?

Redis console, showing that my redis server does the right thing and returns more than 1 row back (yes, the rows are intentionally ~identical, this is test data)

IQ Prod NEW v7:0>SRANDMEMBER "MQ:click-track:LAWR:QA"
"{"ml":"00225535-5c1d-4986-a93b3d7c27e5b427","mc":"0D6DD930-F41E-856C-42796C6D1F84A46F","ts":"2024-04-03T19:05:50.586Z","referer":null}"
IQ Prod NEW v7:0>SRANDMEMBER "MQ:click-track:LAWR:QA" 100
1) "{"ml":"00225535-5c1d-4986-a93b3d7c27e5b427","mc":"0D6DD930-F41E-856C-42796C6D1F84A46F","ts":"2024-04-03T19:14:55.533Z","referer":null}"
2) "{"ml":"00225535-5c1d-4986-a93b3d7c27e5b427","mc":"0D6DD930-F41E-856C-42796C6D1F84A46F","ts":"2024-04-03T19:05:50.586Z","referer":null}"
3) "{"ml":"00225535-5c1d-4986-a93b3d7c27e5b427","mc":"0D6DD930-F41E-856C-42796C6D1F84A46F","ts":"2024-04-03T19:14:52.653Z","referer":null}"
4) "{"ml":"00225535-5c1d-4986-a93b3d7c27e5b427","mc":"0D6DD930-F41E-856C-42796C6D1F84A46F","ts":"2024-04-03T19:01:21.925Z","referer":null}"

But now a node REPL showing that some commands return multiple rows still work as expected (SMEMBERS), but SRANDMEMBER only ever returns 1 row, both with and without the count argument:

node
Welcome to Node.js v18.20.0.
Type ".help" for more information.
> const redis = /* connect to redis server */
undefined
> await redis.SMEMBERS('MQ:click-track:LAWR:QA');
[
  '{"ml":"00225535-5c1d-4986-a93b3d7c27e5b427","mc":"0D6DD930-F41E-856C-42796C6D1F84A46F","ts":"2024-04-03T19:14:55.533Z","referer":null}',
  '{"ml":"00225535-5c1d-4986-a93b3d7c27e5b427","mc":"0D6DD930-F41E-856C-42796C6D1F84A46F","ts":"2024-04-03T19:05:50.586Z","referer":null}',
  '{"ml":"00225535-5c1d-4986-a93b3d7c27e5b427","mc":"0D6DD930-F41E-856C-42796C6D1F84A46F","ts":"2024-04-03T19:14:52.653Z","referer":null}',
  '{"ml":"00225535-5c1d-4986-a93b3d7c27e5b427","mc":"0D6DD930-F41E-856C-42796C6D1F84A46F","ts":"2024-04-03T19:01:21.925Z","referer":null}'
]
> await redis.SRANDMEMBER('MQ:click-track:LAWR:QA');
'{"ml":"00225535-5c1d-4986-a93b3d7c27e5b427","mc":"0D6DD930-F41E-856C-42796C6D1F84A46F","ts":"2024-04-03T19:14:52.653Z","referer":null}'
> await redis.SRANDMEMBER('MQ:click-track:LAWR:QA', 100);
'{"ml":"00225535-5c1d-4986-a93b3d7c27e5b427","mc":"0D6DD930-F41E-856C-42796C6D1F84A46F","ts":"2024-04-03T19:01:21.925Z","referer":null}'
>

Node.js Version

18.20.0

Redis Server Version

7.0.7

Node Redis Version

1.5.14

Platform

MacOS, Linux (AWS Lambda)

Logs

No response

atuttle avatar Apr 03 '24 19:04 atuttle

Digging through the code, we found that it does work but it's been split into a separate command:

redis.SRANDMEMBER_COUNT("key", count);

Is that intentional? Is it documented anywhere?

atuttle avatar Apr 03 '24 19:04 atuttle

@atuttle The reason is to have better TS support (i.e. SRANDMEMBER returns a string, SRANDMEMBER_COUNT returns an Array<string>). TBH I'm not too sure if it's mentioned in the docs.

leibale avatar Apr 03 '24 22:04 leibale