docs
uredis
Product Application
Application Scenario
Get TOP N in ranking

Leaderboard TOP N

In game servers, there are many ranking-related tasks, such as retrieving TOP N operations for player level rankings, money rankings, combat power rankings, and more. While these can be implemented using relational databases, the sorting pressure on the database increases as the volume of data grows.

For Redis, these operations are straightforward. Even if you have millions of users generating millions of new scores every minute, Redis can handle these tasks effortlessly.

You only need jedis.zrevrank(key,member);

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;
public class HelloWorldApp {
    static int TOTAL_SIZE = 10000;
    // Obtain a random name composed of lowercase letters of length 8
    public static String randomName(int length) {
        StringBuilder builder = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            builder.append((char) ThreadLocalRandom.current().nextInt(97,122));//a~z
        }
        return builder.toString();
    }
    public static void main(String[] args)
    {
        // Connection information can be obtained from the console
        String host = "127.0.0.1";
        int port = 10011;
        Jedis jedis = new Jedis(host, port);
        // Leaderboard application, perform the TOP N operation
        try {
            //Key
            String key = "Game Leaderboard!";
            //Clear possible existing data
            jedis.del(key);
            //Simulate generating a number of game players
            List<String> playerList = new ArrayList<String>();
            for (int i = 0; i < TOTAL_SIZE; ++i)
            {
                //Randomly generate the name of each player
                playerList.add(randomName(8));
            }
            System.out.println("Input all " + TOTAL_SIZE +" players ");
            //Record the score of each player
            for (int i = 0; i < playerList.size(); i++)
            {
                //Random number generation, simulating the game score of the player
                int score = (int)(Math.random()*5000);
                String member = playerList.get(i);
                if (i < 10) {
                    System.out.println("Player name:" + member + ", Player score: " + score
                    );
                }
                //Add the player's name and score to the corresponding key's SortedSet
                jedis.zadd(key, score, member);
            }
            System.out.println("More player scores......");
            //Get the sorted player list from the SortedSet of the corresponding key
            Set<Tuple> scoreList = jedis.zrevrangeWithScores(key, 0, -1);
            //Print out the Top 100 player leaderboard rankings
            System.out.println();
            System.out.println(" "+key);
            System.out.println(" Top 100 Players");
            scoreList = jedis.zrevrangeWithScores(key, 0, 99);
            for (Tuple item : scoreList) {
                System.out.println("Player name:"+item.getElement()+", Player score:"+Doubl
                e.valueOf(item.getScore()).intValue());
            }
            //Output the ranking of a certain player
            String player = playerList.get(0);
            System.out.println();
            System.out.println(" "+key);
            System.out.println(" Player "+player+" ranking: "+ jedis.zrevrank(key,player));
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            jedis.quit();
            jedis.close();
        }
    }
}

Output:

Input all 10000 players
Player name:hegmsrcs, Player score: 1191
Player name:ocvhhxke, Player score: 653
Player name:ekgdllwj, Player score: 694
Player name:kxwsnnjj, Player score: 2051
Player name:vjflcktn, Player score: 2100
Player name:jtrlmnlk, Player score: 4257
Player name:aatbchgk, Player score: 2912
Player name:phukvvxy, Player score: 2044
Player name:aqqdqnel, Player score: 1859
Player name:hyndvsen, Player score: 2381
More player scores......
Game Leaderboard!
Top 100 Players
Player name:kqyhxehe, Player score:4999
Player name:datnveli, Player score:4998
Player name:ovxfislm, Player score:4997
Player name:llqnigun, Player score:4997
Player name:ckikmasa, Player score:4997
Player name:wlmdrpnx, Player score:4996
Player name:trslhgga, Player score:4996
Player name:bkbfnutg, Player score:4996
Player name:yqesafda, Player score:4995
Player name:grtjbjrq, Player score:4995
More player rankings......
Game Leaderboard!
Player hegmsrcs ranking: 7618
  • Company
  • ContactUs
  • Blog
Copyright © 2024 SurferCloud All Rights Reserved