自己设计排名系统的实现代码
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);