Redis learning (four) set collection

I. Overview

Set String type is an unordered collection. It is the only member of the collection, which means that the collection of duplicate data can not appear.

Redis is achieved through the collection of the hash table, so add, delete, search complexity is O (1).

The maximum number of members for the collection 232--1 (4,294,967,295, each set can store 40 million members).

Second, the commonly used commands

    Example: sadd set val1 ... valn

    Usage: one or more members of the elements are added to the collection, already exists in the collection of member elements are ignored. If the set key does not exist, create a collection that contains only add elements as members. When the set key is not a collection type, it returns an error.

    Returns: The number is added to the collection of the new element, not including the neglected element.

 

    Example: srem set val1 ... valn

    Usage: used to remove one or more members of the elements in the collection, members of the element does not exist will be ignored. When the key is not a collection type, it returns an error.

    Returns: the number of elements to be removed successfully, excluding overlooked element.

 

    Example: smembers set

    Usage: Returns a collection of all the elements, no element is the empty set

    Returns: All the members of the collection

 

    Example: scard set

    Usage: Get set the number of members

    Returns: the number of collection. When the set key is not present, it returns 0.

 

    示例:sdiff FIRST_KEY OTHER_KEY1..OTHER_KEYN

    Usage: Returns to the difference between a given set of set. There is no set of key will be treated as an empty set. Result of the difference from the previous set of FIRST_KEY, rather than behind OTHER_KEY1, not the entire difference FIRST_KEY OTHER_KEY1..OTHER_KEYN set.

    Returns: A list of the members of the poor set.

127.0.0.1:6379> sadd key1 a b c
(integer) 3
127.0.0.1:6379> sadd keys c d e
(integer) 3
127.0.0.1:6379> sdiff key1 keys
1) "a"
2) "b"
127.0.0.1:6379> 

    Example: sunion set1 set2

    Usage: Returns a collection of union. There is no set of key as an empty set.

    Returns: the union member list

 

    Example: smove source dest member

    Usage: The element specifies the members of the member to move the collection from source to destination collection. SMOVE atomic operation.

               If the source does not exist or does not contain a collection of member element specified, the SMOVE command does not perform any operation, only returns 0. Otherwise, member elements are removed from the source collection, and added to the destination set to go.

                When the destination already contains a set of member elements, SMOVE command simply the member element source collection deletion.

                 When the source or destination type is not set, an error is returned.

    Returns: If the members of the element is successfully removed, returns 1. If the element is not a member of member of a set of source and destination without any operation on the set of execution, it returns 0.

 

    Example: sinter set1 set2

    Usage: all given back to the intersection of a given collection. There is no set of key as an empty set. When there is an empty set among a set of given, the result is the empty set (set operation according to a given

    Returns: Intersection members list

 

    Example: sismember set key

    Usage: to determine whether the element is a member of a member of the collection.

    Returns: return 1 is a member, is not a member or key does not exist return 0

 

    Example: sdiffstore / sinter / sunion dest set1 set2

    Usage: given a set of difference between the current collector / intersection / set and stored in the specified set. If you specify a set of key already exists, it will be overwritten.

    Returns: the set difference / intersection / union member list

 

127.0.0.1:6379> sadd message open full paint key close switch    添加多个val
(integer) 6
127.0.0.1:6379> smembers message                      列出所有的val
1) "paint"
2) "full"
3) "open"
4) "key"
5) "close"
6) "switch"
127.0.0.1:6379> sadd set1 a b c w e r
(integer) 6
127.0.0.1:6379> sadd set2 c w e r k l
(integer) 6
127.0.0.1:6379> sdiff set1 set2                          差集
1) "b"
2) "a"
127.0.0.1:6379> sinter set1 set2                         交集
1) "e"
2) "w"
3) "r"
4) "c"
127.0.0.1:6379> sunion set1 set2                         并集
1) "b"
2) "e"
3) "a"
4) "k"
5) "w"
6) "r"
7) "l"
8) "c"
127.0.0.1:6379> sdiffstore diffset set1 set2            交集转储(并集、差集同理)
(integer) 2
127.0.0.1:6379> smembers diffset
1) "b"
2) "a"
127.0.0.1:6379>

 

Published 22 original articles · won praise 9 · views 8816

Guess you like

Origin blog.csdn.net/ljm_c_bok/article/details/104881501