当前位置:网站首页>Redis realizes ranking function
Redis realizes ranking function
2022-07-04 05:17:00 【Be natural and unrestrained】
「 This is my participation 2022 For the first time, the third challenge is 28 God , Check out the activity details :2022 For the first time, it's a challenge 」
Preface
When I was on the consumer finance platform , The company has a special for offline sales staff APP,APP Record the Commission earned by selling and promoting the company's loan products and the functions of some store development and clock in , The backend was developed by me and another colleague , One module is the real-time ranking of sales commissions in national stores , When it comes to ranking, many people's first reaction is that this is a Top N The problem of , Take it out of the database and use it MySQL Of top The function can be implemented , In fact, we couldn't get the data from the table at that time , Data should also be matched with permissions , There are national and regional rankings , It is also necessary to calculate the effective Commission of all personnel under the regional manager , But also real-time , Reading and recalculating from the database is definitely not enough , Jump to the ranking page and wait at least 5s Left and right data can come out , Then what shall I do? , It can be released Redis in , Then let's see how to use Redis Achieve this ranking function .
One . Realize the idea
It uses Redis in zset data type ,zset The definition of is summarized here, that is, each element can be associated with a score, and it can also sort the collection elements , So this is a good place to sort , Next, let's see how to use its ranking function .
Two . Concrete realization
1. The method of adding data is wrapped
public void zAdd(String key,Object member,double score){
try {
redisTemplate.opsForZSet().add(key,member,score);
} catch (Exception e) {
log.error("redis zAdd has a error,key:{},value:{},score:{},exception:{}",key,member,score,e);
}
}
Copy code 2. Data acquisition method packaging
public Set<Object> zRange(String key,int start,int end){
try {
// Take values and scores in reverse order
Set<ZSetOperations.TypedTuple<Object>> typedTuples = redisTemplate.opsForZSet().reverseRangeWithScores(key, start, end);
if(typedTuples==null||typedTuples.size()==0) return null;
return Collections.singleton(typedTuples);
} catch (Exception e) {
log.error("redis zRange has a error,key:{},start:{},end:{},exception:{}",
key,start,end,e);
return null;
}
}
Copy code 3. The test method , Add out of order , If you want the commission from less to more, just add a minus sign before the Commission
redisUtils.zAdd("rank"," Wang Wu ",new Double("2000.00"));
redisUtils.zAdd("rank"," Zhang San ",new Double("1000.00"));
redisUtils.zAdd("rank"," Wang Ke ",new Double("4000.00"));
redisUtils.zAdd("rank"," Xiang Qiaoqiao ",new Double("6000.00"));
redisUtils.zAdd("rank"," Sha Zhenhua ",new Double("7000.00"));
redisUtils.zAdd("rank"," A lot of money ",new Double("5000.00"));
redisUtils.zAdd("rank"," Huang San ",new Double("3000.00"));
redisUtils.zAdd("rank"," Gao Qiu ",new Double("8000.00"));
redisUtils.zAdd("rank"," xu ",new Double("9000.00"));
redisUtils.zAdd("rank"," Bao Hu ",new Double("10000.00"));
// Get add into redis The data of , Using the above 2 Method
Set<Object> rank = redisUtils.zRange("rank", 0, 9);
//todo Get the data for other logical processing
// Print the results
rank.forEach(System.out::println);
Copy code 4. Execution results
[DefaultTypedTuple [score=10000.0, value= Bao Hu ],
DefaultTypedTuple [score=9000.0, value= xu ],
DefaultTypedTuple [score=8000.0, value= Gao Qiu ],
DefaultTypedTuple [score=7000.0, value= Sha Zhenhua ],
DefaultTypedTuple [score=6000.0, value= Xiang Qiaoqiao ],
DefaultTypedTuple [score=5000.0, value= A lot of money ],
DefaultTypedTuple [score=4000.0, value= Wang Ke ],
DefaultTypedTuple [score=3000.0, value= Huang San ],
DefaultTypedTuple [score=2000.0, value= Wang Wu ],
DefaultTypedTuple [score=1000.0, value= Zhang San ]]
Copy code Summary
zset In addition to the method of calculating the ranking, there is also the method of calculating the number of set conditions zcount Method , View the total number of sets zcard Methods, etc. , It's very convenient to use , But it's still the same sentence. Only in specific business can we know whether it's practical or not , And also pay attention to Redis There's a data elimination strategy , Don't ignore this point , In addition, the discarded business data is still cached in Redis Also remember to clear and check the inside .
边栏推荐
- Download kicad on Alibaba cloud image station
- 2022g2 power station boiler stoker special operation certificate examination question bank and answers
- Notepad++--显示相关的配置
- 《Cross-view Transformers for real-time Map-view Semantic Segmentation》论文笔记
- Daily question brushing record (12)
- [QT] timer
- 2022 a special equipment related management (elevator) examination questions simulation examination platform operation
- 小程序毕业设计---美食、菜谱小程序
- Evolution of system architecture: differences and connections between SOA and microservice architecture
- [matlab] matlab simulates digital baseband transmission system - digital baseband transmission system
猜你喜欢

Headache delayed double deletion

RSA加密应用常见缺陷的原理与实践

Analysis of classical pointer and array written test questions in C language

2022年A特种设备相关管理(电梯)考试题模拟考试平台操作

中科磐云—2022广东木马信息获取解析

2022 question bank and answers for safety management personnel of hazardous chemical business units

定制一个自己项目里需要的分页器

ping端口神器psping

模拟小根堆

LM小型可编程控制器软件(基于CoDeSys)笔记二十一:错误3703
随机推荐
[matlab] general function of communication signal modulation inverse Fourier transform
EVM proof in appliedzkp zkevm (11)
Several smart watch related chips Bluetooth chip low power consumption
Annex 4: scoring criteria of the attacker docx
[matlab] matlab simulation modulation system FM system
中科磐云—数据分析与取证数据包flag
C语言简易学生管理系统(含源码)
Notes on the paper "cross view transformers for real time map view semantic segmentation"
Customize a pager needed in your project
企业级日志分析系统ELK(如果事与愿违那一定另有安排)
Encryption and decryption
ping端口神器psping
Unity is connected to the weather system
[matlab] matlab simulates digital baseband transmission system eye diagram of bipolar baseband signal (cosine roll off forming pulse)
中職組網絡安全—內存取證
LM small programmable controller software (based on CoDeSys) note XXI: error 3703
光模块字母含义及参数简称大全
2022年R2移动式压力容器充装复训题库及答案
Rollup各组件作用
KMP match string