关于实时排名系统设计的一点资料

自己设计排名系统的实现代码

redis

使用 redis 的 SortedSet 数据结构;

https://redis.io/commands#sorted_set

http://redisdoc.com/sorted_set/index.html

http://www.redis.cn/commands.html#sorted_set

java客户端 jedis

@Service
@Slf4j
public class ActivityItemScoreAndRankService {

    @Autowired
    JedisPool jedisPool;

    @Autowired
    ActivityUserRepository activityUserRepository;

    public static final String YOUFAN_ACTIVITY_STAGE_ = "youfan:activity:stage:";

    public static final Long NOW_ACTIVITY_ID = 1L;

    /**
     * 加票数
     */
    public Long incrby(String activityId, Long activityItemId) {
        return incrby(activityId, activityItemId, 1);
    }

    public Long incrby(String activityId, Long activityItemId, int score) {
        String key = YOUFAN_ACTIVITY_STAGE_ + activityId;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String member = getMember(activityItemId);
            return jedis.zincrby(key, score, member).longValue();
        } catch (Exception e) {
            log.error("ActivityItemScoreAndRankService incrby : ", e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return 0L;
    }

    /**
     * 获取票数
     */
    public Long getScore(String activityId, Long activityItemId) {
        String key = YOUFAN_ACTIVITY_STAGE_ + activityId;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String member = getMember(activityItemId);

            Double longSorce = jedis.zscore(key, member);
            if (Objects.isNull(longSorce)) {
                return null;
            }
            return longSorce.longValue();
        } catch (Exception e) {
            log.error("ActivityItemScoreAndRankService getScore : ", e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return 0L;
    }

    /**
     * 获取排名名次
     */
    public Integer getRank(String activityId, Long activityItemId) {
        String key = YOUFAN_ACTIVITY_STAGE_ + activityId;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String member = getMember(activityItemId);

            Long rankLong = jedis.zrevrank(key, member);
            if (Objects.isNull(rankLong)) {
                return null;
            }
            Integer rank = rankLong.intValue();
            if (Objects.isNull(rank)) {
                return null;
            }
            return rank + 1;
        } catch (Exception e) {
            log.error("ActivityItemScoreAndRankService getRank : ", e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return 0;
    }

    /**
     * 获取 名次范围 内的列表
     *
     * @param activityId
     * @param startRank  起始名次
     * @param endRank    终止名次
     * @return
     */
    public Set<String> getRankForRange(String activityId, Integer startRank, Integer endRank) {
        String key = YOUFAN_ACTIVITY_STAGE_ + activityId;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.zrevrange(key, startRank - 1, endRank - 1);
        } catch (Exception e) {
            log.error("ActivityItemScoreAndRankService getRankForRange : ", e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return Collections.emptySet();
    }

    private String getMember(Long activityItemId) {
        return String.valueOf(activityItemId);
    }

    /**
     * 获取基数(总数量)
     *
     * @param activityId
     * @return
     */
    public Long getCount(String activityId) {
        String key = YOUFAN_ACTIVITY_STAGE_ + activityId;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.zcard(key);
        } catch (Exception e) {
            log.error("ActivityItemScoreAndRankService getCount : ", e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return 0L;
    }

    private String getMember(Long activityItemId) {
        return String.valueOf(activityItemId);
    }

}

mysql

    // votes 得票数
    @Query(nativeQuery = true,
            value = "SELECT rank FROM ( " +
                    "SELECT id,votes, @curRank \\:= @curRank + 1 AS rank " +
                    "FROM xxx, (" +
                    "SELECT @curRank \\:= 0 " +
                    ") q " +
                    "WHERE deleted = 0 " +
                    "ORDER BY votes DESC " +
                    ") x WHERE id = ?1 ")
    Integer eranRank(Long xxId);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值