In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use Redis to implement the Leaderboard feature. Xiao Bian thinks it is quite practical, so share it for everyone to make a reference. Let's follow the editor and have a look.
Redis tutorial column below to introduce the use of Redis to achieve the ranking function of the method, I hope to help friends in need!
Leaderboards are a common requirement. Use the ordered set feature of Redis to make leaderboards a good and fast choice.
General leaderboards are effective, such as "user scoreboards." If you don't have effectiveness, you'll always have a few old users at the top, which is frustrating for new users.
First of all, let's have a "today's scoreboard." The sorting rule is that today's users add more points to the list.
Then, when the user adds points, he/she records the ordered set of points added that day.
Suppose today is April 1, 2015, and the user with UID 1 has increased 5 points due to some operation.
Redis commands are as follows:
ZINCRBY rank:20150401 5 1
Suppose there are several other users who have also added points:
ZINCRBY rank:20150401 1 2ZINCRBY rank:20150401 10 3
Take a look at the data in the current ordered collection rank:20150401 (withscores parameter can be accompanied by the score of the obtained element):
ZRANGE rank:20150401 0 -1 withscores1) "2"2) "1"3) "1"4) "5"5) "3"6) "10"
From high to low, get top10:
ZREVRANGE rank:20150401 0 9 withscores1) "3"2) "10"3) "1"4) "5"5) "2"6) "1"
Since there are only three elements, the data is queried.
If you record the day's leaderboard every day, then the other colorful leaderboards are also simple.
For example,"yesterday's scoreboard":
ZREVRANGE rank:20150331 0 9 withscores
Use union to achieve the sum of points for multiple days to achieve "last week's scoreboard":
ZUNIONSTORE rank:last_week 7 rank:20150323 rank:20150324 rank:20150325 rank:20150326 rank:20150327 rank:20150328 rank:20150329 WEIGHTS 1 1 1 1 1 1 1
This merges the 7-day points records into the ordered set rank:last_week. Weight factor WEIGHTS is 1 by default if not given. In order not to hide the details, it was specially written.
Then check last week's Top 10 scoreboard information is:
ZREVRANGE rank:last_week 0 9 withscores
"Monthly list,""quarterly list,""annual list" and so on.
Thank you for reading! About the use of Redis to achieve the ranking function of the method to share here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge. If you think the article is good, you can share it so that more people can see it!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.