Redis Tutorial (V): Set data type _lua

Source: Internet
Author: User
Tags character set new set redis set set redis tutorial

First, overview:

In Redis, we can look at a set type as an unordered character set, and like the list type, we can also perform actions such as adding, deleting, or determining whether an element exists on that type of data value. It should be explained that the time complexity of these operations is O (1), that is, the constant time to complete the operation. The maximum number of elements a set can contain is 4294967295.
Unlike the list type, duplicate elements are not allowed in the set collection and are identical to the set container in the C + + standard library. In other words, if you add the same element more than once, the set retains only one copy of the element. In contrast to the list type, the set type has functionally a very important feature, namely, the aggregation calculation operations between multiple sets on the server side, such as unions, intersections, and differences. Because these operations are done on the server side, they are extremely efficient and also save a significant amount of network IO overhead.

Ii. List of related commands:

Command prototypes Complexity of Time Command description return value
SaddKey member [member ...] O (N) N in the time complexity represents the number of members of the operation. If a member of the parameter already exists in the set during the insert process, the member is ignored and the other members are still inserted normally. If the key does not exist until the command is executed, the command creates a new set and then inserts the members of the parameter in succession. If the key's value is not a set type, the command returns the associated error message. The number of members actually inserted in this operation.
SCardKey O (1) Gets the number of members in the set. Returns the number of members in the set, and returns 0 if the key does not exist.
Sismember Key Member O (1) Determines whether the specified member in the parameter already exists in the set set associated with the key. 1 indicates that it already exists, 0 indicates that it does not exist, or that the key itself does not exist.
smembers Key O (N) N in the time complexity represents the number of members that already exist in the set. Gets all members of the set associated with the key.

Returns all members of the set.

SpopKey O (1) A random removal and returns a member of a set. Because the layout of the elements in the set is not externally controlled, it is not possible to determine which element is in the head or tail of the set, as in the list. Returns the removed member and returns nil if the key does not exist.
SremKey member [member ...] O (N) N In the time complexity indicates the number of members that were deleted. The member specified in the parameter is removed from the set associated with the key and the nonexistent parameter member is ignored, and if the key does not exist, it is treated as an empty set. The number of members actually removed from the set, or 0 if not.
Srandmember Key O (1) As with Spop, a member of a set is returned randomly, unlike the command, which does not delete the returned member. Returns a member of a random position and returns nil if the key does not exist.
smoveSource Destination Member O (1) Atoms move a member of a parameter from the source key into the set associated with the destination key. So at some point, the member either appears in source or appears in destination. If the member does not exist in source, the command will no longer perform any action and return 0, otherwise the member will move from source to destination. If the member already exists in destination at this point, the command simply moves the member out of source. If the value associated with the key is not set, the associated error message is returned. 1 indicates normal movement, and 0 means that the source does not contain parameter members.
Sdiffkey [key ...] O (N) N in the time complexity represents the total number of members in all sets. Returns the difference between the set associated with the first key in the parameter and the member in the sets associated with all subsequent keys. If key does not exist, it is treated as an empty set. A collection of variance result members.
sdiffstoredestination key [key ...] O (N) This command is functionally identical to the Sdiff command, and the only difference is that Sdiff returns the result member of the difference, which stores the difference member in the destination associated set. If the destination key already exists, the operation overwrites its members. Returns the number of difference members.
sinterkey [key ...] O (N*M) N in the time complexity 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 members in the sets associated with all keys in the parameter. Therefore, if any key in the parameter is associated with a set that is empty, or if a key does not exist, the result of the command is the empty. The collection of result members for the intersection.
sinterstoredestination key [key ...] O (N*M) This command is functionally identical to the sinter command, and the only difference is that sinter returns the result member of the intersection, which stores the Intersect member in the destination associated set. If the destination key already exists, the operation overwrites its members. Returns the number of members of the intersection.
sunion key [key ...] O (N) N in the time complexity represents the total number of members in all sets. This command returns the set of members in the sets associated with all keys in the parameter. The collection of result members of the set.
sunionstoredestination key [key ...] O (N) This command is functionally identical to the sunion command, and the only difference is that the sunion returns the result member of the set, which stores the integrator in the destination associated set. If the destination key already exists, the operation overwrites its members. Returns and integrates the number of members.

Third, the command example:

1. Sadd/smembers/scard/sismember:

Copy Code code as follows:

