Using Redis (Set collection type operation)

Source: Internet
Author: User
Tags set set

Set Collection type

Interpretation:

Redis's set is an unordered collection of type string

The set element can contain a maximum of (2 of 32 square-1) elements

For the Set collection type in addition to the basic add delete operation other useful operations also contain the collection's

Union, intersection (intersection), Difference set (difference).

These actions enable the Friend referral feature in SNS

Attention:

Individual elements in each collection cannot be duplicated

Application:

QQ Friend's recommendation

Set type operation:

Sadd Key Member

A string element is added to the set set of key corresponding to the successful return 1 if the element returns 0 in the collection if the key corresponding set set does not have a return error

Srem Key Member[member]

Removes the given element from the set of key to return successfully 1

Smove P1 P2 Member

Remove the member from the P1 corresponding set and add it to the P2 corresponding set

SCard Key

Returns the number of elements in a set

Sismember Key Member

Determine if the member is in set

Sinter key1 Key2 ... KeyN

Returns the intersection of all given keys

Sunion key1 Key2 ... KeyN

Returns the set of all the given keys

Sdiff key1 Key2 ... KeyN

Returns the difference set for all given keys (only key1 without key2)

Smembers Key

Returns the key corresponding to the set of all elements the result is unordered

eg

Laowen10 Friends Circle Laowen20 Circle of Friends
Public friends
Laowen laowen21

Laowen Laowen01 Laowen22

Laowen laowen02 laowen23

Set set of the friend circle that generated the LAOWEN10

Sadd laowen10 laowen11//OK means add OK

Sadd laowen10 laowen12//OK means add OK

Sadd laowen10 laowen13//OK means add OK

Sadd laowen10 laowen01//OK means add OK

Sadd laowen10 laowen02//OK means add OK

Set set of the friend circle that generated the Laowen20

Sadd Laowen20 laowen21//OK means add OK

Sadd Laowen20 laowen22//OK means add OK

Sadd Laowen20 laowen23//OK means add OK

Sadd Laowen20 laowen01//OK means add OK

Sadd Laowen20 laowen02//OK means add OK

Intersection gets to intersection

Sinter Laowen10 Laowen20

Returns the result of the intersection

1) "Laowen01"

2) "Laowen02"

The assembly gets to the set

Sunion Laowen10 Laowen20

Returns the result of a set

1) "Laowen11"

2) "Laowen12"

3) "Laowen13"

4) "Laowen21"

5) "Laowen22"

6) "Laowen23"

7) "Laowen01"

8) "Laowen02"

Difference set acquisition to difference set

Sdiff Laowen10 Laowen20

Laowen10 and Laowen20 return collections that belong to LAOWEN10 only

1) "Laowen11"

2) "Laowen12"

3) "Laowen13"

Sdiff Laowen20 Laowen10

Laowen20 and Laowen10 return collections that belong to Laowen20 only

1) "Laowen21"

2) "Laowen22"

3) "Laowen23"

Gets the collection of the given element

Smembers Laowen10

Gets the set collection to the LAOWEN10

1) "Laowen11"

2) "Laowen12"

3) "Laowen13"

4) "Laowen01"

5) "Laowen02"

Determine if there is a corresponding collection

Sismember Laowen10 Laowen11

(integer) 1//Description laowen11 in the set set of Laowen10

Sismember Laowen10 Laowen21

(integer) 0//Description laowen21 is not in the set set of Laowen10

Gets the number of elements in the set set corresponding to the key

SCard LAOWEN10

(integer) 5//description has 5 elements

Moving elements

Smove laowen10 Laowen20 Laowen11

(integer) 1//Move the elements inside the Laowen10 collection laowen11 to the Laowen20 collection

Delete Element

Srem Laowen20 Laowen11

(integer) 1//Laowen20 The elements inside the collection laowen11 deleted

Using Redis (Set collection type operation)

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.