SpringBoot 2.2.0 SpringData Redis使用示例(三)

ZSet 数据类型操作

zset有序集合,

添加数据

 @Test
    public void test() {
        //增加 按照分数的高低排序
        ZSetOperations<String, String> operations = redisTemplate.opsForZSet();
        operations.add("name", "zhang", 89);
        operations.add("name", "wang", 99);
        operations.add("name", "li", 49);
        operations.add("name", "sheng", 89);
 		 //增加分数
  
        operations.incrementScore("name", "zhang", 1);
        //减小分数
        operations.incrementScore("name", "wang", -10);


    }

获取

 @Test
    public void findZsetTest() {

        //查询单个
        ZSetOperations<String, String> operations = redisTemplate.opsForZSet();
        Double score = operations.score("name", "li");
        System.out.println(score);
        //查询排名 结果从零开始
        Long rank = operations.rank("name", "li");
        System.out.println(rank);

        //查询列表 根据排名查询从0到2的人
        Set<String> names = operations.range("name", 0, 2);
        for (String name : names) {
            System.out.println(name);
        }

        //根据排名区间查询
        Set<TypedTuple<String>> name = operations.rangeWithScores("name", 0, 2);
        for (TypedTuple<String> tuple : name) {
            System.out.println(tuple.getValue()+":"+tuple.getScore());
        }

        //更加分数区间查询
        Set<String> name1 = operations.rangeByScore("name", 30, 90);

        for (String s : name1) {
            System.out.println(s);
        }
    }

统计查询

 @Test
    public void countTest() {
        //统计总数
        ZSetOperations<String, String> operations = redisTemplate.opsForZSet();
        Long name = operations.zCard("name");
        System.out.println(name);
        //根据分数区间统计
        Long count = operations.count("name", 60, 90);
        System.out.println(count);
    }

删除

  @Test
    public void deleteTest() {
        ZSetOperations<String, String> operations = redisTemplate.opsForZSet();
        //可变数组 删除li
        operations.remove("name", "li");
        //根据排名区间删除
        operations.removeRange("name", 1, 2);
        //根据分数区间删除
        operations.removeRangeByScore("name", 89, 100);

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值