Home >Backend Development >C#.Net Tutorial >Redis tutorial (5): Set data type
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:
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) | 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 members | |
O(N | This 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 | |
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 | ##SINTERSTOREdestination key [key ...] |
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 | ##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 | ##SUNIONSTOREdestination key [key ...] | O(N) |
Returns the number of union members. |