#在Shell命令行下启动Redis的客户端程序.
/> REDIS-CLI
#插入测试数据, because the key myset does not exist before, three members of the parameter are inserted normally.
Redis 127.0.0.1:6379> Sadd MySet a b C
(integer) 3
#由于参数中的a在myset中已经存在, so this operation only inserts D and e two new members.
Redis 127.0.0.1:6379> Sadd MySet a D E
(integer) 2
#判断a是否已经存在, the return value of 1 indicates existence.
Redis 127.0.0.1:6379> Sismember MySet A
(integer) 1
#判断f是否已经存在, the return value of 0 indicates that it does not exist.
Redis 127.0.0.1:6379> Sismember MySet F
(integer) 0
#通过smembers命令查看插入的结果, from the results can be, the order of the output and the insertion order is irrelevant.
Redis 127.0.0.1:6379> smembers MySet
1) "C"
2) "D"
3) "A"
4) "B"
5) "E"
#获取Set集合中元素的数量.
Redis 127.0.0.1:6379> SCard MySet
(integer) 5

2. Spop/srem/srandmember/smove:
Copy Code code as follows:

#删除该键, easy to test later.
Redis 127.0.0.1:6379> del MySet
(integer) 1
#为后面的示例准备测试数据.
Redis 127.0.0.1:6379> Sadd MySet a b c D
(integer) 4
#查看Set中成员的位置.
Redis 127.0.0.1:6379> smembers MySet
1) "C"
2) "D"
3) "A"
4) "B"
#从结果可以看出, the command does return a member randomly.
Redis 127.0.0.1:6379> Srandmember MySet
C
#Set中尾部的成员b被移出并返回, in fact B is not the first or last member inserted before.
Redis 127.0.0.1:6379> Spop MySet
"B"
#查看移出后Set的成员信息.
Redis 127.0.0.1:6379> smembers MySet
1) "C"
2) "D"
3) "A"
#从Set中移出a, D and F three members, where F does not exist, so only A and D two members are removed and returned to 2.
Redis 127.0.0.1:6379> Srem MySet a D f
(integer) 2
#查看移出后的输出结果.
Redis 127.0.0.1:6379> smembers MySet
1) "C"
#为后面的smove命令准备数据.
Redis 127.0.0.1:6379> Sadd MySet a B
(integer) 2
Redis 127.0.0.1:6379> Sadd Myset2 c D
(integer) 2
#将a从myset移到myset2, you can see from the results that the move was successful.
Redis 127.0.0.1:6379> smove MySet Myset2 A
(integer) 1
#再次将a从myset移到myset2, because a is no longer a member of the MySet, the move fails and returns 0.
Redis 127.0.0.1:6379> smove MySet Myset2 A
(integer) 0
#分别查看myset和myset2的成员 to confirm that the move is really 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:
Copy Code code as follows:

#为后面的命令准备测试数据.
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
#myset和myset2相比, A, B, and D three members are the difference members between the two. This result continues to be compared with Myset3, B and D are members of the MYSET3 that do not exist.
Redis 127.0.0.1:6379> Sdiff myset Myset2 myset3
1) "D"
2) "B"
#将3个集合的差异成员存在在diffkey关联的Set中 and returns the number of members inserted.
Redis 127.0.0.1:6379> sdiffstore Diffkey myset Myset2
(integer) 2
#查看一下sdiffstore的操作结果.
Redis 127.0.0.1:6379> smembers Diffkey
1) "D"
2) "B"
#从之前准备的数据就可以看出, these three set members intersect only C.
Redis 127.0.0.1:6379> sinter myset Myset2 myset3
1) "C"
#将3个集合中的交集成员存储到与interkey关联的Set中, and returns the number of Intersect members.
Redis 127.0.0.1:6379> sinterstore Interkey myset Myset2
(integer) 1
#查看一下sinterstore的操作结果.
Redis 127.0.0.1:6379> smembers Interkey
1) "C"
#获取3个集合中的成员的并集.
Redis 127.0.0.1:6379> sunion myset Myset2 myset3
1) "B"
2) "C"
3) "D"
4) "E"
5) "A"
#将3个集合中成员的并集存储到unionkey关联的set中, and returns and integrates the number of members.
Redis 127.0.0.1:6379> sunionstore Unionkey myset Myset2
(integer) 5
#查看一下suiionstore的操作结果.
Redis 127.0.0.1:6379> smembers Unionkey
1) "B"
2) "C"
3) "D"
4) "E"
5) "A"

Iv. Scope of application:

      1). You can use the Redis set data type to track unique data, such as accessing unique IP address information for a particular blog. For this scenario, the set data type automatically guarantees the uniqueness of the IP address only if the IP of the visitor is stored in Redis each time the blog is accessed.
      2). It can be used to maintain the association relationship between data objects by taking full advantage of the convenient and efficient operation of the set type of service-side aggregation. For example, all customer IDs purchased for an electronic device are stored in a specified set, and the customer ID that buys another electronic product is stored in another set, and if we want to acquire which customers have purchased both of these items at this time, Set's 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.