“This is the 28th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

preface

Before when consumer finance platform, the company has a dedicated to offline sales staff to use the APP, the APP record sales promotion company loan product to earn commissions and development and the function of the clock in some stores the backend is developed by me and another colleague, one of the modules are real-time sales commission stores across the country, When it comes to most people’s first reaction is rankings this is a matter of Top N, removed from a database using MySQL Top function can not do, in fact at that time, not from the table to the data, data with permissions, even with the national and regional rankings, also need to calculate all staff under the regional manager of commission effectively, but also realistic real-time, Read from the database and then calculate definitely not, jump to the ranking page at least wait for about 5s data can come out, then what to do, you can put in Redis, then let’s see how to use Redis to achieve this ranking function.

One. Implementation ideas

The Redis zset data type is used, the definition of zset is summarized here is that each element can be associated with a score and can be sorted on set elements, so this is suitable for sorting, let’s see how to use it to implement ranking function.

Two. Concrete implementation

1. Add method wrappers for data

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 the code

2. Method packaging for obtaining data

public  Set<Object> zRange(String key,int start,int end){
    try {
    // Values and fractions in reverse order of position
        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 the code

3. Test method, disorderly add, if you want to commission from less to more add a negative sign before the commission

redisUtils.zAdd("rank"."Fifty".new Double("2000.00"));
redisUtils.zAdd("rank"."Zhang".new Double("1000.00"));
redisUtils.zAdd("rank"."Walker".new Double("4000.00"));
redisUtils.zAdd("rank"."Xiang Qiao Qiao".new Double("6000.00"));
redisUtils.zAdd("rank"."Sha Zhenhua".new Double("7000.00"));
redisUtils.zAdd("rank"."Lots of money.".new Double("5000.00"));
redisUtils.zAdd("rank"."Yellow".new Double("3000.00"));
redisUtils.zAdd("rank"."High qiu".new Double("8000.00"));
redisUtils.zAdd("rank"."许晴".new Double("9000.00"));
redisUtils.zAdd("rank"."Package tiger".new Double("10000.00"));
// Get the data added to Redis using method 2 above
Set<Object> rank = redisUtils.zRange("rank".0.9);
// Todo gets the data for other logic processing
// Print the result
rank.forEach(System.out::println);
Copy the code

4. Execution result

[DefaultTypedTuple [score=10000.0, value= packet tiger], DefaultTypedTuple [score=9000.0, value= xu Qing], DefaultTypedTuple [score=8000.0, value= high], DefaultTypedTuple [score=7000.0, value= Sha Zhenhua], DefaultTypedTuple [score=6000.0, value= to Qiao qiao], DefaultTypedTuple [score=5000.0, value= more money], DefaultTypedTuple [score=4000.0, value= Wang Ke], DefaultTypedTuple [score=3000.0, value= Yellow three], DefaultTypedTuple [score=2000.0, value= wang5], DefaultTypedTuple [score=1000.0, value= zhang SAN]]Copy the code

summary

Zset collection in addition to the ranking method and calculation conditions in the number of zcount method, view collection total number zcard method and so on, is very convenient to use, but that sentence still specific only to the specific business just know practical, but also pay attention to Redis data selection strategy, this point also don’t ignore, Also remember to check and remove business data that has been discarded and still cached in Redis.