Small shellfish _redis set/zset type Learning

Source: Internet
Author: User

Redis set, Zset type

First, collection type

Second, view set, Zset collection type command

Example of Set, Zset set type

First, collection type

1, the collection type of Redis, is divided into two kinds, namely order and disorder.

2, since it is a collection type, so it is also to meet the characteristics of the collection

A, the certainty of a set: the nature of the elements that make up the set must be clear, not ambiguous, vague situations

b, the cross-anisotropy of a set: For a given set, the elements in the collection are different ", that is," for a given set, any two of its elements are distinct

C, the disorder of a set: When a collection is represented, the elements that make up the set are unordered

3. Set Classification ( Assuming that both set a and set B are non-null)

A, the assembly: the elements of the public part of collection A and set B are removed, and a new collection is formed

B, intersection: Gets the elements of the Announcements section of collection A and set B, forming a new collection

C, Empty set: The collection of NULL elements

second, view set, Zset collection type command

1. Set type command description

Command name

Format

Description

Sadd

Sadd Key Member

Add the member element to the key

Srem

Srem Key Member

Removes the element from the collection key member

Smembers

Smembers Key

Enumerate all elements of the collection key

Sismembers

Sismembers Key Member

Determines whether the member element is a collection key element

SCard

SCard Key

Gets the number of elements of the collection key

Smove

Smove Source Destination Member

Move the member element from the source collection to the destination collection

Spop

Spop Key

Removes and returns a random element in the collection

Srangmember

Srangmember Key

Returns a random element in the collection

Sinter

Sinter Key Key1 ...

Returns all the members of a collection that are the intersection of all given collections.

A nonexistent key is considered an empty set.

When there is an empty set in a given collection, the result is also an empty set (according to the Law of collection Operations).

Sinterstore

Sinterstore Destination Key Key1 ...

This command is equivalent to sinter, but it saves the result to the destination collection instead of simply returning the result set. If the destination collection already exists, overwrite it

Sunion

Sunion Key Key1

Returns all the members of a collection, which is the set of all the given collections.

Non-existent key is considered an empty set

Sunionstore

Sunionstore Destination Key Key1 ...

This command is equivalent to Sunion, but it saves the result to the destination collection instead of simply returning the result set. If the destination already exists, overwrite it

Sdiff

Sdiff Key Key1

Returns all members of a collection, which is the difference set for all given collections. Non-existent key is considered an empty set

Sdiffstore

Sdiffstore Destination Key Key1 ...

This command is equivalent to Sdiff, but it saves the result to the destination collection instead of simply returning the result set. If the destination collection already exists, overwrite it

2. Zset Type command description

Command name

Format

Description

Zadd

Zadd Key Score Member

Add the member element and its score value to the ordered set key.

The score value can be an integer value or a double-precision floating-point number.

Zrem

Zrem Key Member

Removes the member member in the ordered set key and does not perform any action if member is not a member of an ordered set. An error is returned when key exists but is not an ordered set type

Zcard

Zcard Key

Returns the cardinality of an ordered set key

Zcount

Zcount Key min Max

Returns a member of the ordered set key with the score value between Min and Max (by default including the score value equals min or max)

Zscore

Zscore Key Member

Returns the score value of the member member in the ordered set key

Zincrby

Zincrby Key Increment member

Adds an increment increment to the score value of the member member of the ordered set key

Zrange

Zrange key start stop [Withscores]

Returns the member of the ordered set key, within the specified interval. Where members are sorted by score value (small to large)

Zrevrange

Zrevrange key start stop [Withscores]

Returns the member of the ordered set key, within the specified interval. Where members are placed in descending order of score values (from large to small)

Zrangebyscore

Zrangebyscore key min Max [withscores] [LIMIT offset Count]

Returns the ordered set key, where all score values are between Min and Max (including equals min or Max) members. Ordered set members are ascending (from small to large) in order of score values

Zrevrangebyscore

Zrevrangebyscore key Max min [withscores] [LIMIT offset Count]

Returns the ordered set key, where the score value is between Max and Min (the default includes all members equal to Max or min). Ordered set members are arranged in descending order of score values (from large to small).

Zrank

Zrank Key Member

Returns the rank of the member member in the ordered set key. Where ordered set members are arranged in ascending order of score values (from small to large). The ranking is based on 0, meaning that the member with the lowest score value is ranked at 0.

Zrevrank

Zrevrank Key Member

Returns the rank of the member member in the ordered set key. Where ordered set members are sorted by score values descending (from large to small). The rankings are based on 0, which means that the members with the largest score value rank 0.

Zremrangebyrank

Zremrangebyrank Key Start stop

In addition to the ordered set key, all members within the rank (rank) range are specified.

The interval is indicated by the following standard parameter, start and stop, including start and stop.

Zremrangebyscore

Zremrangebyscore Key min Max

Remove ordered set key, all score values between Min and Max, including members equal to Min or max

Zinterstore

Zinterstore destination Numkeys key [key ...] [WEIGHTS weight [weight ...] [AGGREGATE sum| min| MAX]

Computes the intersection of one or more ordered sets given, where the number of a given key must be specified with the Numkeys parameter

Zunionstore

Zunionstore destination Numkeys key [key ...] [WEIGHTS weight [weight ...] [AGGREGATE sum| min| MAX]

Calculates the set of a given one or more ordered sets, where the number of a given key must be specified with the Numkeys parameter. By default, the score value of a member in the result set is the score value of that member under all given sets

3. Set type Command example

3.1. Add element ' Xiaobei ' for collection user

127.0.0.1:6379> Sadd User Xiaobei

(integer) 1

3 . 2. View all elements of the collection user

127.0.0.1:6379>smembers User

1) "Xiaobei"

3.3, determine whether Xiaobei is the element of the collection user

127.0.0.1:6379>sismember User Xiaobei

(integer) 1

3.4. Remove the elements from the collection user Xiaobei

127.0.0.1:6379> Sremuser Xiaobei

(integer) 1

3.5. Determine the number of set user

127.0.0.1:6379> Sadduser Xiaobei

(integer) 1

127.0.0.1:6379> Sadduser Script-boy

(integer) 1

127.0.0.1:6379>scard User

(integer) 2

3.6. Move the element script-boy of the set user to the set User1

127.0.0.1:6379>smove User User1 Script-boy

(integer) 1

127.0.0.1:6379>smembers user1

1) "Script-boy"

3.7. Set user and set User1

127.0.0.1:6379>smembers User

1) "Script-boy"

2) "Xiaobei"

127.0.0.1:6379>smembers user1

1) "Script-boy"

127.0.0.1:6379>sinter User User1

1) "Script-boy"

3.8. Save the intersection of Set user and set User1 in User2

127.0.0.1:6379>sinterstore user2 User1 user

(integer) 1

127.0.0.1:6379>smembers User2

1) "Script-boy"

3.9. Set user and set User1

127.0.0.1:6379>sunion User User1

1) "Script-boy"

2) "Xiaobei"

3.10, the collection of user and set User1 save in User2

127.0.0.1:6379>sunionstore User2 User User1

(integer) 1

127.0.0.1:6379>smembers User2

1) "Script-boy"

3.11. Finding the difference set of set User1 to set user

127.0.0.1:6379>smembers User

1) "Script-boy"

127.0.0.1:6379>smembers user1

1) "Script-boy"

2) "Xiaobei"

127.0.0.1:6379>sdiff User1 user

1) "Xiaobei"

3.12. Save the set User1 to set user's difference set in User2

127.0.0.1:6379>sdiffstore user2 User1 user

(integer) 1

127.0.0.1:6379>smembers User2

1) "Xiaobei"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Small shellfish _redis set/zset type Learning

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.