Redis Sets Commands

Author: Al-mamun Sarkar Date: 2020-09-21 13:27:09

Redis set is a list of unique values used to store sets data structure in Redis under a key. I this lesson I will show the commands of the Redis sets.

SADD command is used to create a new set or add an item at the set:

Syntax:

SADD key value1 value2 ...

Example:

SADD numbers 1 3 4 3

 

Some Other Commands of Sets:

Commands Description  Syntax Example
Adding elements to set SADD key value1 value2 ... ADD rolls 9 2 3 4 6 5 6 7
Getting all elements of a set SMEMBERS key SMEMBERS numbers
Getting the number of members of a set SCARD key SCARD numbers
Getting difference among multiple sets SDIFF key1 key2 ... SDIFF rolls numbers
Store the difference among multiple sets to a new set SDIFFSTORE destination key1 key2 .. SDIFFSTORE diif rolls numbers
Getting intersect SINTER key1 key2 ... SINTER rolls numbers
Store intersects into another set SINTERSTORE destination key1 key2 ... SINTERSTORE commons rolls numbers
Checking is a value is a member of a set or not SISMEMBER key member SISMEMBER numbers 3
Transfer a member value from one set to another SMOVE source destination member SMOVE rolls numbers 9
Remove and get a random number from a set SPOP key SPOP numbers
Getting one or many members from a set SRANDMEMBER key SRANDMEMBER numbers
Remove one or multiple members from a set SREM key member1 [member2] SREM rolls 2 3
Get the union of multiple sets SUNION key1 key2 .... SUNION rolls numbers
Store union of multiple sets to a set  SUNIONSTORE destination key1 [key2] SUNIONSTORE all rolls numbers
Iterate sets SSCAN key cursor [MATCH pattern] [COUNT count] SSCAN numbers 0 match *