Redis Tutorial (V): Set data type

Source: Internet
Author: User
Tags new set redis tutorial
First, overview:

In Redis, we can see the set type as an unordered character set, and as with the list type, we can also perform operations such as adding, deleting, or judging whether an element exists on the data value of that type. It should be stated that the time complexity of these operations is O (1), which is the completion of the operation within a 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 collection, which is identical to the set container in the C + + standard library. In other words, if you add the same element more than once, only one copy of the element will be preserved in set. The set type has a functionally important feature in comparison to the list type, which is to perform aggregate compute 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 highly efficient and also save a lot of network IO overhead.

Ii. List of related commands:

TD valign= "Top" colspan= "1" rowspan= "1" style= "Word-break:break-all;" >o (1)
Command prototypes Complexity of Time Command description return value
Saddkey member [member ...] O (N) N In time complexity indicates the number of members of the operation. If the inserted procedure is used, the member in the parameter already exists in the set, the member will be ignored, and the other member will still be inserted normally. If the key does not exist before the command is executed, the command creates a new set and then inserts the members of the parameter in succession. If the value of the key 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, or 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 collection associated with key. 1 means that it already exists, 0 means it does not exist, or 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 the members in the set associated with the key. Returns all the members in the set.
Spopkey O (1) Randomly removes and returns a member of the set. Because the layout of elements in set is not externally controlled, it is not possible to determine which element is at the head or tail of the set as the list does. Returns the removed member, or nil if the key does not exist.
Sremkey member [member ...] O (N) N In time complexity indicates the number of members to be deleted. Removes the member specified in the parameter from the set associated with the key, the non-existent 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 set, or 0 if not.
srandmember key o (1) Returns a member of a random position, or nil if key does not exist.
smovesource Destination member 1 means normal movement, 0 means that source does not contain parameter members
Sdiffkey [Key ...] O (N) N in 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 members in the sets associated with all keys thereafter. If key does not exist, it is treated as an empty set. Collection of variance result members
Sdiffstoredestination key [Key ...] O (N The command is functionally identical to the Sdiff command, and the only difference between the two is that Sdiff returns the result member of the difference, which stores the differential member in the destination associated set. If the destination key already exists, the operation overrides its members. Returns the number of differential members.
Sinterkey [Key ...] O (N*M) N in time complexity represents the number of elements in the minimum set, and m represents the number of sets in the parameter. The command returns the intersection of the members in the sets associated with all keys in the parameter. Therefore, if either key in the parameter is associated with a set that is empty, or if a key does not exist, the result of the command will be an empty box. The collection of intersection result members.
Sinterstoredestination key [Key ...] O (N*M) The command is functionally identical to the sinter command, and the only difference between the two is that sinter returns the result member of the intersection, and the command stores the intersection member in the destination associated set. If the destination key already exists, the operation overrides its members. Returns the number of intersection members.
Sunion key [Key ...] O (N) N in time complexity represents the total number of members in all sets. The command returns the sets of the members in all keys associated with the key in the parameter. Sets the set of resultant members.
Sunionstoredestination key [Key ...] O (N) The command is functionally identical to the sunion command, and the only difference between the two is that the sunion returns the result member of the set, and the command stores the integrator in the destination associated set. If the destination key already exists, the operation overrides its members. Returns and the number of integrators.

Examples of commands:

1. Sadd/smembers/scard/sismember:

#在Shell命令行下启动Redis的客户端程序.    /> redis-cli    #插入测试数据, since the key myset does not exist before, the three members in 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 the new members of D and E two.    Redis 127.0.0.1:6379> Sadd myset a D e    (integer) 2    #判断a是否已经存在, a return value of 1 indicates existence.    Redis 127.0.0.1:6379> sismember myset a    (integer) 1    #判断f是否已经存在, a return value of 0 means that it does not exist.    Redis 127.0.0.1:6379> sismember myset F    (integer) 0    #通过smembers命令查看插入的结果, from the result, the order of the output is independent of the insertion order.    redis 127.0.0.1:6379> smembers myset    1) "C"    2) "D"    3) "a"    4) "B"    5) "E"    # Gets the number of elements in the set collection.    redis 127.0.0.1:6379> SCard myset    (integer) 5

2. Spop/srem/srandmember/smove:

#删除该键 to facilitate subsequent testing.    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 did 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 as 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从m    Yset moved to Myset2, the result shows that the move was successful.    Redis 127.0.0.1:6379> smove myset myset2 A (integer) 1 #再次将a从myset移到myset2, because a is not a member of MySet at this point, 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 was 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:

 #为后面的命令准备测试数据. 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 red is 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.    Using this result, we continue to compare the differences with Myset3, and B and D are the non-existent members of MYSET3.    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 myset3 (integer) 2 #查看一下sdiffstore的操作结果.    Redis 127.0.0.1:6379> smembers diffkey 1) "D" 2) "B" #从之前准备的数据就可以看出, these three sets have a member intersection of only C.    Redis 127.0.0.1:6379> sinter myset myset2 myset3 1) "C" #将3个集合中的交集成员存储到与interkey关联的Set中, and returns the number of intersection members.    Redis 127.0.0.1:6379> sinterstore interkey myset myset2 myset3 (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 the number of integrators.    Redis 127.0.0.1:6379> sunionstore unionkey myset myset2 myset3 (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 Redis's set data type to track some unique data, such as unique IP address information for accessing a blog. For this scenario, we only need to place the IP of the visitor in Redis each time we access the blog, and the set data type automatically guarantees the uniqueness of the IP address.
2). Make full use of the set type of service-side aggregation operation convenient and efficient features, can be used to maintain the relationship between data objects. For example, the customer ID of all purchases of an electronic device is stored in a specified set, and the customer ID of the purchase of another electronic product is stored in another set, and if we want to obtain a customer who buys both of these goods at this time, Set the intersections command can give full play to its advantages of convenience and efficiency.

The above is the Redis tutorial (v): Set data type content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

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