Introduction to the
  • Collections store multiple disparate elements in an unordered fashion
  • You can quickly add elements to or delete elements from a set, and you can perform set operations on multiple sets: union, intersection, and difference
  • Equivalent to Set in Java

Do not use collections to store ordered data. If you want to store ordered and repeated values, you can use lists. If you want to store ordered and non-repetitive values, you can use ordered collections

Element operation
  • SADD key element [element …] To add one or more elements to a given collection, existing elements are automatically ignored, and the command returns the number of new elements added to the collection

    127.0.0.1:6379> sadd set hi hello haha hehe
    (integer) 4
    127.0.0.1:6379> sadd set hi 
    (integer) 0
    Copy the code
  • SREM key element [element …] Removes one or more elements from the collection. Elements that do not exist in the collection are automatically ignored. The command returns the number of existing and removed elements

    127.0.0.1:6379> srem set hi hello world
    (integer) 2
    Copy the code
  • SISMEMBER Key Element checks if the given element exists in the collection and returns 1 if it does. If the element does not exist, or the given key does not exist, then returns 0

    127.0.0.1:6379> sismember set hello 
    (integer) 0
    127.0.0.1:6379> sismember set hehe
    (integer) 1
    Copy the code
  • SCARD key returns the number of elements contained in the collection

    127.0.0.1:6379> scard set
    (integer) 2
    Copy the code
  • The SMEMBERS key returns all the elements contained in the collection

    127.0.0.1:6379> smembers set
    1) "haha"
    2) "hehe"
    Copy the code

    When the cardinality of the collection is large, executing this command may cause server blocking. For the same collection element, the same collection command may return a different result: unordered collection

  • The SPOP key is randomly removed from the collection and returns an element

    127.0.0.1:6379> spop set
    "haha"
    127.0.0.1:6379> spop set
    "hehe"
    127.0.0.1:6379> spop set
    (nil)
    Copy the code
  • SRANDMEMBER key [count] returns a random element from the set. If no optional count argument is given, the command returns a random element from the set. • When count is positive and less than the cardinality of the set, the command returns an array of count elements, each of which is different. If count is greater than or equal to the cardinality of the collection, the command returns the entire collection • When count is negative, the command returns an array whose elements may be repeated multiple times and whose length is the absolute value of count

    Unlike SPOP, SRANDMEMBER does not remove returned elements

    127.0.0.1:6379> srandMember set "world" 127.0.0.1:6379> srandMember set 10 1) "Hello" 2) "hi" 3) "haha" 4) "world" 5) "Hehe" 127.0.0.1:6379> srandMember set-10 1) "world" 2) "hello" 3) "hello" 4) "hehe" 5) "hello" 6) "world" 7) "world" 8) "world" 9) "hi" 10) "hi"Copy the code
Set operation
  • SDIFF key [key …] Computes the difference set of all given sets and returns the result

    127.0.0.1:6379> sadd num1 123 456 666
    (integer) 3
    127.0.0.1:6379> 
    127.0.0.1:6379> sadd num2 123 456 777 888
    (integer) 4
    127.0.0.1:6379> sdiff num1 num2
    1) "666"
    127.0.0.1:6379> sdiff num2 num1
    1) "777"
    2) "888"
    Copy the code
  • SDIFFSTORE destkey key [key …] Calculates the difference set of all the given sets and stores the result in destkey

    127.0.0.1:6379> sdiffStore num3 num2 num1 (integer) 2 127.0.0.1:6379> smembers num3 1) "" 2)"Copy the code
  • SINTER key [key …] Computes the intersection of all given sets and returns the result

    127.0.0.1:6379> sinter num1 num2
    1) "123"
    2) "456"
    127.0.0.1:6379> sinter num2 num1
    1) "123"
    2) "456"
    Copy the code
  • SINTERSTORE destkey key [key …] Computes the intersection of all given sets and stores the result in destkey

    127.0.0.1:6379> sinterstore num4 num1 num2
    (integer) 2
    127.0.0.1:6379> smembers num4
    1) "123"
    2) "456"
    Copy the code
  • SUNION key [key …] Computes the union of all given sets and returns the result

    127.0.0.1:6379> sunion num1 num2
    1) "123"
    2) "456"
    3) "666"
    4) "777"
    5) "888"
    127.0.0.1:6379> sunion num2 num1
    1) "123"
    2) "456"
    3) "666"
    4) "777"
    5) "888"
    Copy the code
  • SUNIONSTORE destkey key [key …] Computes the union of all given sets and stores the result in destkey

    127.0.0.1:6379> sunionstore num5  num2 num1
    (integer) 5
    127.0.0.1:6379> smembers num5
    1) "123"
    2) "456"
    3) "666"
    4) "777"
    5) "888"
    Copy the code