5. Basic Redis commands --- unordered set, redis command --- set

Source: Internet
Author: User

5. Basic Redis commands --- unordered set, redis command --- set
1. Each element in the set is unique and has no order.
2. Comparison between set and list


Set List
Storage content Up to 2 ^ 32-1 strings Up to 2 ^ 32-1 strings
Orderliness Unordered Ordered
Uniqueness Unique Not unique

3. The set uses an empty hash implementation. Therefore, the time complexity of inserting, deleting, and determining whether an element exists in the set is O (1)
4. SADD key member1 member2. .. add multiple elements at the same time.
127.0.0.1: 6379> SADD setA 1 2 3 4 (integer) 4

5. Obtain all data from SMEMBERS key 127.0.0.1: 6379> SMEMBERS setA
1) "1"
2) "2"
3) "3"
4) "4"
6. SREM key value1 value2: delete data, and return the number of deleted 127.0.0.1: 6379> SMEMBERS setA
1) "1"
2) "2"
3) "3"
4) "4"
127.0.0.1: 6379> SREM setA 1 2
(Integer) 2
7. SISMEMBER key member determines whether the member exists. If 1 is returned, 0 is returned, and 127.0.0.1: 6379> SMEMBERS setA does not exist.
1) "3"
2) "4"
127.0.0.1: 6379> SISMEMBER setA 3
(Integer) 1
127.0.0.1: 6379> SISMEMBER setA 4
(Integer) 1 127.0.0.1: 6379> SISMEMBER setA 5
(Integer) 0
8. Inter-set operation SDIFF key1 key2 key3. difference set SINTER key1 key2 key3 ...... intersection SUNION key1 key2 key3. ...... Union set
9. SRANDMEMBER randomly obtains an element SRANDMEMBER set count. If the count value is a positive number, count distinct elements are randomly obtained from the set. If the count value is greater than the set size, all elements are returned. If the count value is negative, | count | elements are randomly obtained from the set. These elements may share the same principle: the elements returned by SRANDMEMBER are not very random, this situation is caused by the storage structure (hash) used by the collection type. The hash function maps elements to different buckets to find the time-complexity elements of O (1. For example, if the hash value of B is 0 when Element B is stored in the hash list, Element B is stored in bucket 0. The next time you get an element, use the same algorithm to calculate that the hash value of B is 0 and read the element directly from bucket 0. If an element conflict occurs, that is, the hash values of multiple elements are the same, the set uses the zipper method to resolve the conflict, and the elements in the hash value conflict are stored in the same bucket as a linked list, find the bucket of the corresponding element before finding the corresponding element in the linked list. The SANDMEMBER command randomly retrieves a bucket and then randomly retrieves an element in the corresponding linked list. Therefore, the fewer elements in the bucket, the higher the probability of being randomly selected. In fact, HashMap works in the same way.
10. A Random SPOP element pops up from the collection.










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.