【lettuce-排行榜】

背景:

这次游戏中台采用lettuce的zset完成游戏内的本服和跨服排行榜,因此写一下案例。

pom.xml

    
      io.lettuce
      lettuce-core
      6.2.4.RELEASE
      
        
          netty-common
          io.netty
        
        
          netty-handler
          io.netty
        
        
          netty-transport
          io.netty
        
      
    

RedisManager.java

package org.example.testRank.manager;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import io.lettuce.core.api.sync.RedisCommands;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.time.Duration;

@Slf4j
public class RedisManager {

    private static RedisManager instance = new RedisManager();

    private RedisClient redisClient;
    private StatefulRedisConnection connection;

    /*** async */
    @Getter
    private RedisAsyncCommands asyncCommands;

    /*** sync*/
    @Getter
    private RedisCommands commands;

    public static RedisManager inst() {
        return instance;
    }

    public void init(String host, int port) {
        int dbIndex = 15;
        int timeout = 10;

        try {
            RedisURI uri = RedisURI.builder()
                    .withHost(host)
                    .withPort(port)
                    .withDatabase(dbIndex)
                    .withTimeout(Duration.ofSeconds(timeout)).build();

            redisClient = RedisClient.create(uri);
            connection = redisClient.connect();
            asyncCommands = connection.async();
            commands = connection.sync();
        } catch (Exception e) {
            log.error("redis init error=", e);
        }
    }

    public void close() {
        if (connection != null) {
            connection.close();
        }
        if (redisClient != null) {
            redisClient.close();
        }
    }
}

RankManager.java

package org.example.testRank.manager;

import com.google.common.collect.Lists;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.ScoredValue;
import lombok.extern.slf4j.Slf4j;
import org.example.testRank.model.RankInfo;
import org.example.testRank.model.RankItem;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
public class RankManager {
    private static RankManager instance = new RankManager();

    public static RankManager inst() {
        return instance;
    }

    /**
     * 尝试上榜
     * @param rankKey     排行榜类型
     * @param uid         玩家id
     * @param num         得分
     * @param increment   是否是增加 false的话直接设置为得分
     */
    public void updateRank(String rankKey, long uid, double num, boolean increment) {
        RedisFuture future = RedisManager.inst().getAsyncCommands().zscore(rankKey, uid + "");
        future.whenCompleteAsync((v, e) -> {
            if (increment && v != null) {
                RedisManager.inst().getAsyncCommands().zadd(rankKey, addNumAndGetScoreWithTime(v.doubleValue(), num), String.valueOf(uid));
            } else {
                RedisManager.inst().getAsyncCommands().zadd(rankKey, getScoreWithTime(num), String.valueOf(uid));
            }
        });
    }


    /**
     * 获取排行榜列表 + 自己的排名
     */
    public RankInfo getRankInfo(String rankKey, int start, int end, long selfUid) {
        RankInfo rankInfo = new RankInfo();

        List rankItems = Lists.newArrayList();

        List> list = RedisManager.inst().getCommands().zrevrangeWithScores(rankKey, start, end);

        int userRank = start;
        for (ScoredValue scoredValue : list) {
            userRank++;
            String uid = scoredValue.getValue();
            double score = getRealScore(scoredValue.getScore());
            rankItems.add(new RankItem(uid, userRank, (long) score));
        }

        rankInfo.setRankItems(rankItems);

        Long selfRankObj = RedisManager.inst().getCommands().zrevrank(rankKey, selfUid + "");
        Double selfScoreObj = RedisManager.inst().getCommands().zscore(rankKey, selfUid + "");

        rankInfo.setSelfRankItem(new RankItem(selfUid + "", selfRankObj == null ? 0 : selfRankObj.intValue()+1, selfScoreObj == null ? 0 : selfScoreObj.longValue()));

        return rankInfo;
    }

    private double getScoreWithTime(double score) {
        return score + (1 - Double.parseDouble("0." + System.currentTimeMillis()));
    }

    private double getRealScore(double score) {
        BigDecimal bigDecimal = new BigDecimal(score);
        String realScore = String.valueOf(bigDecimal).split("\\.")[0];
        return Double.parseDouble(realScore);
    }

    private double addNumAndGetScoreWithTime(double score, double addNum) {
        double num = getRealScore(score) + addNum;
        return getScoreWithTime(num);
    }
}

RankItem.java

package org.example.testRank.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;

@Data
@AllArgsConstructor
@ToString
public class RankItem {
    private String uid;
    private int rank;
    private long score;
}

RankInfo.java

package org.example.testRank.model;

import lombok.Data;
import lombok.ToString;

import java.util.List;

@Data
@ToString
public class RankInfo {
    private List rankItems;
    private RankItem selfRankItem;
}

Main.java

package org.example.testRank;

import lombok.extern.slf4j.Slf4j;
import org.example.testRank.manager.RankManager;
import org.example.testRank.manager.RedisManager;
import org.example.testRank.model.RankInfo;

@Slf4j
public class Main {
    public static String rankKey = "power_rank";

    public static void main(String[] args) {
        RedisManager.inst().init("localhost", 6379);


//        RankManager.inst().updateRank(rankKey, 1002, 10, true);
//
//        RankManager.inst().updateRank(rankKey, 1001, 10, true);

//        RankManager.inst().updateRank(rankKey, 1003, 100, true);

        RankInfo rankInfo = RankManager.inst().getRankInfo(rankKey, 0, -1, 1002);


        log.info("{}", rankInfo);
    }
}

/*
RankInfo(rankItems=[RankItem(uid=1003, rank=1, score=100), RankItem(uid=1001, rank=2, score=30), RankItem(uid=1002, rank=3, score=10)], selfRankItem=RankItem(uid=1002, rank=3, score=10))
 */

redis中查看下

【lettuce-排行榜】_第1张图片

你可能感兴趣的:(#,redis,java,服务器)