Sinsing an analysis of the ordered collection in Redis

Source: Internet
Author: User

the ordered collection in Redis is Sorted-set, which, like set, is a collection of strings and does not allow duplicate members to appear in a collection sheet. The main difference between an ordered set and a set is that each element in an ordered set has an ordinal associated with it, which is the sequence number that score,redis the members of the set from small to large. In particular, although the element value of an ordered collection is unique, the value corresponding to the score can be multiple. Adding, deleting, updating a member in an ordered collection is fast, and its time complexity is the logarithm of the members in the collection. because members in an ordered collection are in an orderly position in the collection, even the members that access the middle part of the collection are highly efficient.

We can use an ordered set to design something similar to the leaderboard, so that whenever the user's points change, the Zadd command can be implemented to update the user's points, and then the Zrange to obtain a number of user information before the points. Of course, you can also use the Zrank command to get the user's ranking information through username.

The following are common commands for an ordered collection:

(1) Zadd set name 1 element 1 [subscript 2 element 2 ...] It is to add elements to an ordered collection, in which we can specify multiple sets of subscript element pairs. If a member already exists in the parameter when it is added, the command updates the member's score to the new value, and then re-sorts the member based on the new value. If the collection name does not exist, the command creates a new ordered collection and inserts the subscript element pair into it. If the collection name already exists, but the value associated with it as a key is not an ordered collection, an error message is returned. The return value of the operation is the number of members actually inserted.

(2) Zcard collection name the command gets the number of members in the ordered collection. The return value is the number of members in an ordered collection, and returns 0 if the ordered collection does not exist.

(3) Zcount set name minimum subscript maximum subscript where we can use +inf or-inf, where +inf represents the maximum value of the subscript in an ordered set, and-inf represents the smallest value in an ordered set. By default, the minimum subscript and the maximum subscript are closed intervals, but we can add a "(" to indicate the opening interval in the minimum subscript or the maximum subscript. It returns the number of elements that are within the target range.

(4) Zincrby set name increment element name it can add an increment to the subscript for the specified member of the specified ordered collection sheet, and if the member does not exist, the command adds the member and assumes its initial subscript is 0, which is served by adding an increment to its subscript. If the collection name does not exist, the command automatically creates the collection and adds the corresponding key-value pairs. It returns a new subscript in the form of a string.

(5) Zrange set name start subscript end subscript [Withscores] It returns the member between the subscript and the end subscript, note that the subscript here starts at 0, and-1 indicates the last member. Our optional parameter, Withscores, indicates that each member's subscript is returned at the same time. It represents a list of members that returns the index between the starting subscript and the end subscript.

(6) Zrangebyscore set name minimum subscript maximum subscript [withscores][limit offset Count] The command returns the subscript in the smallest subscript to the maximum of all the members in the following table, and the returned members are returned in order from lowest to highest score, if Members have the same score, they are returned in the dictionary order of the members. An optional limit is used to control the number range of returned members. An optional parameter, offset, indicates that the returned member is returned from the qualifying offset member, and also returns count members.

(7) Zrank set name element name it is that the members in the collection are stored in order from low to high, and the command returns the position value of the specified member in the parameter, where 0 represents the position value of the specified member in the parameter.

(8) Zrem set name element 1 element 2 ..... The command removes the member specified in the parameter and is ignored if it does not exist. It returns the number of members that were actually deleted.

(9) Zrevrange set name start subscript end subscript [Withscores] It is basically the same as Zrange, the only difference being that the command gets the members at the specified position by reverse ordering, that is, the order from highest to lowest.

Zrevrank collection name element It gets ranked in the order from high to low, note that the maximum order of the subscript is 0 Austria

(one) Zscore the collection name element it gets the subscript of the specified member in the specified collection name

Zrevrangebyscore set name maximum subscript minimum subscript [withscores][limit offset Count] The command is the same as Zrangebyscore, except that the order is sorted from high to low subscript 。

Zremrangebyrank set name start subscript end Subscript It is the member that deletes the index position between the starting subscript and the end subscript, noting that the starting subscript and the end subscript are 0-based, that is, 0 is the member with the lowest score, and 1 represents the last member, which is the highest score Rapporteur

Zremrangebyscore set name min subscript maximum subscript It is to delete all the members between the minimum subscript and the maximum subscript, and for the minimum subscript and the maximum subscript, you can also use the form of an open interval.


Basically we introduced here, hope to be useful.

Sinsing an analysis of the ordered collection in Redis

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.