Redis Research (8)-Ordered Set Type 1, redis research set type

Source: Internet
Author: User

Redis Research (8)-Ordered Set Type 1, redis research set type

I. Introduction

Sorted set Associates each element in the set with a score. Therefore, you can obtain the first N elements with the highest or lowest scores, and obtain the elements in the specified score range and other score-related operations. Although each element in the set is different, their scores can be the same.


The ordered set type is similar to the list type.

(1) Both are ordered.

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

But there is a big difference between the two.

(1) The list is implemented through a linked list, so that the data at both ends of the list can be obtained quickly and the access speed to intermediate data is slow.

(2) The ordered set type is implemented using a hash table and a skip table, and intermediate data is obtained quickly (time complexity O (log (N ))).

(3) You cannot adjust the element position in the list. You can adjust the element position in an ordered set by changing the score.
(4) memory consumption for the sorted set ratio list.


Ii. Commands

1. Add Elements

zadd key score member

Zadd is used to insert an element and its score. If the element exists, the new value replaces the old value. Returns the number of added elements.

Record scores of three celebrities

You can modify the Peter score.

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

+ Inf and-inf indicate positive infinity and negative infinity respectively.

2. Obtain the score of an element.

zscore key member

3. Obtain the list of elements ranked in a certain range.

zrange key start stopzrevrange key start stop

Returns the value from small to large. A negative number indicates searching from the back to the front, and a-1 indicates the last element.

If you want to return scores together

Zrange time complexity O (logn + m), n is the base of the ordered set, and m is the number of returned elements.


If scores are the same, in alphabetical order (0-9 <A-Z <a-z), Chinese are encoded according to the encoding method, the UTF-8 is encoded as follows,

The only difference between zrevrange and zrange is that the former returns results from the largest to the smallest score.


4. Obtain the element of the specified score range

zrangebyscore key min max
Returns the elements whose scores are min and max (included) based on the element scores from small to large:


If it does not contain the endpoint value, the "(" symbol, such as 80, but not 100

Get higher than 80 (excluding 80), but do not know the high score.


Add some values for testing


Now you want to get 3 people whose scores are higher than 60 from the second person

To obtain the first three persons whose scores are less than or equal to 100,

5. Increase the score of an element.


zincrby key increment member

If the specified quota element does not exist, Redis will first establish it and assign its score to 0 before executing the 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.