feat: 更新 Redis 示例

pull/22/head
dunwu 2022-06-08 21:03:22 +08:00
parent 7c791cd950
commit 9f69c6f192
2 changed files with 39 additions and 22 deletions

View File

@ -67,6 +67,9 @@ public class RankDemo {
public RankElement getRankByMember(String member) { public RankElement getRankByMember(String member) {
if (isRegionRankEnabled) { if (isRegionRankEnabled) {
RankRegionElement element = getRankByMemberWithRegions(member); RankRegionElement element = getRankByMemberWithRegions(member);
if (element == null) {
return null;
}
return BeanUtil.toBean(element, RankElement.class); return BeanUtil.toBean(element, RankElement.class);
} else { } else {
// 排行榜采用不分区方案 // 排行榜采用不分区方案
@ -137,16 +140,21 @@ public class RankDemo {
* @return / * @return /
*/ */
public RankElement getRankByMemberWithNoRegions(String member) { public RankElement getRankByMemberWithNoRegions(String member) {
Long rank = jedis.zrevrank(RANK, member); Pipeline pipeline = jedis.pipelined();
if (rank != null) { Response<Long> rankResponse = pipeline.zrevrank(RANK, member);
Set<Tuple> tuples = jedis.zrevrangeWithScores(RANK, rank, rank); Response<Double> scoreResponse = pipeline.zscore(RANK, member);
for (Tuple tuple : tuples) { pipeline.syncAndReturnAll();
if (tuple.getElement().equals(member)) {
return new RankElement(member, tuple.getScore(), rank); if (rankResponse == null || scoreResponse == null) {
} return null;
}
} }
return new RankElement(member, null, TOTAL_RANK_LENGTH);
Long rank = rankResponse.get();
Double score = scoreResponse.get();
if (rank == null || score == null) {
return null;
}
return new RankElement(member, score, rank);
} }
/** /**
@ -214,13 +222,15 @@ public class RankDemo {
* @return / * @return /
*/ */
public RankRegionElement getRankByMemberWithRegions(String member) { public RankRegionElement getRankByMemberWithRegions(String member) {
long totalRank = TOTAL_RANK_LENGTH;
// pipeline 合并查询 // pipeline 合并查询
List<Response<Long>> responseList = new LinkedList<>(); List<Map<String, Response<?>>> responseList = new LinkedList<>();
Pipeline pipeline = jedis.pipelined(); Pipeline pipeline = jedis.pipelined();
for (RankRegion region : REGIONS) { for (RankRegion region : REGIONS) {
responseList.add(pipeline.zrevrank(region.getRegionKey(), member)); Map<String, Response<?>> map = new HashMap<>(2);
map.put("rank", pipeline.zrevrank(region.getRegionKey(), member));
map.put("score", pipeline.zscore(region.getRegionKey(), member));
responseList.add(map);
} }
pipeline.syncAndReturnAll(); pipeline.syncAndReturnAll();
@ -231,18 +241,25 @@ public class RankDemo {
// 处理 pipeline 查询结果 // 处理 pipeline 查询结果
for (int i = 0; i < responseList.size(); i++) { for (int i = 0; i < responseList.size(); i++) {
Response<Long> response = responseList.get(i); Map<String, Response<?>> map = responseList.get(i);
if (response != null && response.get() != null) { Response<?> rankResponse = map.get("rank");
Long rank = response.get(); Response<?> scoreResponse = map.get("score");
RankRegion region = REGIONS.get(i); if (rankResponse == null && scoreResponse == null) {
totalRank = getTotalRank(region.getRegionNo(), rank); continue;
return new RankRegionElement(region.getRegionNo(), region.getRegionKey(), member, null, rank,
totalRank);
} }
Long rank = (Long) rankResponse.get();
Double score = (Double) scoreResponse.get();
if (rank == null || score == null) {
continue;
}
RankRegion region = REGIONS.get(i);
long totalRank = getTotalRank(region.getRegionNo(), rank);
return new RankRegionElement(region.getRegionNo(), region.getRegionKey(), member, score, rank, totalRank);
} }
int lastRegionNo = getLastRegionNo(); return null;
return new RankRegionElement(lastRegionNo, getRankRedisKey(lastRegionNo), member, null, null, totalRank);
} }
/** /**

View File

@ -95,7 +95,7 @@ public class RankDemoTests {
RankElement rank1 = rank.getRankByMember(member1); RankElement rank1 = rank.getRankByMember(member1);
RankElement rank2 = rank.getRankByMember(member2); RankElement rank2 = rank.getRankByMember(member2);
Assertions.assertEquals(RankDemo.FIRST, rank1.getTotalRank()); Assertions.assertEquals(RankDemo.FIRST, rank1.getTotalRank());
Assertions.assertEquals(RankDemo.TOTAL_RANK_LENGTH, rank2.getTotalRank()); Assertions.assertNull(rank2);
} }