生活随笔
收集整理的這篇文章主要介紹了
Redis(案例六:ZSet数据)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
案例實戰之SortedSet?戶積分實時榜單最佳實踐
背景
?戶玩游戲-積分實時榜單
IT視頻熱銷實時榜單
電商商品熱銷實時榜單
?般的排?榜讀多寫少,可以對 master 進?寫?操作,然后多個 slave 進?讀取操作。如果是對象記得重寫HashCode與Equals?法
UserPointVO
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
;}@Overridepublic 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
);}@Overridepublic int hashCode() {return Objects.hash(phone
);}
}
加入測試數據
@SpringBootTest
class XdclassRedisApplicationTests {@Autowiredprivate RedisTemplate redisTemplate
;@Testvoid 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就是第?
給某個?戶加積分
查看某個?戶的積分
import net.xdclass.xdclassredis.model.VideoDO;
import net.xdclass.xdclassredis.util.JsonData;
import net.xdclass.xdclassredis.vo.UserPointVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;
import java.util.Set;@RestController
@RequestMapping("api/v1/rank")
public class RankController {@Autowiredprivate RedisTemplate redisTemplate
;private static final String DAILY_RANK_KEY
= "video:rank:daily";@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
);}@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
);}@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
);}@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
);}@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
);}@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
);}}
總結
以上是生活随笔為你收集整理的Redis(案例六:ZSet数据)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。