Redis data type -- Set

Source: Internet
Author: User
-- Reprinted:

I. Overview:

In redis, we can view the set type as a character set combination without sorting, which is the same as the list type, you can also add, delete, or determine whether an element exists on the data value of this type. Note that the time complexity of these operations is O (1), that is, the operation is completed within the constant time. The maximum number of elements that a set can contain is 4294967295.
Unlike the list type, duplicate elements are not allowed in the Set set, which is exactly the same as the set container in the C ++ standard library. In other words, if the same element is added multiple times, only one copy of the element is retained in set. Compared with the list type, the set type also has a very important feature in terms of functionality, that is, completing aggregation computing operations between multiple sets on the server side, such as unions, intersections, and differences. Because these operations are completed on the server, the efficiency is extremely high, and it also saves a lot of network I/O overhead.

Ii. Related command list:

Command prototype Time Complexity Command description Return Value
SaddKey member [member...] O (N) N in time complexity indicates the number of Operation members. If a member in the parameter already exists in the set during the insertion process, the member will be ignored, and other Members will still be inserted normally. If the key does not exist before executing the command, the command creates a new set and inserts the members in the parameter one after another. If the value of the key is not of the set type, the command returns the relevant error message. The number of actually inserted members in this operation.
ScardKey O (1) Obtains the number of members in a set. Returns the number of members in the set. If the key does not exist, 0 is returned.
SismemberKey Member O (1) Determine whether the specified member in the parameter already exists in the Set set that is associated with the key. 1 indicates that the key already exists, 0 indicates that the key does not exist, or the key itself does not exist.
SmembersKey O (N) N in time complexity indicates the number of existing members in the set. Obtain all the members of the set associated with the key.

Returns all the members in the set.

SpopKey O (1) Randomly remove and return a Member from the set. Because the layout of elements in the set is not subject to external control, it is impossible to determine which element is located at the header or tail of the set as the list. The removed member is returned. If the key does not exist, Nil is returned.
SremKey member [member...] O (N) N in time complexity indicates the number of deleted members. Delete the specified member from the set associated with the key. A nonexistent parameter member is ignored. If the key does not exist, it is treated as a null set. Number of actually removed members from the set. If not, 0 is returned.
SrandmemberKey O (1) Like spop, a member in the set is returned randomly. The difference is that the command does not delete the returned member. Returns a random member. If the key does not exist, returns nil.
SmoveSource destination Member O (1) Atomically move the members in the parameter from the source key to the set associated with the destination key. Therefore, this member may appear in source or destination at a certain time point. If the member does not exist in the source, the command will not perform any operations and return 0. Otherwise, the member will be migrated from the source to the destination. If the member already exists in destination, this command only removes the Member from source. If the value associated with the key is not set, related error messages are returned. 1 indicates normal movement, and 0 indicates that the source does not contain parameter members.
SdiffKey [Key...] O (N) N in time complexity indicates the total number of members in all sets. Returns the differences between the set associated with the first key in the parameter and the other sets associated with all keys. If the key does not exist, it is considered as a null set. A set of differential result members.
SdiffstoreDestination key [Key...] O (N) This command and sdiff command have identical functions. The only difference between the two is that sdiff returns the result Member of the difference, and this command stores the difference member in the set associated with destination. If the destination key already exists, this operation overwrites its members. Returns the number of different members.
SinterKey [Key...] O (N * m) N in time complexity indicates the number of elements in the minimum set, and M indicates the number of sets in the parameter. This command returns the intersection of all the Members in the sets associated with keys in the parameter. Therefore, if the set associated with any key in the parameter is null, or a key does not exist, the result of this command is an empty set. Set of intersection result members.
SinterstoreDestination key [Key...] O (N * m) This command and the sinter command have identical functions. The only difference between the two is that Sinter Returns the result Member of the intersection, and this command stores the intersection member in the set associated with destination. If the destination key already exists, this operation overwrites its members. Returns the number of intersection members.
SunionKey [Key...] O (N) N in time complexity indicates the total number of members in all sets. This command returns the union of all the Members in the sets associated with keys in the parameter. The set of Union result members.
SunionstoreDestination key [Key...] O (N) This command and the sunion command have the same functions. The only difference between the two is that sunion returns the result members of the Union, and this command stores the union members in the set associated with destination. If the destination key already exists, this operation overwrites its members. Returns the number of union members.

Iii. Command example:

1. sadd/smembers/scard/sismember: # Start the redis client program under the shell command line. /> Redis-cli # insert test data. Because the key does not exist before myset, all three members of the parameter are inserted normally. Redis 127.0.0.1: 6379> sadd myset a B C (integer) 3 # Because A in the parameter already exists in myset, this operation only inserts new members D and E. Redis 127.0.0.1: 6379> sadd myset a d e (integer) 2 # determine whether a already exists. If the returned value is 1, it indicates that a exists. Redis 127.0.0.1: 6379> sismember myset A (integer) 1 # judge whether F already exists. If the returned value is 0, it indicates that it does not exist. Redis 127.0.0.1: 6379> sismember myset F (integer) 0 # Run the smembers command to view the insert result. The result is acceptable. The output sequence is irrelevant to the insert sequence. Redis 127.0.0.1: 6379> smembers myset 1) "C" 2) "D" 3) "A" 4) "B" 5) "E" # obtain the number of elements in the Set set. Redis 127.0.0.1: 6379> scard myset (integer) 5 2. spop/Srem/srandmember/smove: # Delete this key to facilitate subsequent tests. Redis 127.0.0.1: 6379> Del myset (integer) 1 # Prepare test data for the following example. Redis 127.0.0.1: 6379> sadd myset a B c d (integer) 4 # view the position of the members in the set. Redis 127.0.0.1: 6379> smembers myset 1) "C" 2) "D" 3) "A" 4) "B" # As shown in the result, this command returns a random member. Redis 127.0.0.1: 6379> srandmember myset "C" # member B at the end of set is removed and returned. In fact, B is not the first or last member inserted before. Redis 127.0.0.1: 6379> spop myset "B" # view the member information of the removed set. Redis 127.0.0.1: 6379> smembers myset 1) "C" 2) "D" 3) "A" # remove members A, D, and F from the set, F does not exist. Therefore, only members A and D are removed and 2 is returned. Redis 127.0.0.1: 6379> Srem myset a d F (integer) 2 # view the output result after removal. Redis 127.0.0.1: 6379> smembers myset 1) "C" # Prepare data for the subsequent smove commands. Redis 127.0.0.1: 6379> sadd myset a B (integer) 2 redis 127.0.0.1: 6379> sadd myset2 C D (integer) 2 # Move a from myset to myset2, the result shows that the movement is successful. Redis 127.0.0.1: 6379> smove myset myset2 A (integer) 1 # Move a from myset to myset2 again. Because a is no longer a member of myset, moving fails and 0 is returned. Redis 127.0.0.1: 6379> smove myset myset2 A (integer) 0 # Check the members of myset and myset2 respectively to check whether the movement is successful. Redis 127.0.0.1: 6379> smembers myset 1) "B" redis 127.0.0.1: 6379> smembers myset2 1) "C" 2) "D" 3) "A" 3. sdiff/sdiffstore/sinter/sinterstore: # Prepare test data for subsequent commands. Redis 127.0.0.1: 6379> sadd myset a B c d (integer) 4 redis 127.0.0.1: 6379> sadd myset2 C (integer) 1 redis 127.0.0.1: 6379> sadd myset3 A C E (integer) 3 # compared to myset and myset2, members A, B, and D are the different members of the two. Use this result to continue the comparison with myset3. B and D are non-existent members of myset3. Redis 127.0.0.1: 6379> sdiff myset myset2 myset3 1) "D" 2) "B" # store the differential members of the three sets in the set associated with the diffkey, and returns the number of inserted members. Redis 127.0.0.1: 6379> sdiffstore diffkey myset myset2 myset3 (integer) 2 # view the sdiffstore operation result. Redis 127.0.0.1: 6379> smembers diffkey 1) "D" 2) "B" # From the data prepared earlier, we can see that the intersection of the Three sets is only C. Redis 127.0.0.1: 6379> sinter myset myset2 myset3 1) "C" # store the intersection members in the three sets to the set associated with interkey, and return the number of intersection members. Redis 127.0.0.1: 6379> sinterstore interkey myset myset2 myset3 (integer) 1 # view the sinterstore operation result. Redis 127.0.0.1: 6379> smembers interkey 1) "C" # obtain the union of the members in the three sets. Redis 127.0.0.1: 6379> sunion myset myset2 myset3 1) "B" 2) "C" 3) "D" 4) "E" 5) "A" # store the union of the three sets to the set associated with the unionkey, and return the number of union members. Redis 127.0.0.1: 6379> sunionstore unionkey myset myset2 myset3 (integer) 5 # view suiionstore operation results. Redis 127.0.0.1: 6379> smembers unionkey 1) "B" 2) "C" 3) "D" 4) "E" 5) ""

 



Iv. Application Scope:

1) You can use the set data type of redis to track some unique data, such as the unique IP address used to access a blog. In this scenario, we only need to store the visitor's IP address into redis each time we access this blog. The set data type will automatically ensure the uniqueness of the IP address.
2). Make full use of the Set-type server-side aggregation operation, which is convenient and efficient and can be used to maintain the associations between data objects. For example, all customer IDs that purchase an electronic device are stored in a specified set, while those that purchase another electronic product are stored in another set, if we want to find out which customers have purchased these two products at the same time, the set intersections command can give full play to its convenience and efficiency advantages.

Redis data type -- Set

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.