Redis basic data type (set collection)

set collection

Storage type

Store an unordered collection of string type, the maximum storage quantity is 2^32-1 (about 4 billion)

Common operation commands

sadd: add elements to the set of the specified key

  • command

    SADD key member [member ...]
    
  • Description

    Add one or more specified member elements to the set key. If the specified one or more elements already exist in the set key, ignore it. If the set key does not exist, create a new set key and add the member element to the set key.

    If the type of key is not a collection, an error is returned.

  • return value

    Returns the number of newly successfully added elements to the collection, excluding elements that already exist in the collection.

  • example

    redis> SADD myset "Hello"
    (integer) 1
    redis> SADD myset "World"
    (integer) 1
    redis> SADD myset "World"
    (integer) 0
    redis> SMEMBERS myset
    1) "World"
    2) "Hello"
    redis> 
    

scard: Get the number of collection elements

  • command

    SCARD key
    
  • Description

    Returns the cardinality of the key stored in the collection (the number of collection elements).

  • return value

    The cardinality of the set (the number of elements), if the key does not exist, it returns 0.

  • example

    redis> SADD myset "Hello"
    (integer) 1
    redis> SADD myset "World"
    (integer) 1
    redis> SCARD myset
    (integer) 2
    redis> 
    

sdiff: Returns the element of the difference between a set and a given set.

  • command

    SDIFF key [key ...]
    
  • return value

    Elements of the result set

  • example

    redis> SADD key1 "a"
    (integer) 1
    redis> SADD key1 "b"
    (integer) 1
    redis> SADD key1 "c"
    (integer) 1
    redis> SADD key2 "c"
    (integer) 1
    redis> SADD key2 "d"
    (integer) 1
    redis> SADD key2 "e"
    (integer) 1
    redis> SDIFF key1 key2
    1) "a"
    2) "b"
    redis> 
    

sdiffstore: store the elements of the difference between a set and a given set into another set

  • command

    SDIFFSTORE destination key [key ...]
    
  • return value

    This command is similar to SDIFF , the difference is that the command does not return a result set, but stores the result in the destinationset.

    If it destinationalready exists, it will be overwritten.

  • example

    redis> SADD key1 "a"
    (integer) 1
    redis> SADD key1 "b"
    (integer) 1
    redis> SADD key1 "c"
    (integer) 1
    redis> SADD key2 "c"
    (integer) 1
    redis> SADD key2 "d"
    (integer) 1
    redis> SADD key2 "e"
    (integer) 1
    redis> SDIFFSTORE key key1 key2
    (integer) 2
    redis> SMEMBERS key
    1) "b"
    2) "a"
    redis> 
    

sinter: Get the intersection of the specified set

  • command

    SINTER key [key ...]
    
  • Description

    If the key does not exist, it is considered to be an empty set. When the given set is empty, the result is also empty. (A set is empty, and the result is always empty).

  • return value

    The list of members of the result set.

  • example

    redis> SADD key1 "a"
    (integer) 1
    redis> SADD key1 "b"
    (integer) 1
    redis> SADD key1 "c"
    (integer) 1
    redis> SADD key2 "c"
    (integer) 1
    redis> SADD key2 "d"
    (integer) 1
    redis> SADD key2 "e"
    (integer) 1
    redis> SINTER key1 key2
    1) "c"
    redis> 
    

sinterstore: store the intersection of the specified collection in another collection

  • command

    SINTERSTORE destination key [key ...]
    
  • Description

    This command is SINTERsimilar to the command, but instead of returning the result set directly, it saves the result in the destination set.

    If the destination set exists, it will be overwritten.

  • return value

    The number of members in the result set.

  • example

    redis> SADD key1 "a"
    (integer) 1
    redis> SADD key1 "b"
    (integer) 1
    redis> SADD key1 "c"
    (integer) 1
    redis> SADD key2 "c"
    (integer) 1
    redis> SADD key2 "d"
    (integer) 1
    redis> SADD key2 "e"
    (integer) 1
    redis> SINTERSTORE key key1 key2
    (integer) 1
    redis> SMEMBERS key
    1) "c"
    redis> 
    

sismember: Determine whether the set contains the specified element

  • command

    SISMEMBER key member
    
  • Description

    Returns whether the member member is a member of the stored set key.

  • return value

    • 1-member element is a member of the set key
    • 0-member element is not a member of key, or set key does not exist
  • example

    redis> SADD myset "one"
    (integer) 1
    redis> SISMEMBER myset "one"
    (integer) 1
    redis> SISMEMBER myset "two"
    (integer) 0
    redis> 
    

smembers: Get all elements of the collection

  • command

    SMEMBERS key
    
  • Description

    Return all the elements of the key set.

    The effect of this SINTERcommand is the same as that of a command that uses one parameter .

  • return value

    All elements in the collection.

  • example

    redis> SADD myset "Hello"
    (integer) 1
    redis> SADD myset "World"
    (integer) 1
    redis> SMEMBERS myset
    1) "World"
    2) "Hello"
    redis> 
    

smove: Move the specified element from one collection to another collection

  • command

    SMOVE source destination member
    
  • Description

    Move member from the source collection to the destination collection. For other clients, the element will appear as a member of the source or destination collection at a specific time.

    If the source collection does not exist or does not contain the specified element, the move command does nothing and returns 0. Otherwise, the object will be removed from the source collection and added to the destination collection. If the element already exists in the destination collection, The move command only removes the element from the source collection. If source and destination are not collection types, an error is returned.

  • return value

    • 1-The element is successfully removed 1
    • 0-the element is not a member of the source collection, no operation
  • example

    redis> SADD myset "one"
    (integer) 1
    redis> SADD myset "two"
    (integer) 1
    redis> SADD myotherset "three"
    (integer) 1
    redis> SMOVE myset myotherset "two"
    (integer) 1
    redis> SMEMBERS myset
    1) "one"
    redis> SMEMBERS myotherset
    1) "three"
    2) "two"
    redis> 
    

