- 背景
- 用户玩游戏-积分实时榜单
- IT视频热销实时榜单
- 电商商品热销实时榜单
- 一般的排行榜读多写少,可以对 master 进行写入操作,然后多个 slave 进行读取操作。
- 如果是对象记得重写HashCode与Equals方法
- 对象准备
package net.xdclass.xdclassredis.vo; import java.util.Objects; public class UserPointVO { public UserPointVO(String username, String phone) { this.username = username; this.phone = phone; } private String username; private String phone; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; UserPointVO that = (UserPointVO) o; return Objects.equals(phone, that.phone); } @Override public int hashCode() { return Objects.hash(phone); } }
添加榜单模拟
@Test void testData() { UserPointVO p1 = new UserPointVO("老王","13113"); UserPointVO p2 = new UserPointVO("老A","324"); UserPointVO p3 = new UserPointVO("老B","242"); UserPointVO p4 = new UserPointVO("老C","542345"); UserPointVO p5 = new UserPointVO("老D","235"); UserPointVO p6 = new UserPointVO("老E","1245"); UserPointVO p7 = new UserPointVO("老F","2356432"); UserPointVO p8 = new UserPointVO("老G","532332"); BoundZSetOperations<String, UserPointVO> operations = redisTemplate.boundZSetOps("point:rank:real"); operations.add(p1,324); operations.add(p2,542); operations.add(p3,52); operations.add(p4,434); operations.add(p5,1123); operations.add(p6,64); operations.add(p7,765); operations.add(p8,8); }
接口开发
- 返回榜单-从大到小排序
- 查看这个人的排名,从大到小,0就是第一
- 给某个用户加积分
- 查看某个用户的积分
简单的代码示例
/** * 返回全部榜单,从大到小 * @return */ @RequestMapping("real_rank1") public JsonData realRank1() { BoundZSetOperations<String, UserPointVO> operations = redisTemplate.boundZSetOps("point:rank:real"); Set<UserPointVO> set = operations.reverseRange(0, -1); return JsonData.buildSuccess(set); } /** * 返回全部榜单,从小到大 * @return */ @RequestMapping("real_rank2") public JsonData realRank2() { BoundZSetOperations<String, UserPointVO> operations = redisTemplate.boundZSetOps("point:rank:real"); Set<UserPointVO> set = operations.range(0, -1); return JsonData.buildSuccess(set); } /** * 返回全部榜单,从大到小,指定长度 * @return */ @RequestMapping("real_rank3") public JsonData realRank3() { BoundZSetOperations<String, UserPointVO> operations = redisTemplate.boundZSetOps("point:rank:real"); Set<UserPointVO> set = operations.reverseRange(0, 3); return JsonData.buildSuccess(set); } /** * 查看某个用户的排名 * @param phone * @param name * @return */ @RequestMapping("find_myrank") public JsonData realMyRank(String phone,String name) { BoundZSetOperations<String, UserPointVO> operations = redisTemplate.boundZSetOps("point:rank:real"); UserPointVO userPointVO = new UserPointVO(name,phone); long rank = operations.reverseRank(userPointVO); return JsonData.buildSuccess(++rank); } /** * 加积分 * @param phone * @param name * @return */ @RequestMapping("uprank") public JsonData uprank(String phone,String name,int point) { BoundZSetOperations<String, UserPointVO> operations = redisTemplate.boundZSetOps("point:rank:real"); UserPointVO userPointVO = new UserPointVO(name,phone); operations.incrementScore(userPointVO,point); Set<UserPointVO> set = operations.range(0, -1); return JsonData.buildSuccess(set); } /** * 查看个人的积分 * @param phone * @param name * @return */ @RequestMapping("mypoint") public JsonData mypoint(String phone,String name) { BoundZSetOperations<String, UserPointVO> operations = redisTemplate.boundZSetOps("point:rank:real"); UserPointVO userPointVO = new UserPointVO(name,phone); double score = operations.score(userPointVO); return JsonData.buildSuccess(score); }
本文作者为DBC,转载请注明。