Redis study notes 5 (set)

Source: Internet
Author: User
Redis study note 5 (set) I. Overview: In Redis, we can view the Set type as a character set 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. It must be noted that the time complexity of these operations is O (1), that is, the operation is completed within the constant time.

Redis study note 5 (set) I. Overview: In Redis, we can view the Set type as a character set 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. It must be noted that the time complexity of these operations is O (1), that is, the operation is completed within the constant time.

Redis study notes 5 (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

In SADD key member [member...] O (N) time complexity, N indicates the number of members of the operation. 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.

SCARD key O (1) gets the number of members in the Set. Returns the number of members in the Set. If the Key does not exist, 0 is returned.

SISMEMBER key member O (1) determines whether the specified member in the parameter already exists in the Set 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.

In SMEMBERS key O (N) time complexity, N 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.

SPOP key O (1) is randomly removed and a member in the Set is returned. 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.

In the SREM key member [member...] O (N) time complexity, N 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.

Like SPOP, SRANDMEMBER key O (1) randomly returns a member in the Set. The difference is that this command does not delete the returned member. Returns a random member. If the Key does not exist, returns nil.

SMOVE source destination member O (1) atomically transfers 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.

In SDIFF key [key...] O (N) time complexity, N 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.

SDIFFSTORE destination key [key...] O (N) The command and the SDIFF command have the same functions. The only difference between the two is the result Member of the SDIFF returned difference, this command stores the different members in the Set associated with destination. If the destination key already exists, this operation overwrites its members. Returns the number of different members.

In the time complexity of SINTER key [key...] O (N * M), N represents the number of elements in the minimum Set, and M represents 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.

SINTERSTORE destination key [key...] O (N * M) The command and the SINTER command have the same functions. The only difference between the two is the result Member of the SINTER returned intersection, this command stores the intersection members in the Set associated with destination. If the destination key already exists, this operation overwrites its members. Returns the number of intersection members.

In SUNION key [key...] O (N) time complexity, N 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.

SUNIONSTORE destination key [key...] O (N) The command and the SUNION command have the same functions. The only difference between the two is the result Member of the Union returned by SUNION, 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, 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.

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). 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.

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.