C#redis Set Set

Source: Internet
Author: User
Tags new set set set

Fast New Year, the task is not so much, you can have time to understand the other content, see a blog today about Weex, feel quite practical, and so on can understand. But the goal of this year is to be completed. Continue with Redis today.

First, foreplay

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.

Second, the theory

command prototype Time complexity command description return value
Sadd Key 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.
SCard Key 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.

SPOP Key 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.
Srem Key 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) Like Spop, a member of a set is returned randomly, but the command does not delete the returned member. Returns a member of a random position, or nil if key does not exist.
smove Source Destination Member O (1) Atomically moves the member in the argument 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 be moved 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 indicates that the source does not contain parameter members.
Sdiff key [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. The collection of variance result members.
sdiffstore destination 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.
SINTER key [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.
sinterstore destination 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.
sunionstore destination 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.

Third, the actual exercise

127.0.0.1:6379>del mykey (integer)1127.0.0.1:6379>Sadd MyKey a b C d (integer)4127.0.0.1:6379>scard mykey (integer)4127.0.0.1:6379>sismember MyKey A (integer)1127.0.0.1:6379>smembers MyKey1)"D"2)"C"3)"a"4)"b"127.0.0.1:6379>Spop MyKey"a"127.0.0.1:6379>Srem mykey C (integer)1127.0.0.1:6379>Srandmember MyKey"b"127.0.0.1:6379>Srandmember MyKey"D"127.0.0.1:6379>Sadd Newkey a D e (integer)3127.0.0.1:6379>smembers MyKey1)"D"2)"b"127.0.0.1:6379>Smove mykey newkey B (integer)1127.0.0.1:6379>smembers MyKey1)"D"127.0.0.1:6379>sdiff mykey newkey (empty list orSet)127.0.0.1:6379>Sdiff Newkey MyKey1)"e"2)"a"3)"b"127.0.0.1:6379>sdiffstore newkey mykey (integer)1127.0.0.1:6379>smembers Newkey1)"D"127.0.0.1:6379>smembers MyKey1)"D"127.0.0.1:6379>sinterstore mykey newkey (integer)1127.0.0.1:6379>smembers MyKey1)"D"127.0.0.1:6379>smenmbers Newkey (Error) ERR unknown command'smenmbers'127.0.0.1:6379>smembers Newkey1)"D"127.0.0.1:6379>sadd MyKey A (integer)1127.0.0.1:6379>smembers MyKey1)"a"2)"D"127.0.0.1:6379>sunion MyKey Newkey1)"D"2)"a"127.0.0.1:6379>sunionstore mykey newkey (integer)1127.0.0.1:6379>smembers MyKey1)"D"127.0.0.1:6379>

C#redis Set Set

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.