## 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 listWhat 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)
ZADD "KEY" SCORE "VALUE" [ SCORE "VALUE"]
MyRedis:0>ZADD test 1 "one""1"MyRedis:0>zadd test 4 "four" 5 "five""2"
ZCARD "key"
MyRedis:0>ZCARD test"5"
ZSCORE "KEY" "VALUE"
MyRedis:0>ZSCORE "test" "one""2"
ZINCRBY "key" score "value"
MyRedis:0>ZSCORE "test" "one""2"MyRedis:0>ZINCRBY "test" 1 "one""3"MyRedis:0>ZSCORE "test" "one" "3"
ZRANGE "key" 开始下标 结束下标
MyRedis:0>ZRANGE "test" 0 1 1) "two" 2) "one"
org.springframework.boot spring-boot-starter-data-redis
//=============================== 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; } }
/** * 根据公司名找到指定公司 * @param companyName * @return */ @Override public AjaxResult selectCompanyName(String companyName) { Set
/** * 获得公司排行榜(前十) * @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); }
带时间戳的分数 = 实际分数*10000000000 + (9999999999 – timestamp)
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!