Redis Research (eight)-ordered collection type 1

Source: Internet
Author: User

First, Introduction

Sorted Set each element in the collection is associated with a fraction, so the top n elements with the highest or lowest score can be obtained, and the fractional-related operations are obtained for the elements within the specified fractional range. Although each element in the collection is different, their scores can be the same.


An ordered collection type is somewhat similar to a list type

(1) Both are orderly

(2) Both can obtain a certain range of elements.

But there's a big difference.

(1) The list is implemented by linked lists, which gets fast data at both ends and slow access to intermediate data.

(2) The ordered set type is implemented by using a hash table and a jump list, and obtaining intermediate data is also very fast (time complexity O (log (N))).

(3) List cannot adjust element position, ordered set can adjust element position by changing fractions
(4) The ordered collection consumes more memory than the list.


Second, the order

1. Adding elements

Zadd Key Score Member

Zadd is used to insert the element and the fraction of the element, and if the element exists, the new value replaces the old value. Returns the number of elements added.

Record three people's scores

You can modify the Peter score

Fractions can be not only integers, they also support double-precision floating-point numbers

+inf and-inf respectively represent positive infinity and negative infinity.

2. Get the fraction of the element

Zscore Key Member

3. Get a list of elements that are ranked within a range

Zrange key start Stopzrevrange key start stop

Returns from small to large with a negative number representing a forward lookup, 1 representing the last element

To return a score together

Zrange time Complexity O (logn+m), n is the cardinality of an ordered set, and M is the number of elements returned.


If the scores are the same, according to the dictionary order (0-9<a-z<a-z), the Chinese according to the encoding, UTF-8 encoding The situation is as follows,

The only difference between Zrevrange and Zrange is the fact that the former gives the result from large to small fractions.


4. Get the element with the specified fraction range

Zrangebyscore Key min Max
Returns the fraction at Min and Max (contained) elements according to the element fraction from small to large:


If the endpoint value is not included, the "(" symbol, such as 80, but not 100

Get above 80 (not including 80), but don't know how high the score is.


Add some value, test with


Now want to get a score above 60 for 3 people starting from the second person

To get the top 3 people below or equal to 100 points,

5. Increase the score of an element


Zincrby Key Increment member

If the quota element does not exist, Redis executes it before executing it and assigns its fractional value to 0 before performing the operation.


Redis Research (eight)-ordered collection type 1

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.