In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
What the editor wants to share with you this time is how Redis achieves the function of Wechat step list. The article is rich in content. Interested friends can learn about it. I hope you can get something after reading this article.
1. Preface
When I wrote a blog about Redis's five data structures and their common commands, readers commented that they wanted to know the usage scenarios of these five data structures, but they never had time to write about them.
Coincidentally, when I was looking for a job interview in March, an interviewer first asked me about the data structures of Redis. After I finished, the interviewer asked me the following questions:
How to use Redis to achieve Wechat step ranking?
I believe many friends know that you can use the orderly collection of Redis ZSET to achieve, this blog is based on this question, to explain the use of ZSET, as well as the general implementation of Wechat step ranking ideas.
2. Usage scenarios of ZSET
The classic usage scenario of ZSET is used to achieve the ranking, to cite a few common examples, such as Baidu Hot list:
For example, the most searched list of Weibo:
For example, Wechat step list:
The implementation ideas of the three scenarios are basically the same. Next, we take the Wechat step list as an example to learn how to use Redis's ZSET to achieve Wechat step list.
3. The general realization idea of Wechat step ranking
Note: the focus of this article is the use of Redis's ZSET, so it only analyzes the general implementation ideas of the Wechat step ranking, the actual implementation must be much more complex than the analysis in the article.
First of all, let's analyze the needs of the Wechat step ranking:
The ranking list is based on the date, and the ranking list of historical dates can be viewed. The ranking table may not show the steps of all friends. For example, my Wechat has 349 friends, but the ranking list has never shown so many. Suppose that the number of friends who only show the first 200 steps is updated asynchronously, so after the steps are synchronized every once in a while, the ranking will change in the ranking. Friends' avatars and Wechat nicknames can be understood as unchanged (the probability of change is small, like the title and Url on the hot search list), but the number of steps and likes are variable.
Based on the requirements of the above analysis, the general implementation ideas are as follows:
When setting key using Redis's ZSET data structure, based on WeChat account and date, for example, my Wechat is zwwhnly, and today's date is 2020-06-01, then key can be designed as follows: when StepNumberRanking:zwwhnly:20200601 sets value, it takes the friend's nickname as the member member and the number of steps of the friend as the score score, as shown below:
Use the HASH data structure of Redis, where member,value, a member of step 3 of key+ whose key is step 2, stores friend profile photos, nicknames, steps and likes respectively, as shown below:
When getting the ranking of Wechat steps, it is divided into the following 2 steps:
1) first query the friend nicknames in the Wechat step list, that is, query the value of StepNumberRanking:zwwhnly:20200601
2) query the information on the number of friends' steps according to the obtained friend nicknames, that is, query the value of StepNumberRanking:zwwhnly:20200601:yst.
4. The Redis command used
The above analysis gives a general idea of the implementation, and then we will explain the Redis commands used.
4.1 ZADD
Execute the following command to initialize the Wechat ranking. Take the nine friends in the image above as an example, initialize them twice:
ZADD StepNumberRanking:zwwhnly:20200602 25452 yst 23683 zq 23599 ljx 20391 yyq 19628 XxZz
ZADD StepNumberRanking:zwwhnly:20200602 18261 lxx 16636 zcc 16555 clc 16098 fl
The effect of execution is shown in the following figure:
As you can see, the default is arranged in score positive order, that is, the number of steps is arranged from fewer to more.
4.2 HMSET
Because nicknames, avatars, steps and likes need to be displayed when displaying the ranking of steps, you can store them with the help of Redis's HASH data structure. In this case, you need to use the HMSET command:
The effect of execution is shown in the following figure:
4.3 ZINCRBY
Every once in a while, the number of steps of a friend will be updated. At this time, you can use the ZINCRBY command to update the number of steps of a friend. Suppose we only update the number of steps of our top two friends, and increase their steps by 10, then we can execute the following command:
ZINCRBY StepNumberRanking:zwwhnly:20200602 10 yst
ZINCRBY StepNumberRanking:zwwhnly:20200602 10 zq
The effect of execution is shown in the following figure:
After updating the steps in the rankings, don't forget to execute the HMSET command to update your friends' steps:
4.4 HINCRBY
When we like our friends in the step list, we can use the HINCRBY command to add 1 to the likeNum in the figure above:
HINCRBY StepNumberRanking:zwwhnly:20200602:zq likeNum 1
4.5 ZRANGE
After all the data is ready, all that is left is the query. We can use the ZRANGE command to get the friend information in the ranking list:
ZRANGE StepNumberRanking:zwwhnly:20200602 0-1
As you can see, the friend information is sorted from the least to the most, while the ranking list should be sorted from the most to the least, which uses the following ZREVRANGE command.
4.6 ZREVRANGE
The ZREVRANGE command is similar to the ZRANGE command, but in reverse order by score, which fits the ranking scenario.
For example, execute a command:
ZREVRANGE StepNumberRanking:zwwhnly:20200602 0-1 WITHSCORES
As you can see, the query of friend information is sorted from the largest to the smallest step by step, which is exactly the order in which the ranking list is to be displayed.
However, rankings generally do not show all the data. Here, we have relatively little data. If you only get the number of top5 friends, you can execute the following command:
ZREVRANGE StepNumberRanking:zwwhnly:20200602 0 4 WITHSCORES
If you want to get top200, change the above 4 to 199.
4.7 HGETALL
After getting the friends' information in the ranking, the last step is to get the number of steps, likes, avatars and nicknames of these friends, that is, the information we stored using the HASH data structure. At this time, we can use the HGETALL command, as shown below:
HGETALL StepNumberRanking:zwwhnly:20200602:yst
If you're not familiar with these commands, take a look at a blog I published earlier: an introduction to the use of five data structures in Redis.
5. Summary
Redis's ZSET data structure is very suitable for use in the scenarios of ranking, such as Baidu Hot search, Weibo Hot search list, Game ranking, Wechat step ranking. The interviewer certainly won't ask you what commands ZSET has, the details of each command, and so on, but ask you how to use Redis to achieve Wechat step ranking, and you will know your mastery of Redis data structure.
Therefore, it is important to learn the basics of Redis's five data structures, but it is more important to know how to use these data structures and what scenarios are most appropriate for each data structure.
After reading this article on how Redis achieves Wechat step-by-step ranking, if you think the article is well written, you can share it with more people.
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.