Redis Research (vi)-collection type

Source: Internet
Author: User

First, Introduction

A collection type (set) key can store a maximum of 2^32-1 strings.

The collection type is implemented within Redis using a hash table with an empty value, so the complexity of the operation is O (1). Multiple collection type keys can also be used to perform a set, intersection, and difference operation.


Second, the order

1. Add/Remove elements

Sadd Key Membersrem Key member

Sadd is used to add one or more elements to the collection and is created automatically if the key does not exist. A collection cannot have the same element, so if the element to be added already exists in the collection, the element is ignored. The return value of this command is the number of elements that were successfully added.

The second Sadd command returns 2 because a already exists and actually adds only two elements.

Srem is used to remove one or more elements from the collection and returns the number of successful deletions.

Because D does not exist in the collection, only one element is deleted and the return value is 1.


2. Get all the elements in the collection

Smembers Key

Returns all elements of a collection


3. Determine if the element is in the collection

Sismember Key Member

Time complexity O (1), regardless of the number of elements.


4. Operations between sets

Sdiff Keysinter keysunion Key

(1) Sdiff differential set operation, a-B, belongs to A but does not belong to

Sdiff supports simultaneous ingress of multiple keys

The order calculates the SETA-SETB first, then calculates the difference between the result and the setc.

(2) Sinter intersection operation

Sinter also supports the simultaneous passing of multiple keys.

(3) Sunion and set operation.

Sunion also supports the simultaneous passing of multiple keys.


Third, command supplements

1. Get the number of elements in the collection


SCard Key

2. Perform set operations and store the results

Sdiffstore Destination Keysinterstore destination Keysunionstore destination Key

As with the Sdiff function, the only difference is that the former does not directly return the result of the operation, but instead stores the result in the destination key.


3. Randomly get the elements in the collection

Srandmember key [Count]

The count parameter randomly obtains multiple elements at a time.

(1) Count positive, get count of non-repeating elements, when larger than the number of elements in the collection, returns the entire element of the collection

(2) Count negative, absolute value elements, may be the same.


4. Pop an element from the collection

Spop Key

Randomly selects an element pop-up from the collection.


Redis Research (vi)-collection type

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.