## 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 functionBased 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)###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"]###Effect:###
MyRedis:0>ZADD test 1 "one""1"MyRedis:0>zadd test 4 "four" 5 "five""2"######Get the number of elements in the collection######
ZCARD "key"###Effect###
MyRedis:0>ZCARD test"5"######Get the specified element score (weight) ######
ZSCORE "KEY" "VALUE"###Effect###
MyRedis:0>ZSCORE "test" "one""2"######The specified element of the specified collection increases the specified score######
ZINCRBY "key" score "value"###Effect: ###
MyRedis:0>ZSCORE "test" "one""2"MyRedis:0>ZINCRBY "test" 1 "one""3"MyRedis:0>ZSCORE "test" "one" "3"##### #Get the elements in the specified range (the default is in ascending order of score|weight)######
ZRANGE "key" 开始下标 结束下标###Effect###
MyRedis:0>ZRANGE "test" 0 1 1) "two" 2) "one"###It takes about so many commands to complete this requirement, let’s start implementing ours Requirements######2.springboot redis implementation######Import redis dependency###
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> </dependency>###Write tool class###
//=============================== sort set ================================= /** * 添加指定元素到有序集合中 * @param key * @param score * @param value * @return */ public boolean sortSetAdd(String key,double score,String value){ try{ return redisTemplate.opsForZSet().add(key,value,score); }catch (Exception e){ e.printStackTrace(); return false; } } /** * 有序集合中对指定成员的分数加上增量 increment * @param key * @param value * @param i * @return */ public double sortSetZincrby(String key,String value,double i){ try { //返回新增元素后的分数 return redisTemplate.opsForZSet().incrementScore(key, value, i); }catch(Exception e){ e.printStackTrace(); return -1; } } /** * 获得有序集合指定范围元素 (从大到小) * @param key * @param start * @param end * @return */ public Set sortSetRange(String key,int start,int end){ try { return redisTemplate.opsForZSet().reverseRange(key, start, end); }catch (Exception e){ e.printStackTrace(); return null; } }###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<object> set = redisUtils.sGet("company"); for(Object i : set){ String json = JSONObject.toJSONString(i); JSONObject jsonObject = JSONObject.parseObject(json); if(jsonObject.getString("companyName").equals(companyName)){ //搜索次数 + 1 redisUtils.sortSetZincrby("companyRank",companyName,1); log.info("直接缓存中返回"); return new AjaxResult().ok(jsonObject); } } log.error("缓存中没有,查数据库"); TbCommpanyExample tbCommpanyExample = new TbCommpanyExample(); tbCommpanyExample.createCriteria().andCompanyNameEqualTo(companyName); List<tbcommpany> list = tbCommpanyMapper.selectByExample(tbCommpanyExample); if(list.size() != 0){ //放入缓存中 redisUtils.sSet("company",list.get(0)); //数据库中存在 //搜索次数 + 1 redisUtils.sortSetZincrby("companyRank",companyName,1); log.info("sql"); return new AjaxResult().ok(list.get(0)); }else{ return new AjaxResult().error("没有找到该公司:"+companyName); } }</tbcommpany></object>###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); }###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: ###
带时间戳的分数 = 实际分数*10000000000 + (9999999999 – timestamp)###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!