Redis learning Manual (set data type)

Source: Internet
Author: User
Tags set set

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 under the shell command lineProgram.
/> 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, only new members D and E are inserted in this operation.
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 already exists.
Redis 127.0.0.1: 6379> Sismember myset
(Integer) 1
# Determine whether F already exists. If the returned value is 0, it indicates that F does not exist.
Redis 127.0.0.1: 6379> Sismember myset F
(Integer) 0
# Run the smembers command to view the insert result. From the result, the output sequence is irrelevant to the insert sequence.
Redis 127.0.0.1: 6379> Smembers myset
1) "C"
2) "D"
3) ""
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 the 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 a set member.
Redis 127.0.0.1: 6379> Smembers myset
1) "C"
2) "D"
3) ""
4) "B"
# The result shows that the 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) ""
# Remove members A, D, and F from the set, where 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
(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
(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) ""

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 with myset2, the 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 return 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 three sets of members.
Redis 127.0.0.1: 6379> Sunion myset myset2 myset3
1) "B"
2) "C"
3) "D"
4) "e"
5) ""
# 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). makes full use of the Set-type server-side aggregation operation, which is convenient and efficient, and can be used to maintain 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.

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.