You have to work really hard to look effortless!

Wechat search public number [long Coding road], together From Zero To Hero!

preface

A Set, or Set, is consistent with the concept of a Set in mathematics, except that Redis sets contain elements of type String. The values of elements in a Set are unique and cannot be duplicated. Let’s take a look at the instructions related to the Set base type.

SADD

Available versions: >= 1.0.0

Time complexity: to add one element is O(1), to add N elements is O(N)

The command format

SADD key member [member ...]
Copy the code

Command description

  • Will one or morememberElement to thekeyIn the corresponding set, if the set already existsmemberElement, this operation is ignored
  • ifkeyThe corresponding set doesn’t exist, an empty set is created, and then thememberElement insert
  • ifkeyThe corresponding is not a collection type, and an error is returned

The return value

Integer value: number of elements actually inserted into the list (elements that already exist in the list are not inserted)

The sample

#Add an element
127.0.0.1:6379> sadd myset hello
(integer) 1

#Add two elements, 'hello' already exists, only one will be added
127.0.0.1:6379> sadd myset hello world
(integer) 1

#View all elements
127.0.0.1:6379> smembers myset
1) "hello"
2) "world"
Copy the code

SMEMBERS

Available versions: >= 1.0.0

Time complexity: O(N), where N is set size

The command format

SMEMBERS key
Copy the code

Command description

  • returnkeyIt corresponds to all the elements in the set
  • This command will return all elements. If the collection is large, it will affect Redis performanceSSCANCommand instead of

The return value

Array: All the elements in a collection

The sample

127.0.0.1:6379> sadd myset hello world
(integer) 1

#View all elements
127.0.0.1:6379> smembers myset
1) "hello"
2) "world"
Copy the code

SISMEMBER

Available versions: >= 1.0.0

Time complexity: O(1)

The command format

SISMEMBER key member
Copy the code

Command description

  • Determines whether member is an element in the key collection

The return value

1: Member is an element in a collection

0: The collection does not exist or member is not an element of the collection

The sample

127.0.0.1:6379> sadd myset hello world
(integer) 0

#It's not a member of the set
127.0.0.1:6379> sismember myset west
(integer) 0

#Is a member of the set
127.0.0.1:6379> sismember myset hello
(integer) 1
Copy the code

SMISMEMBER

Available version: >= 6.2.0

Time complexity: O(N), where N is the number of elements provided

The command format

SMISMEMBER key member [member ...]
Copy the code

Command description

  • Determines whether the given members are elements of the key set

  • For each member element, return 1 to indicate that it is in the set key, or 0 if it is not in the set or the set itself does not exist

The return value

Array: Each value in an array corresponds to whether the corresponding member is an element of the collection. The length of the array is the same as the number of members given, and the positions correspond one to one.

The sample

127.0.0.1:6379> sadd member_set hello world
(integer) 2

#Hello is in the set127.0.0.1:6379> smismember member_set hello lifelmy 1) (INTEGER) 12) (INTEGER) 0Copy the code

SPOP

Available versions: >= 1.0.0

Time complexity: O(1) if no count value is provided; When the count value is supplied, the complexity is O(N), where N is the number of returned elements

The optional parameter count was added after version 3.2.0

The command format

SPOP key [count]
Copy the code

Command description

  • Removes an element at random from the collection and returns its value

  • If you just want to return elements without removing them, use the SRANDMEMBER command

  • By default, remove and return one element. If you specify count, return count. If count is greater than the length of the collection, all elements in the collection are returned

The return value

If count is not specified: return element value or nil (key does not exist)

When specifying count: collection of removed elements or empty collection (key does not exist)

The sample

127.0.0.1:6379> sadd myset1 hello
(integer) 1
127.0.0.1:6379> sadd myset1 world
(integer) 1
#Randomly remove and return a value
127.0.0.1:6379> spop myset1
"world"

127.0.0.1:6379> sadd myset1 lifelmy
(integer) 1
127.0.0.1:6379> smembers myset1
1) "lifelmy"
2) "hello"

#When count is greater than the set length127.0.0.1:6379> spop myset1 1) "lifelmy" 2) "hello"
#A set that doesn't exist
127.0.0.1:6379> spop notexist 2
(empty array)
Copy the code

SRANDMEMBER

Available versions: >= 1.0.0

Time complexity: O(1) if no count value is provided; When the count value is supplied, the complexity is O(N), where N is the number of returned elements

The command format

SRANDMEMBER key [count]
Copy the code

Command description

  • When no count value is specified, the value of an element in the collection is randomly returned

  • If count is positive and less than or equal to the length of the set, return a set of size count with different elements; If count is greater than the set length, all elements in the set are returned

  • If count is negative, then the command returns an array whose elements may be repeated many times, and whose length is the absolute value of count.

The return value

When count is not specified: return random element value or nil (if set does not exist)

When count is specified: the collection of elements returned or the empty collection (if the collection does not exist)

The sample

127.0.0.1:6379> sadd myset2 hello
(integer) 1
127.0.0.1:6379> sadd myset2 world
(integer) 1

#The count parameter is not specified127.0.0.1:6379 > srandmember myset2 "hello"
#Count is greater than the list length
127.0.0.1:6379> srandmember myset2 3
1) "hello"
2) "world"

#The count is negative127.0.0.1:6379> srandmember myset2-3 1) "hello" 2) "world"Copy the code

SREM

Available versions: >= 1.0.0

Time complexity: O(N), where N is the number of elements given

The command format

SREM key member [member ...]
Copy the code

Command description

  • Removes the given element value member from the collection

  • When the given element value member is not a member of the collection, the element is ignored

  • If the given key set does not exist, it is treated as an empty set

  • Returns an error when the key is not of a collection type

The return value

Integer value: the number of elements actually removed from the collection

The sample

127.0.0.1:6379> sadd myset3 hello
(integer) 1
127.0.0.1:6379> sadd myset3 world
(integer) 1
127.0.0.1:6379> smembers myset3
1) "hello"
2) "world"

#Remove an element
127.0.0.1:6379> srem myset3 hello
(integer) 1
127.0.0.1:6379> smembers myset3
1) "world"

#Remove elements that do not exist
127.0.0.1:6379> srem myset3 lifelmy
(integer) 0
127.0.0.1:6379> smembers myset3
1) "world"
Copy the code

SMOVE

Available versions: >= 1.0.0

Time complexity: O(1)

The command format

SMOVE source destination member
Copy the code

Command description

  • willsourceElements of a setmember, move to the setdestinationIn the
  • This is an atomic operation, and at any given moment,memberEither insourceIn, or indestinationIn the
  • If the collectionsourceDoes not exist, orsourceNot in the setmemberElement, does nothing but return 0
  • ifdestinationIt already exists in the setmemberElement value, then only thesourceIn thememberremove
  • ifsourceordestinationReturn error if the corresponding element is not of type SET

The return value

1: The element is moved successfully

0: The source collection does not contain the member element, and no operation is performed

The sample

127.0.0.1:6379> sadd myset4 hello world
(integer) 2
127.0.0.1:6379> sadd myset5 hello
(integer) 1

#Mobile success
127.0.0.1:6379> smove myset4 myset5 world
(integer) 1

#Myset4 removes an element that myset5 already contains and does not add
127.0.0.1:6379> smove myset4 myset5 hello
(integer) 1

#Viewing collection elements127.0.0.1:6379> smembers myset4 (empty array) 127.0.0.1:6379> smembers myset5
#Myset4 does not have 'hello' and returns 0
127.0.0.1:6379> smove myset4 myset5 hello
(integer) 0
Copy the code

SCARD

Available versions: >= 1.0.0

Time complexity: O(1)

The command format

SCARD key
Copy the code

Command description

  • Returns the cardinality (length) of the set

The return value

Integer value: the length of the set. Return 0 if the set does not exist

The sample

127.0.0.1:6379> sadd myset6 hello world
(integer) 2

#Return set length
127.0.0.1:6379> scard myset6
(integer) 2

#A nonexistent collection returns 0
127.0.0.1:6379> scard notexists
(integer) 0
Copy the code

SSCAN

Available versions: >= 2.8.0

Time complexity: the time complexity of each call is O(1), the complete traversal is O(N), and N is the number of set elements

The command format

SSCAN key cursor [MATCH pattern] [COUNT count]
Copy the code

Command description

  • Traverse the collection
  • For details about the commands, see subsequent database operationsSCANPart of the

More and more

Personal blog: lifelmy.github. IO /

Wechat official Account: Long Coding road