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 achieve ranking by redis

2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to achieve the ranking of redis. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1 preface

To achieve a ranking, what we usually think of is that mysql's order by is simply rudely pulled out. But is this really elegant?

It is well known that the database is the bottleneck of the system. If you were given a multimillion-dollar table and asked to sort the rankings, it would take a terrible time.

How about caching? indexes are forced to be used in order by. But is this really elegant?

2 Redis ranking

We analyze the ranking, one ranking for each user, which means to repeat, then we will think of Set, a data structure of Java. But Set is disordered. Is there a structure that can keep the elements unique and orderly?

Fortunately, there is. Redis's ZSet is such a data structure. The elements in Zset are unique and ordered, sorted by score from smallest to largest. As a good crud programmer, we understand the structure of zset from these aspects.

2.1 ZADD additions and modifications

Its time complexity is O (M*log (N)), where N is the cardinality of the ordered set and M is the number of new members successfully added. Insert if key does not exist and update if it does.

The use is as follows:

Redis > ZADD page_rank 10 google.com (integer) 1

Description:

Page_rankde is key,10 is a score, google.com is value

2.2 ZRANK query

Time complexity: O (log (N))

The use is as follows:

Redis > ZRANGE salary 0-1 # shows all members 1) "peter" 2) "tom" 3) "jack" redis > ZRANK salary tom # shows tom's salary ranking, second (integer) 1

Description:

The key,tom of salary is value, and you can query the corresponding ranking as long as you enter a specific key and value.

2. Delete del

Use redis's del command directly

3-score design

Going back to the implementation of the ranking, if you want to use the zset structure to achieve it, the important thing is how to design the score. Analyze the design of the ranking list. If the ranking is designed according to a dimension such as the number of gold coins, it is only necessary to reverse the number as a score score. The reverse is because zset is sorted from smallest to largest by default.

The implementation is as follows:

Public Double getScore (Long oneDayGoldBean) {String score = String.valueOf (oneDayGoldBean); return-Double.valueOf (score);}

If the ranking is designed in two dimensions, such as the number of gold coins and time. Because score is a parameter that can be of double type, we can use the total number of milliseconds of a day minus the number of milliseconds spent as a decimal part, then concatenate it as a string, and then take it as a score.

The implementation is as follows:

Public Double getScore (Long oneDayGoldBean, Long useTime) {String value1 = String.valueOf (oneDayGoldBean/1.0); long todayEndSS = getTodayEndSS (useTime); String value2 = String.valueOf (todayEndSS); String score = value1+value2; return-Double.valueOf (score);} private long getTodayEndSS (long current) {/ / the number of milliseconds at 00:00 today long zero = 0L; / / the number of milliseconds at 23:59:59 today long twelve = zero + 24 * 60 * 60 * 1000; return (twelve-current) / 1000 }

4 code implementation

@ Overridepublic boolean insertLeaderboard () {Double score = getScore (100l, 1000l); return redisTemplate.opsForZSet () .add ("leaderboard", "1", score);} @ Overridepublic Set checkLeaderboard () {/ / 0-1 indicates that the set value return redisTemplate.opsForZSet () .range ("leaderboard", 0,-1) of all value is returned. } this is the end of the article on "how to achieve the ranking of redis". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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: 276

*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

Wechat

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

12
Report