## respected Redis ordered collection implements ranking list. I hope it will be helpful to friends in need!
Preface
As an almost indispensable element in Internet applications, rankings can arouse human beings’ desire for comparison. The products in a certain treasure There are many ways to implement rankings, such as sales rankings and store reputation rankings. You can use the quick sort algorithm to implement the Comparator interface to sort by a certain weight. Now many companies are using redis, a NoSQL database, to implement the ranking function
Based on redis to implement the ranking list
What we need to do now is to rank the companies. The ranking standard is the number of searches for the company by users, and make a ranking of the top ten companies
1 .Related redis knowledge
The redis data structure related to the implementation of the ranking function is sort set (ordered set)
About sort set
We know that set is a Sets, one of the characteristics of sets is that they have no duplicate elements. In addition to having no duplicate elements, sort sets also have the characteristic of orderliness.
Data structure composition:
Weight: also called score (score) redis sorts the elements in the set in ascending order by weight Sorting (default), weight can be repeated ######value: set elements, elements cannot be repeated ######
String(set key),double(权重),String(value)
Copy after login
###sort set is implemented through a hash table, so adding, function, and search time The complexity is O(1), and each collection can store more than 4 billion elements######Basic commands#########Add one or more elements to the collection##### #
ZADD "KEY" SCORE "VALUE" [ SCORE "VALUE"]
Copy after login
###Effect:###
MyRedis:0>ZADD test 1 "one""1"MyRedis:0>zadd test 4 "four" 5 "five""2"
Copy after login
######Get the number of elements in the collection######
ZCARD "key"
Copy after login
###Effect###
MyRedis:0>ZCARD test"5"
Copy after login
######Get the specified element score (weight) ######
ZSCORE "KEY" "VALUE"
Copy after login
###Effect###
MyRedis:0>ZSCORE "test" "one""2"
Copy after login
######The specified element of the specified collection increases the specified score######
##### #Get the elements in the specified range (the default is in ascending order of score|weight)######
ZRANGE "key" 开始下标 结束下标
Copy after login
###Effect###
MyRedis:0>ZRANGE "test" 0 1 1) "two" 2) "one"
Copy after login
###It takes about so many commands to complete this requirement, let’s start implementing ours Requirements######2.springboot redis implementation######Import redis dependency###
###Business implementation:######### ######Because the ranking has high real-time requirements, I personally think it is not necessary to persist it to the database###
/** * 根据公司名找到指定公司 * @param companyName * @return */ @Override public AjaxResult selectCompanyName(String companyName) { Set
Copy after login
###Get the ranking###
/** * 获得公司排行榜(前十) * @return */ @Override public AjaxResult getCompanyRank() { Set set = redisUtils.sortSetRange("companyRank",0,9); if(set.size() == 0){ return new AjaxResult().error("公司排行榜为空"); } return new AjaxResult().ok(set); }
Copy after login
###3. Testing and summary#### ###########postman test: ###############Another question is the ranking of the same scores######If I want A to be The one who arrived first is ranked in front of B who has the same score but arrived later. How to solve this problem? ######To solve this problem, we can consider adding a timestamp to the score. The calculation formula is: ###
###This company with time can write it by itself to reduce the error as much as possible###### ###############
The above is the detailed content of About java implementing ranking list based on redis ordered collection. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn