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;
    }

    @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);
    }
}

加入测试数据

@SpringBootTest
class XdclassRedisApplicationTests {

	@Autowired
	private RedisTemplate redisTemplate;

	@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就是第⼀
给某个⽤户加积分
查看某个⽤户的积分

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 {

    @Autowired
    private 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)