In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how to use SortedSet in Redis to achieve multi-dimensional sorting, the article is very detailed, has a certain reference value, interested friends must read it!
About SortedSet
First of all, we all know that the SortedSet of Redis can be sorted by score. Take the most popular list of mobile app stores as an example, in reverse order according to the number of downloads, its simple usage is as follows:
127.0.0.1 zadd TopApp 6379 > zadd TopApp 12000000 wechat (integer) 1127.0.0.1 taobao 10000000 alipay (integer) 2127.0.1 integer 6379 > ZREVRANGE TopApp 0-11) "wechat" 2) "alipay" 3) "taobao"
Make a brief summary of SortedSet:
Default ascending order, that is, through the command ZRANGE; if you want to sort in descending order, you need to do so by the command ZREVRANGE
When the score has the same score, the member is sorted in lexicographical order, which uses binary, which compares the byte array of strings, so it actually compares ASCII codes.
After the brief usage introduction, several schemes are given, and how to use Redis to achieve multi-dimensional sorting is introduced. Similarly, take the ranking of the most popular list of mobile app stores as an example: first, it is sorted in reverse order by the number of APP downloads, and if the number of downloads is the same, it is sorted in reverse order according to the last update time.
Option 1
The first solution introduced does not need to rely on SortedSet, its implementation is very simple, but requires a simple product compromise, that is, the list can not be updated in real time. The solution is to calculate the hot list every 1 minute (the time interval can be determined by the product) through SQL (select * from tb_apps order by download_count desc, updated_time desc limit 300) or other means, and then save the TOP300 to the cache with List structure.
Explanation: according to the user behavior analysis of the app store, real users rarely preview data after 10 pages, and even if there are such users, we can ignore them. So you only need to save a total of 10 pages, that is, 10x30=300 APP information, with the List structure. When fetching data in pages, it can be easily achieved through the lrange command.
This scheme is simple, but it is very useful. Even if it is not real-time, it does not affect the user experience. It is recommended to release quickly at the beginning of the project.
Option 2
Scheme 2 is the use of SortedSet to achieve multi-dimensional sorting, which is mainly introduced in this paper.
Before introducing the scheme, let's take a look at the SortedSet sorting factor score, which is a double-precision 64-bit floating-point numeric string. Both + inf and-inf are valid values and can include integers ranging from-(2 ^ 53) to + (2 ^ 53), or-9007199254740992 to 9007199254740992.
So how do we achieve multi-dimensional sorting? The answer is to construct a special score. Taking this case as an example, the influence factors of sorting are downloads and update time, so we can construct a special floating-point type of score: the integer part is the download quantity, and the decimal part is the last update timestamp.
Talk is cheap,show me the code . Suppose there are 5 app downloads and the last update time are as follows (note: the update time is only accurate to seconds):
The number of wechat- downloads: 12000000, the last update time: 1564022201, its score: 12000000.1564022201
The number of qq- downloads: 12000000, the last update time: 15640222, and its score: 12000000.1564022222
Tiktok- downloads: 9808900, last update time: 1563552267, score: 9808900.1563552267
The number of taobao- downloads: 11006600, the last update time: 1564345601, its score: 11006600.1564345601
The number of alipay- downloads: 11006600, the last update time: 1564345600, its score: 11006600.1564345600
Next, we save the five APP to Redis with the SortedSet data type with the following command:
Zadd TopApp 12000000.1564022201 wechat 12000000.1564022222 qq 9808900.1563552267 tiktok 11006600.1564345601 taobao 11006600.1564345600 alipay
After saving, let's see if the sorting results meet our expectations:
127.0.1 zrevrange TopApp 6379 > alipay 0-11) "qq" 2) "wechat" 3) "taobao" 4) "alipay" 5) "tiktok"
Write at the end
Isn't it perfect?
It is not perfect, this clever way can only achieve two-dimensional sorting. What if there is a three-dimensional sort, a four-dimensional sort? Here the author provides an implementation reference, that is, a custom score weight calculation formula, which contains all the factors that affect the ranking, such as downloadCount*1000+updatedTime. This kind of implementation can be done regardless of the number of sorting dimensions, but it should be noted that you must be careful not to let score overflow when you implement it.
The above is all the content of the article "how to use SortedSet to achieve multi-dimensional sorting in Redis". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.