Implementation of ranking function based on Redis in java - with source code

Keywords: Java Database Redis

In my spare time, I sorted out the ranking function previously realized by Redis Zset data type.

preface

The reason why redis was selected for the ranking function is that there are a large amount of user data, nearly 200000 people, and it is not impossible to check the top 100 ranking from the database, but it is not enough to see compared with redis, so the ranking function was done in redis at that time.

Questions to think about before doing?

Redis has so many data types. Which data type do you use?
We can select according to the demand characteristics. Firstly, the ranking list is orderly and there can be no duplicate data. According to this characteristic, it is found that the data type Zset meets our requirements.

How does Zset store multiple required fields?

Zset can only store value and score. It needs the user's nickname, avatar, user id and score. How can it be stored. First, we can store the sorting value score in score. As for the user nickname, avatar and user id, we can separate them with separators and store them in value. For example, 10002# Wang Er# https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83erHB8aIib1R0B0iadpMmHRsyLF2loxLia7Mx3aZv9obHGgUJcl0ibBBys4QRZwAL0J5a81VRfU1jHBexg/132

Jedis is selected as the Java client development kit.

If you don't say much, go to the renderings first


Data storage format

As mentioned earlier, the user ranking needs the user's nickname, avatar, and even the user id, so I store it like this.

Multiple fields are stored in value and # separated. When you use it, you can get an array through split(), and you can get these data. Of course, you can also store them in json format, etc.

code

//Two methods of ServiceImp layer

Jedis jedis = new Jedis("127.0.0.1", 6379);

    @Override
    public List<UserScore> getUserScoreList() {
        // Take out the top five according to the ranking of user scores
        Set<Tuple> tuples = jedis.zrevrangeWithScores("user-score", 0, 4);
        List<UserScore> list = new ArrayList<>();
        int ranking = 0;
        for (Tuple tuple : tuples) {
            UserScore us = new UserScore();
            String element = tuple.getElement();
            String[] arr = element.split("#");
            us.setScore((int) tuple.getScore());
            ranking++;
            us.setRanking(ranking);
            us.setName(arr[1]);
            us.setImg(arr[2]);
            list.add(us);
        }
        return list;
    }

@Override
    public Long insertUserScore(UserScore userScore) {
        String value = userScore.getUserid()+"#"+userScore.getName()+"#"+userScore.getImg();
        final Long zadd = jedis.zadd(Constants.USER_SCORE, userScore.getScore(), value);
        return zadd;
    }
//Three methods of controller layer
 @RequestMapping("/")
    public ModelAndView home() {

        ModelAndView view = new ModelAndView("index");

        List<UserScore> rankingList = userService.getUserScoreList();
        view.addObject("rankingList", rankingList);
        return view;
    }

    @RequestMapping("/add")
    public ModelAndView add() {

        ModelAndView view = new ModelAndView("add");

        return view;
    }

    @RequestMapping("/addUserScore")
    public ModelAndView addUserScore(UserScore userScore) {

        ModelAndView view = new ModelAndView("index");
        Long zadd = userService.insertUserScore(userScore);
        if(zadd !=null && zadd!=0){
            List<UserScore> rankingList = userService.getUserScoreList();
            view.addObject("rankingList", rankingList);
        }
        return view;
    }

The code is very simple, but those who haven't done it think it will be very troublesome.
If these codes can't meet your needs, look at the source code. After the source code is down, run and you'll know what's going on.

Source download

Source address: https://download.csdn.net/download/nxw_tsp/25866908

After downloading the source code, please see: README.MD

Posted by howard-moore on Mon, 11 Oct 2021 19:43:36 -0700