Xinxing analyzes the ordered collection in Redis, and Xinxing analyzes redis

Source: Internet
Author: User

Xinxing analyzes the ordered collection in Redis, and Xinxing analyzes redis

The sorted-set in Redis is similar to the set. It is a string set and duplicate members are not allowed to appear in a set. The main difference between an ordered set and a set is that each element in an ordered set has a sequence number connected to it. This sequence number is score, redis uses this serial number to sort members in the set from small to large. It is worth noting that although the element values of an ordered set are unique, the score corresponding to this value can be multiple. Adding, deleting, and updating members in an ordered set are fast. The time complexity is the logarithm of the members in the set. Because the positions of members in an ordered set are ordered in the set, it is highly efficient to access members in the middle of the set.

We can use an ordered set to design something similar to the point ranking, so whenever the user's points change, you can execute the zadd command to update the user's points, then, zrange is used to obtain the information of several users before points. Of course, you can also use the zrank command to obtain user ranking information through username.

The following are common commands for ordered sets:

(1) zadd Set Name subscript 1 element 1 [subscript 2 element 2 ......] it adds elements to an ordered set. In this command, we can specify multiple sets of subscript element pairs. If a member in the parameter already exists at the time of addition, the command updates the score of the member to a new value, and sorts the member based on the new value. If the set name does not exist, this command creates a new sorted set and inserts the subscript element pair into it. If the set name already exists, but the value associated with the key is not an ordered set, an error message is returned. The return value of this operation is the number of actually inserted members.

(2) zcard set name this command gets the number of members in the sorted set. The returned value is the number of members in an ordered set. If the ordered set does not exist, 0 is returned.

(3) zcount Set Name minimum subscript maximum subscript where we can use + inf or-inf, where + inf indicates the maximum value of the lower mark in the ordered set, while-inf indicates the minimum value in the ordered set. By default, the minimum subscript and the maximum subscript are closed intervals, but we can add a "(" to the minimum subscript or the maximum subscript to indicate the open interval. It returns the number of elements in the specified subscript range.

(4) The zincrby Set Name increment element name can add an increment for the subscript of the specified Member of the specified ordered set. If the member does not exist, this command adds the Member and assumes that its initial subscript is 0. It is expected to add an increment to its subscript. If the set name does not exist, the command automatically creates the set and adds the corresponding key-value pair. It returns a new subscript in the form of a string.

(5) The starting subscript of the zrange Set Name ends the subscript [withscores]. It returns the member of the subscript between the starting subscript and the ending subscript. Note that the subscript starts from 0, -1 indicates the last member. Our optional parameter withscores indicates that the subscript of each member is returned at the same time. It indicates that the list of members of the index between the start subscript and the end subscript is returned.

(6) zrangebyscore Set Name minimum subscript maximum subscript [withscores] [limit offset count] This command returns all the members of the subscript from the minimum subscript to the maximum following table, the returned Members are returned in ascending order of scores. If the members have the same scores, the returned results are returned alphabetically by the members. Optional limit is used to control the number range of returned Members. The optional parameter offset indicates that the return starts from the offset member that meets the condition and returns the count member.

(7) The element name of the zrank set name is stored by the members in the set in the ascending order of subscript. This command returns the Location Value of the specified member in the parameter, 0 indicates the position value of the specified member in the parameter.

(8) The zrem Set Name element 1 element 2... this command removes the specified member from the parameter. If it does not exist, it will be ignored. It returns the number of actually deleted members.

(9) The starting subscript of the zrevrange Set Name ends the subscript [withscores], which is basically the same as that of zrange. The only difference is that the command obtains the members at the specified position through reverse sorting, that is, the order from high to low.

(10) The name element of the zrevrank set numbers from high to low. Note that the maximum order of subscripts is 0.

(11) zscore Set Name element: it obtains the subscript of the specified member in the specified set name.

(12) zrevrangebyscore Set Name maximum subscript minimum subscript [withscores] [limit offset count] in addition to sorting by subscript from high to low, other commands are the same as zrangebyscore.

(13) The start subscript of the zremrangebyrank Set Name ends the subscript. It is the Member that deletes the index position between the start subscript and the end subscript. Note that both the start subscript and the end subscript are 0-based, that is, 0 indicates the member with the lowest score, and-1 indicates the last Member, that is, the member with the highest score.

(14) zremrangebyscore Set Name minimum subscript maximum subscript it is to delete all the members of the score between the minimum subscript and the maximum subscript, for the minimum subscript and the maximum subscript, you can also use the open range form.


We will introduce it here, and hope it will be useful.

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.