Set in Redis is an unordered, non-duplicate collection data structure. It provides a series of operation commands for adding, deleting, and searching operations on Set. The following are some common commands of Set type in Redis:
-
SADD key member [member …] : Adds one or more members to the specified collection.
Example:
SADD myset value1 value2 value3
-
SCARD key : Get the number of members of the specified collection.
Example:
SCARD myset
-
SMEMBERS key : Get all members of the specified collection.
Example:
SMEMBERS myset
-
SISMEMBER key member : Determine whether the specified member exists in the set.
Example:
SISMEMBER myset value
-
SREM key member [member …] : Remove one or more members from the set.
Example:
SREM myset value1 value3
-
SPOP key : Randomly removes and returns a member of the set.
Example:
SPOP myset
-
SRANDMEMBER key [count] : Randomly obtain one or more members in the set. Count can specify the number to be returned. If count is a positive number, non-duplicate members will be returned. If count is a negative number, duplicate members will be returned.
Example:
SRANDMEMBER myset 2
-
SUNION key [key …] : Returns the union of all given sets.
Example:
SUNION set1 set2
-
SDIFF key [key …] : Returns the difference of the first set relative to other sets.
Example:
SDIFF set1 set2
-
SINTER key [key …] : Returns the intersection of the given sets.
Example:
SINTER set1 set2