spop: randomly remove a specified number of elements from the set

  • command

    SPOP key [count]
    
  • Description

    keyRemove and return one or more random elements from the stored set.

    countParameters will be provided in later versions, but are not available in 2.6, 2.8, and 3.0.

  • example

    redis> SADD myset "one"
    (integer) 1
    redis> SADD myset "two"
    (integer) 1
    redis> SADD myset "three"
    (integer) 1
    redis> SPOP myset
    1) "three"
    redis> SMEMBERS myset
    1) "one"
    2) "two"
    redis> SADD myset "four"
    (integer) 1
    redis> SADD myset "five"
    (integer) 1
    redis> SPOP myset 3
    1) "one"
    2) "four"
    3) "two"
    redis> SMEMBERS myset
    1) "five"
    

srandmember: Randomly query a specified number of elements from the set

  • command

    SRANDMEMBER key [count]
    
  • Description

    Provide only the key parameter, then randomly return an element in the key set.

    Starting from Redis 2.6, the count parameter can be accepted. If count is an integer and less than the number of elements, an array containing count different elements will be returned. If count is an integer and greater than the number of elements in the collection, only all of the entire collection will be returned. Element, when count is a negative number, it will return an array containing the absolute value of count elements. If the absolute value of count is greater than the number of elements, there will be multiple occurrences of an element in the returned result set.

    When only the key parameter is provided, the function of this command is similar to the SPOP command, except that the SPOP command will remove the selected random element from the set, while SRANDMEMBER only returns the random element without doing any operation.

  • return value

    If the count parameter is not used, the command returns a random element, or nil if the key does not exist.

    Use the count parameter to return a random array of elements, or an empty array if the key does not exist.

  • example

    redis> SADD myset one two three
    (integer) 3
    redis> SRANDMEMBER myset
    "one"
    redis> SRANDMEMBER myset 2
    1) "three"
    2) "one"
    redis> SRANDMEMBER myset -5
    1) "one"
    2) "one"
    3) "one"
    4) "one"
    5) "one"
    redis> 
    

rpoplpush: Put the tail element of one list at the head of another list

  • command

    RPOPLPUSH source destination
    
  • Description

    Atomically return and remove the last element of the list stored in source (the last element of the list), and put this element in the first element position of the list stored in the destination (the head of the list).

    For example: Suppose source stores the lists a, b, and c, and destination stores the lists x, y, and z. The result of executing RPOPLPUSH is that the source stores the lists a, b, and the destination stores the lists c, x, y, and z.

    If the source does not exist, it will return a nil value and do nothing. If the source and destination are the same, then this operation is equivalent to removing the last element of the list and placing that element at the head of the list, so this command can also be regarded as a command to rotate the list.

  • return value

    Elements removed and put in

  • example

    redis> RPUSH mylist "one"
    (integer) 1
    redis> RPUSH mylist "two"
    (integer) 2
    redis> RPUSH mylist "three"
    (integer) 3
    redis> RPOPLPUSH mylist myotherlist
    "three"
    redis> LRANGE mylist 0 -1
    1) "one"
    2) "two"
    redis> LRANGE myotherlist 0 -1
    1) "three"
    redis> 
    

srem: remove the specified element in the collection

  • command

    SREM key member [member ...]
    
  • Description

    Remove the specified element from the key set. If the specified element is not an element in the key set, it will be ignored. If the key set does not exist, it will be regarded as an empty set. The command returns 0.

    If the type of key is not a set, an error is returned.

  • return value

    The number of elements removed from the set, excluding non-existent members.

  • example

    redis> SADD myset "one"
    (integer) 1
    redis> SADD myset "two"
    (integer) 1
    redis> SADD myset "three"
    (integer) 1
    redis> SREM myset "one"
    (integer) 1
    redis> SREM myset "four"
    (integer) 0
    redis> SMEMBERS myset
    1) "three"
    2) "two"
    redis> 
    

sunion: Get the union of the specified set

  • command

    SUNION key [key ...]
    
  • Description

    Returns all members in the union of the given multiple sets.

    A key that does not exist can be considered an empty set.

  • example

    redis> SADD key1 "a"
    (integer) 1
    redis> SADD key1 "b"
    (integer) 1
    redis> SADD key1 "c"
    (integer) 1
    redis> SADD key2 "c"
    (integer) 1
    redis> SADD key2 "d"
    (integer) 1
    redis> SADD key2 "e"
    (integer) 1
    redis> SUNION key1 key2
    1) "a"
    2) "b"
    3) "c"
    4) "d"
    5) "e"
    redis> 
    

sunionstore: Store the union of the specified set in another set

  • command

    SUNIONSTORE destination key [key ...]
    
  • Description

    The function of this command is similar to the SUNION command, the difference is that it does not return a result set, but stores the result in the destination set.

    If the destination already exists, it will be overwritten.

  • return value

    The number of elements in the result set.

  • example

    redis> SADD key1 "a"
    (integer) 1
    redis> SADD key1 "b"
    (integer) 1
    redis> SADD key1 "c"
    (integer) 1
    redis> SADD key2 "c"
    (integer) 1
    redis> SADD key2 "d"
    (integer) 1
    redis> SADD key2 "e"
    (integer) 1
    redis> SUNIONSTORE key key1 key2
    (integer) 5
    redis> SMEMBERS key
    1) "c"
    2) "e"
    3) "b"
    4) "a"
    5) "d"
    redis>
    

Guess you like

Origin blog.csdn.net/huangge1199/article/details/112331907