Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the redis ordered collection type Zset

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use redis ordered collection type Zset". In daily operation, I believe that many people have doubts about how to use redis ordered collection type Zset. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use redis ordered collection type Zset". Next, please follow the editor to study!

The Redis ordered collection zset, like the collection set, is a collection of string type elements, and duplicate members are not allowed. The difference is that each element of zset is associated with a score (the score can be repeated), and redis uses scores to sort the members of the collection from small to large zadd

Syntax: zadd key score member [score member...]

Function: add one or more member elements and their score values to the ordered set key, if member exists in the set, then overwrite the original value; score can be an integer or floating point number.

Return value: number, number of newly added elements.

Zrange

Syntax: zrange key startIndex endIndex [WITHSCORES]

Function: query the ordered collection and specify the elements within the interval. Collection members are sorted by score value from smallest to largest; startIndex and endIndex both start at 0 for the first element, 1 for the second element, and so on; both startIndex and endIndex can take negative numbers, indicating back-to-forward, and-1 for the first-to-last element; the WITHSCORES option returns score and value together.

Return value: a collection of members of the specified interval.

Zrangebyscore

Syntax: zrangebyscore key min max [WITHSCORES] [LIMIT offset count]

Function: get all the members in the ordered set key whose score values are between min and max (including min and max). The ordered members are sorted incrementally (from small to large).

Use the symbol "(" to indicate including min but not max

Withscores displays score and value

Limit is used to limit the number and interval of returned results, starting with the offset and taking count in the result set.

Return value: the collection data of the specified interval

Zrem

Syntax: zrem key member [member...]

Function: delete one or more members of the ordered collection key, and members that do not exist are ignored.

Return value: the number of members successfully deleted, excluding ignored members.

Zcard

Syntax: zcard key

Purpose: to get the number of element members of the ordered set key.

Return value: key exists, which returns the number of collection elements; key does not exist, 0 is returned.

Zcount

Syntax: zcount key min max

Function: returns the number of members in the ordered set key whose score value is between min and max (including the score value is equal to min or max).

Return value: specifies the number of elements in the ordered set whose scores are within the specified interval.

Zrank

Syntax: zrank key member

Function: get the ranking of the member member in the ordered set key. The ordered set members are arranged according to the score value from small to large, ranking from 0 to 0, and the smallest score is 0.

Return value: specifies the ranking of the element in the ordered collection; returns nil if the specified element does not exist.

Zscore

Syntax: zscore key member

Function: get the score of the element member in the ordered collection key.

Return value: returns the score of the specified ordered collection element.

Zrevrank

Syntax: zrevrank key member

Function: get the ranking of the member member in the ordered set key. The ordered set members are arranged according to the score value from the largest to the smallest. Starting from 0, the largest score is 0.

Return value: specifies the ranking of the element in the ordered collection; returns nil if the specified element does not exist.

Zrevrange

Syntax: zrevrange key startIndex endIndex [WITHSCORES]

Function: query the ordered collection and specify the elements within the interval. Collection members are sorted by score value from highest to lowest; startIndex and endIndex both start at 0 for the first element, 1 for the second element, and so on; both startIndex and endIndex can take negative numbers for back-to-forward, and-1 for the first-to-last element; the WITHSCORES option returns score and value together.

Return value: a collection of members of the specified interval.

Zrevrangebyscore

Syntax: zrevrangebyscore key max min [WITHSCORES] [LIMIT offset count]

Function: get all the members in the ordered set key whose score values are between max and min (including max and min). The ordered members are sorted in descending order (from largest to smallest).

Use the symbol "(" to indicate excluding min and max

Withscores displays score and value

Limit is used to limit the number and interval of returned results, starting with the offset and taking count in the result set.

Return value: the collection data of the specified interval

At this point, the study on "how to use the redis ordered collection type Zset" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report