日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

Redis(案例六:ZSet数据)

發布時間:2023/12/3 数据库 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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";/*** 返回全部榜單,從大到小* @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);}}

總結

以上是生活随笔為你收集整理的Redis(案例六:ZSet数据)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。