当前位置:网站首页>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 .
边栏推荐
- Notepad++--显示相关的配置
- appliedzkp zkevm(11)中的EVM Proof
- Trie数-字典树
- 2022 R2 mobile pressure vessel filling retraining question bank and answers
- 2022危险化学品经营单位安全管理人员上岗证题库及答案
- KMP匹配字符串
- Automated testing selenium foundation -- webdriverapi
- Topological sorting and graphical display of critical path
- 【QT】定时器
- Zkevm (12) state proof of appliedzkp
猜你喜欢

数据标注是一块肥肉,盯上这块肉的不止中国丨曼孚科技

A summary of the 8544 problem that SolidWorks Standard cannot obtain a license

令人头痛的延时双删

补某视频网站的js,进行视频解密

LM small programmable controller software (based on CoDeSys) note XXI: error 3703

中职组网络安全—内存取证

Evolution of system architecture: differences and connections between SOA and microservice architecture

Secondary vocational group network security - memory Forensics

简单g++和gdb调试
![[QT] timer](/img/df/5db6af851ef19f33fd7e7a7ed46586.png)
[QT] timer
随机推荐
When using flash to store parameters, the code area of flash is erased, which leads to the interrupt of entering hardware error
NTFS security permissions
[matlab] matlab simulation - low pass Gaussian white noise
Annex I: power of attorney for 202x XXX attack and defense drill
如何构建属于自己的知识引擎?社群开放申请
Appearance of LabVIEW error dialog box
With the advent of the IP era, how can E-sports hotels take advantage of the "east wind" of games?
Trie number dictionary tree
Integer type of C language
laravel 中获取刚刚插入的记录的id
appliedzkp的zkevm(12)State Proof
Enterprise level log analysis system elk (if things backfire, there must be other arrangements)
flink1.13 sql基础语法(二)join操作
光模块字母含义及参数简称大全
[matlab] matlab simulation modulation system FM system
Character types of C language
Annex 4: scoring criteria of the attacker docx
We believe that the development of consumer Internet will still be limited to the Internet industry itself
2022年T电梯修理操作证考试题库及模拟考试
Zhongke Panyun - 2022 Guangxi reverse analysis ideas