Home  >  Article  >  Backend Development  >  Redis tutorial (5): Set data type

Redis tutorial (5): Set data type

黄舟
黄舟Original
2016-12-28 14:41:241273browse

1. Overview:

In Redis, we can regard the Set type as an unsorted character collection. Like the List type, we can also add, delete, or delete data values ​​of this type. Determine whether an element exists and other operations. It should be noted that the time complexity of these operations is O(1), that is, the operations are completed in 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 exactly the same as the set container in the C++ standard library. In other words, if you add the same element multiple times, only one copy of the element will be kept in the Set. Compared with the List type, the Set type also has a very important functional feature, that is, the aggregation calculation operations between multiple Sets, such as unions, intersections, and differences, are completed on the server side. Since these operations are completed on the server side, they are extremely efficient and save a lot of network IO overhead.

2. Related command list:

##SDIFFkey [key ...]O(N)The N in time complexity represents the total number of members in all Sets. Returns the difference between the members in the Set associated with the first Key in the parameter and the Sets associated with all subsequent Keys. If Key does not exist, it is considered an empty Set. A collection of difference result membersSDIFFSTOREdestination key [key ...] O(NThis command is the same as SDIFF The commands are functionally identical, the only difference between the two is that SDIFF returns the result member of the difference, whereas this command stores the difference member in the Set associated with the destination. If the destination key already exists, this operation overwrites its members. Return the number of difference members ##SINTERkey [key ...] ##SINTERSTOREdestination key [key ...] O(N*M) This command is functionally identical to the SINTER command. The only difference between the two is that SINTER returns the result members of the intersection, while this command returns the intersection members. Stored in the Set associated with destination. If the destination key already exists, this operation will overwrite its members Return the number of intersection members O(N)##SUNIONSTOREdestination key [key ...] O(N)This command is functionally identical to the SUNION command. The only difference between the two is that SUNION returns the result members of the union, while this command stores the union members in the Set associated with the destination. If the destination key already exists. The operation will overwrite its members Returns the number of union members.

3. Command examples:

1. SADD/SMEMBERS/SCARD/SISMEMBER:

#在Shell命令行下启动Redis的客户端程序。
    /> redis-cli
    #插入测试数据,由于该键myset之前并不存在,因此参数中的三个成员都被正常插入。
    redis 127.0.0.1:6379> sadd myset a b c
    (integer) 3
    #由于参数中的a在myset中已经存在,因此本次操作仅仅插入了d和e两个新成员。
    redis 127.0.0.1:6379> sadd myset a d e
    (integer) 2
    #判断a是否已经存在,返回值为1表示存在。
    redis 127.0.0.1:6379> sismember myset a
    (integer) 1
    #判断f是否已经存在,返回值为0表示不存在。
    redis 127.0.0.1:6379> sismember myset f
    (integer) 0
    #通过smembers命令查看插入的结果,从结果可以,输出的顺序和插入顺序无关。
    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:

#删除该键,便于后面的测试。
    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"
    #从结果可以看出,该命令确实是随机的返回了某一成员。
    redis 127.0.0.1:6379> srandmember myset
    "c"
    #Set中尾部的成员b被移出并返回,事实上b并不是之前插入的第一个或最后一个成员。
    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和f三个成员,其中f并不存在,因此只有a和d两个成员被移出,返回为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,从结果可以看出移动成功。
    redis 127.0.0.1:6379> smove myset myset2 a
    (integer) 1
    #再次将a从myset移到myset2,由于此时a已经不是myset的成员了,因此移动失败并返回0。
    redis 127.0.0.1:6379> smove myset myset2 a
    (integer) 0
    #分别查看myset和myset2的成员,确认移动是否真的成功。
    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
    redis 127.0.0.1:6379> sadd myset3 a c e
    (integer) 3
    #myset和myset2相比,a、b和d三个成员是两者之间的差异成员。再用这个结果继续和myset3进行差异比较,b和d是myset3不存在的成员。
    redis 127.0.0.1:6379> sdiff myset myset2 myset3
    1) "d"
    2) "b"
    #将3个集合的差异成员存在在diffkey关联的Set中,并返回插入的成员数量。
    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"
    #从之前准备的数据就可以看出,这三个Set的成员交集只有c。
    redis 127.0.0.1:6379> sinter myset myset2 myset3
    1) "c"
    #将3个集合中的交集成员存储到与interkey关联的Set中,并返回交集成员的数量。
    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中,并返回并集成员的数量。
    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"

