SISMEMBER and SCARD
Every time a Wordcraft player guesses a word, the game must check "has this word already been guessed today?" and the lobby shows "1,284 players online". Neither needs the full list of members. Sets can answer yes-or-no and how-many directly, however large they grow.
The rule
Ask the set a question; don't fetch the set to answer it.
badges:asha holds first-win, streak-3 and speedster.
127.0.0.1:6379> SISMEMBER badges:asha speedster
(integer) 1
127.0.0.1:6379> SISMEMBER badges:asha night-owl
(integer) 0
127.0.0.1:6379> SMISMEMBER badges:asha first-win night-owl streak-3
1) (integer) 1
2) (integer) 0
3) (integer) 1
127.0.0.1:6379> SCARD badges:asha
(integer) 3
127.0.0.1:6379> SCARD badges:nobody
(integer) 0SISMEMBER key memberreplies1if the member is in the set,0if not. A missing key is treated as an empty set, so the answer is0, not an error.SMISMEMBER key member [member ...](Redis 6.2+) checks several members at once and replies with one1or0per member, in the order you asked.SCARD keyreplies with the number of members (the set's cardinality);0for a missing key.
Why not SMEMBERS? Fetching every member just to check one sends the whole set across the network and makes your app search it. SISMEMBER is a direct lookup that takes about the same time whether the set has ten members or ten million, and SCARD reads a count Redis already keeps.
Two more commands pick members at random: SRANDMEMBER key returns one without removing it (a random "word of the hour"), and SPOP key removes and returns one (drawing a prize winner who cannot win twice).
The shortcut
Match the question to the command: "Is X in it?" → SISMEMBER. "Are these in it?" → SMISMEMBER. "How many?" → SCARD. "Give me all" → only then SMEMBERS.
The question
"Has
orbitalready been guessed?" It is a yes-or-no about one member.
The trap is the reverse question. SISMEMBER checks one member in one set; it cannot tell you which sets contain a member. If you need "which rooms is Asha in?", keep a second set per player, such as rooms:asha, and update both sets together whenever she joins or leaves.
Drills
Answer fast — trust the rule.
guessed:today does not exist. What does SISMEMBER guessed:today orbit reply?
badges:asha holds first-win, streak-3, speedster. What does SMISMEMBER badges:asha speedster first-win zzz reply?
What does SCARD reply for a set after SADD s a b c then SADD s c d?
Which command draws a random prize winner from entrants so they cannot be drawn again?
Remember
- Ask the set a question; don't fetch the set to answer it.
- In it? SISMEMBER. Several? SMISMEMBER. How many? SCARD.
- A missing key is an empty set: 0, never an error.
