用Redis实现排行榜的详细用例

下面是一个使用 Redis 的 Sorted Set 数据结构实现的排行榜系统的详细示例,包括查看全部排名、获取单个排名、增加分数等操作。我们将使用 Lettuce 库来与 Redis 进行交互。

项目结构

  1. pom.xml:添加必要的依赖。
  2. LeaderBoardService:实现排行榜的功能。
  3. LeaderBoardApplication:测试排行榜功能。

第一步:添加必要的依赖

在你的 Maven 项目的 pom.xml 文件中添加 Lettuce 和 Spring Data Redis 的依赖:

xml 复制代码
<dependencies>
    <!-- Spring Boot Starter Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Spring Data Redis -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    <!-- Lettuce dependency for Redis interaction -->
    <dependency>
        <groupId>io.lettuce.core</groupId>
        <artifactId>lettuce-core</artifactId>
        <version>6.1.5</version>
    </dependency>
</dependencies>

第二步:编写 LeaderBoardService 类

这个类实现所有与排行榜相关的操作,包括添加玩家、增加分数、查看全部排名和获取单个排名。

java 复制代码
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class LeaderBoardService {
    private static final String LEADERBOARD_KEY = "game:leaderboard";
    private RedisClient redisClient;
    private StatefulRedisConnection<String, String> connection;
    private RedisCommands<String, String> commands;

    public LeaderBoardService() {
        this.redisClient = RedisClient.create("redis://localhost:6379");
        this.connection = redisClient.connect();
        this.commands = connection.sync();
    }

    // 添加或者更新玩家分数
    public void updatePlayerScore(String player, double score) {
        commands.zadd(LEADERBOARD_KEY, score, player);
    }

    // 增加玩家分数
    public void incrementPlayerScore(String player, double increment) {
        commands.zincrby(LEADERBOARD_KEY, increment, player);
    }

    // 获取玩家的排名(从0开始)
    public Long getPlayerRank(String player) {
        return commands.zrevrank(LEADERBOARD_KEY, player);
    }

    // 获取玩家的分数
    public Double getPlayerScore(String player) {
        return commands.zscore(LEADERBOARD_KEY, player);
    }

    // 获取全部排名列表
    public List<Map.Entry<String, Double>> getAllPlayers() {
        return commands.zrevrangeWithScores(LEADERBOARD_KEY, 0, -1)
                .stream()
                .map(entry -> new AbstractMap.SimpleEntry<>(entry.getValue(), entry.getScore()))
                .collect(Collectors.toList());
    }

    public void close() {
        connection.close();
        redisClient.shutdown();
    }

    public static void main(String[] args) {
        LeaderBoardService leaderBoardService = new LeaderBoardService();

        // 添加玩家和分数
        leaderBoardService.updatePlayerScore("player1", 500);
        leaderBoardService.updatePlayerScore("player2", 300);
        leaderBoardService.updatePlayerScore("player3", 400);
        
        // 增加玩家分数
        leaderBoardService.incrementPlayerScore("player2", 200);

        // 获取玩家排名
        Long rank = leaderBoardService.getPlayerRank("player2");
        System.out.println("Player2's rank: " + rank);

        // 获取玩家分数
        Double score = leaderBoardService.getPlayerScore("player2");
        System.out.println("Player2's score: " + score);

        // 获取全部排名列表
        List<Map.Entry<String, Double>> players = leaderBoardService.getAllPlayers();
        for (Map.Entry<String, Double> entry : players) {
            System.out.println("Player: " + entry.getKey() + ", Score: " + entry.getValue());
        }

        leaderBoardService.close();
    }
}

代码解释

1. 初始化 Redis 连接

使用 Lettuce 初始化 Redis 客户端连接:

java 复制代码
public LeaderBoardService() {
    this.redisClient = RedisClient.create("redis://localhost:6379");
    this.connection = redisClient.connect();
    this.commands = connection.sync();
}
2. 添加或更新玩家分数