4. Application scope:


## 1). You can use the Set data type of Redis to track some Unique data, such as the unique IP address information for visiting a certain blog. For this scenario, we only need to store the visitor's IP in Redis every time we visit the blog, and the Set data type will automatically ensure the uniqueness of the IP address.

2). Make full use of the convenient and efficient features of Set type server-side aggregation operations, which can be used to maintain the association between data objects. For example, all customer IDs who purchase a certain electronic device are stored in a specified Set, and customer IDs who purchase another electronic product are stored in another Set. If at this time we want to get which customers purchased this at the same time, When there are two commodities, Set's intersections command can give full play to its advantages of convenience and efficiency.

The above is the content of Redis tutorial (5): Set data type. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Command prototype Time complexity Command description Return value
SADDkey member [member ...] O(N) The N in time complexity represents the number of members of the operation. If used during the insertion process, some members in the parameters already exist in the Set, the member will be ignored, and other members will still be inserted normally. If the Key does not exist before executing this command, this command will create a new Set, and then insert the members in the parameters one after another. If the Key's Value is not of Set type, this command will return relevant error information. The number of members actually inserted in this operation.
SCARDkey O(1) Get the number of members in the Set. Returns the number of members in the Set. If the Key does not exist, returns 0.
SISMEMBER key member O(1) Determine whether the member specified in the parameter already exists in the Set collection associated with the Key. 1 means it already exists, 0 means it does not exist, or the Key itself does not exist.
SMEMBERS key O(N) The N in time complexity represents the number of members that already exist in the Set. Get all members in the Set associated with the Key. Return all members in Set.
SPOPkey O(1) Randomly remove and return a member of the Set. Since the layout of the elements in a Set is not controlled externally, it is impossible to determine which element is at the head or tail of the Set like a List. Return the removed member. If the Key does not exist, return nil.
SREMkey member [member ...] O(N) The N in time complexity represents the number of deleted members. Delete the member specified in the parameter from the Set associated with the Key. Parameter members that do not exist will be ignored. If the Key does not exist, it will be treated as an empty Set. The number of members actually removed from the Set, if there are none, 0 is returned.
SRANDMEMBER key O(1) Same as SPOP, randomly returns a member of the Set. The difference is that this command does not Returned members will be deleted. Returns the member at a random position, or nil if the Key does not exist.
SMOVEsource destination member O(1) Atomicly move the member in the parameter from the source key to the Set associated with the destination key middle. So at a certain moment, the member either appears in the source or in the destination. If the member does not exist in the source, this command will perform no further operations and return 0. Otherwise, the member will be moved from the source to the destination. If the member already exists in the destination at this time, then this command only removes the member from the source.If the Value associated with Key is not a Set, relevant error information will be returned. 1 means normal movement, 0 means the source does not contain parameter members
O(N*M) Time N in complexity represents the number of elements in the minimum Set, and M represents the number of Sets in the parameter. This command will return the intersection of the members in the Sets associated with all Keys in the parameter. Therefore, if the Set associated with any Key in the parameter is empty. , or a key does not exist, then the result of this command will be an empty set The set of intersection result members
##SUNION key [key. ...]
N in time complexity represents the total number of members in all Sets. This command will return the union of members in Sets associated with all Keys in the parameter. Set. The set of union result members
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn