preface

Yes, HERE I go again. This article is the seventh in the Redis series to examine the Set data structure. I also don’t know if my good brothers are having a good time, whether they are cold or not (Guangzhou changed from summer to winter overnight), whether they are being lazy behind my back, and those who are lazy are cheating and playing with women’s feelings (aye aye aye, can they only use their mouth, not their hands). Writing this stuff is really tiring. I can’t move my neck. . That’s pretty obvious, guys, so pay attention and add likes. Good brothers still want to stick to hand knock oh, don’t steal this lazy. All right, let’s go ahead, go ahead……

An overview of the

The Set type is also used to hold multiple string elements, but unlike the List type, in a collectionNo duplicate elements are allowed, and the elements of the set areA disorderlyCannot get elements by index subscript. Remember Set in JAVA (I don’t use JAVA as an example, I don’t know anything else)? Good brothers, look at the picture and talk (please call me soul painter).

1 command

1.1 Intra-set operations

1.1.1 Adding Elements

 ## Add elements
 sadd key element [element .]
 ## add ABC to mySet, return the number of successfully added elements
 sadd myset a b c
Copy the code

1.1.2 Deleting Elements

 Multiple ## formats can be specified
 srem key element [element .]
 ## Delete ab from myset, return the number of successful deletes
 srem myset a b
Copy the code

1.1.3 Counting the number of elements

 ## format, scard time complexity is O (1), it does not iterate over all elements of the collection, but directly uses variables inside Redis
 scard key
 ## Return the number of elements in myset
 scard myset
Copy the code

1.1.4 Determine whether elements are in the set

 # # format
 sismember key element
 ## return 1 if, 0 otherwise
 sismember myset c
Copy the code

1.1.5 Return a specified number of elements randomly from the collection

 ## format, [count] is an optional argument, default to 1 if not written
 srandmember key [count]
 ## Return two elements of myset at random
 srandmember myset 2
Copy the code

1.1.6 Randomly eject elements from the collection

 Srandmember and spop both randomly select elements from the collection. The difference is that after the spop command is executed, the elements will be removed from the collection. Srandmember does not.
 spop key
 ## Remove a random one from mySet
 spop myset
Copy the code

1.1.7 Get All elements

 # # format
 smembers key
 ## returns all elements in myset. Note that the result is unordered
 smembers myset
Copy the code

1.2 Inter-set operation

Start by setting up two collections. User :1:tag and user:2:tag, adding elements via sadd.

 ## user:1:tag
 sadd user:1:tag sunny cute handsome
 ## user:2:tag
 sadd user:2:tag beefcake cute sunny
Copy the code

1.2.1 Find the intersection of multiple sets

 # # format
 sinter key [key .]
 Select the intersection of user:1:tag user:2:tag and return sunny and cute
 sinter user:1:tag user:2:tag
Copy the code

1.2.2 Finding the union of multiple sets

 # # format
 sunion key [key .]
 ## Select user:1:tag user:2:tag and return sunny cute handsome beefcake
 sunion user:1:tag user:2:tag
Copy the code

1.2.3 Finding the difference set of multiple sets

 # # format
 sdiff key [key .]
 ## Look at the difference set of user:1:tag user:2:tag
 sdiff user:1:tag user:2:tag
Copy the code

2 Time Complexity

Picture not talk series

3 Application Scenarios

  1. Tag the user as in our example above. There are multiple labels for one person.
  2. Set of friends/followers/fans/interested people, you can use the commands above to take intersection and merge related.
  3. Random display, via srandmember to randomly return the corresponding content, like some home page to get dynamic content can be played like this.
  4. Blacklist/whitelist: For some services, you need to set the user blacklist, IP address blacklist, and device blacklist for security purposes. The set type is suitable for storing these blacklist data. The sismember command can be used to determine whether the user, IP address, and device are in the blacklist.

That will do for this issue, there is not welcome good brothers comment area, additional attention and thumb up \ color # FF0000} {{attention and thumb up} attention and thumb up

3, Redis Sorted Set application scenario, API parsing previous article :Redis List application scenario, API parsing