davidkomer
davidkomer

Reputation: 3078

Get multiple sets

I've currently got a dataset which is something like:

channel1 = user1,user2,user3

channel2 = user4,user5,user6

(note- these are not actual names, the text is not a predictable sequence)

I would like to have the most optimized capability for the following:

1) Add user to a channel

2) Remove user from a channel

3) Get list of all users in several selected channels, maintaining knowledge of which channel they are in (in case it matters- this can also be simply checking whether a channel has any users or not without getting an actual list of them)

4) Detect if a specific user is in a channel (willing to forego this feature if necessary)

I'm a bit hungup on the fact that there are only two ways I can see of getting multiple keys at once:

A) Using regular keys and a mget key1, key2, key3

B) Using sets and sunion

Any solutions which can implement the above points 1-4 in optimal time and atomic operations?

EDIT: One idea which might work in my specific case is to store the channel name as part of the username and then use sets. Still, it would be great to get a more generic answer

Upvotes: 3

Views: 6382

Answers (1)

Didier Spezia
Didier Spezia

Reputation: 73216

Short answer: use sets + pipelining + MULTI/EXEC, or sets + Lua.

1) Add user to a channel

SADD command

2) Remove user from a channel

SREM command

3) Get list of all users in several selected channels

There are several ways to do it.

If you don't need strict atomicity, you just have to pipeline several SMEMBERS commands to retrieve all the sets in one roundtrip. If you are just interested whether channels have users or not, you can replace SMEMBERS by SCARD.

If you need strict atomicity, you can pipeline a MULTI/EXEC block containing SMEMBERS or SCARD commands. The output of the EXEC command will contain all the results. This is the solution I would recommend.

An alternative (atomic) way is to call a server-side Lua script using the EVAL command. Lua script executions are always atomic. The script could take a number of channel as input parameters, and build a multi-layer bulk reply to return the output.

4) Detect if a specific user is in a channel

SISMEMBER command - pipeline them if you need to check for several users.

Upvotes: 4

Related Questions