使用 ZADD 命令添加或更新玩家的分数:

java 复制代码
public void updatePlayerScore(String player, double score) {
    commands.zadd(LEADERBOARD_KEY, score, player);
}
3. 增加玩家分数

使用 ZINCRBY 命令增加玩家的分数:

java 复制代码
public void incrementPlayerScore(String player, double increment) {
    commands.zincrby(LEADERBOARD_KEY, increment, player);
}
4. 获取玩家排名

使用 ZREVRANK 命令获取玩家的排名,排名是从0开始的:

java 复制代码
public Long getPlayerRank(String player) {
    return commands.zrevrank(LEADERBOARD_KEY, player);
}
5. 获取玩家分数

使用 ZSCORE 命令获取玩家的分数:

java 复制代码
public Double getPlayerScore(String player) {
    return commands.zscore(LEADERBOARD_KEY, player);
}
6. 获取全部排名列表

使用 ZREVRANGE 命令获取所有玩家的排名和分数:

java 复制代码
public List<Map.Entry<String, Double>> getAllPlayers() {
    return commands.zrevrangeWithScores(LEADERBOARD_KEY, 0, -1)
            .stream()
            .map(entry -> new AbstractMap.SimpleEntry<>(entry.getValue(), entry.getScore()))
            .collect(Collectors.toList());
}

第三步:编写 LeaderBoardApplication 类

main 方法中调用 LeaderBoardService 的方法,测试排行榜系统的功能。

java 复制代码
public class LeaderBoardApplication {

    public static void main(String[] args) {
        LeaderBoardService leaderBoardService = new LeaderBoardService();

        // 添加玩家和分数
        leaderBoardService.updatePlayerScore("player1", 500);
        leaderBoardService.updatePlayerScore("player2", 300);
        leaderBoardService.updatePlayerScore("player3", 400);
        
        // 增加玩家分数
        leaderBoardService.incrementPlayerScore("player2", 200);

        // 获取玩家排名
        Long rank = leaderBoardService.getPlayerRank("player2");
        System.out.println("Player2's rank: " + rank);

        // 获取玩家分数
        Double score = leaderBoardService.getPlayerScore("player2");
        System.out.println("Player2's score: " + score);

        // 获取全部排名列表
        List<Map.Entry<String, Double>> players = leaderBoardService.getAllPlayers();
        for (Map.Entry<String, Double> entry : players) {
            System.out.println("Player: " + entry.getKey() + ", Score: " + entry.getValue());
        }

        leaderBoardService.close();
    }
}

总结

通过上述步骤,我们实现了一个基于 Redis Sorted Set 的排行榜系统。该系统包括添加或更新玩家分数、增加玩家分数、获取单个玩家的排名和分数、查看全体玩家排名等功能。使用 Lettuce 库,我们能够高效地与 Redis 进行交互,实现高性能的实时排行榜系统。这个例子展示了 Redis Sorted Set 在实际应用场景中的强大能力。

相关推荐
用户3169353811832 天前
Java连接Redis
redis
倔强的石头_2 天前
《Kingbase护城河》——数据库存储空间全景探测与精细化瘦身实战
数据库
冬奇Lab3 天前
每日一个开源项目(第134篇):Zvec - 阿里开源的嵌入式向量数据库,向量搜索界的 SQLite
数据库·人工智能·llm
ClouGence3 天前
Oracle CDC 架构优化:从主库直连到 DataGuard 备库同步
数据库·后端·oracle
无响应de神3 天前
三、用户与权限管理
数据库·mysql
小小工匠4 天前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
麦聪聊数据4 天前
数据服务化时代:企业数据能力输出的核心路径
数据库
shushangyun_4 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
DARLING Zero two♡4 天前
【MySQL数据库】数据类型与表约束
数据库·mysql
ofoxcoding4 天前
在AI API聚合平台配置DeepSeek V3.2提示词缓存实战:快速接入与成本优化指南
人工智能·spring·缓存·ai