StringRedisTemplate方法和Redis语法对应关系(List)

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Test
    public void testList() {
        stringRedisTemplate.delete("testList");

        //lpush key node1 node2 ...
        //将一个或多个值插入到列表头部,如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作,当 key 存在但不是列表类型时,返回一个错误
        Long lpushResult = stringRedisTemplate.opsForList().leftPush("testList", "1");
        System.out.println("\tlpush list node1 node2 ...:" + lpushResult);

        System.out.println(stringRedisTemplate.opsForList().range("testList", 0, stringRedisTemplate.opsForList().size("testList") - 1));

        List<String> lpushList = Arrays.asList("2", "3", "4");
        lpushResult = stringRedisTemplate.opsForList().leftPushAll("testList", lpushList);
        System.out.println("\tlpush list node1 node2 ...:" + lpushResult);

        System.out.println(stringRedisTemplate.opsForList().range("testList", 0, stringRedisTemplate.opsForList().size("testList") - 1));

        //rpush key node1 node2 ...
        //将一个或多个值插入到列表的尾部(最右边),如果列表不存在,一个空列表会被创建并执行 RPUSH 操作,当列表存在但不是列表类型时,返回一个错误
        Long rpushResult = stringRedisTemplate.opsForList().rightPush("testList", "5");
        System.out.println("\trpush key node1 node2 ...:" + rpushResult);

        System.out.println(stringRedisTemplate.opsForList().range("testList", 0, stringRedisTemplate.opsForList().size("testList") - 1));

        List<String> rpushList = Arrays.asList("6", "7", "8");
        rpushResult = stringRedisTemplate.opsForList().rightPushAll("testList", rpushList);
        System.out.println("\trpush key node1 node2 ...:" + rpushResult);

        System.out.println(stringRedisTemplate.opsForList().range("testList", 0, stringRedisTemplate.opsForList().size("testList") - 1));

        //lindex key index
        //通过索引获取列表中的元素。可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素
        String lindexResult = stringRedisTemplate.opsForList().index("testList", 0);
        System.out.println("\tlindex key index:" + lindexResult);

        System.out.println(stringRedisTemplate.opsForList().range("testList", 0, stringRedisTemplate.opsForList().size("testList") - 1));

        //llen key
        //返回列表的长度。 如果列表 key 不存在,则 key 被解释为一个空列表,返回 0 。 如果 key 不是列表类型,返回一个错误
        Long llenResult = stringRedisTemplate.opsForList().size("testList");
        System.out.println("\tllen key:" + llenResult);

        System.out.println(stringRedisTemplate.opsForList().range("testList", 0, stringRedisTemplate.opsForList().size("testList") - 1));

        //lpop key
        //移除并返回列表的第一个元素
        String lpopResult = stringRedisTemplate.opsForList().leftPop("testList");
        System.out.println("\tlpop key:" + lpopResult);

        System.out.println(stringRedisTemplate.opsForList().range("testList", 0, stringRedisTemplate.opsForList().size("testList") - 1));

        //rpop key
        //移除列表的最后一个元素,返回值为移除的元素
        String rpopResult = stringRedisTemplate.opsForList().rightPop("testList");
        System.out.println("\trpop key:" + rpopResult);

        System.out.println(stringRedisTemplate.opsForList().range("testList", 0, stringRedisTemplate.opsForList().size("testList") - 1));

        //lpushx key node
        //将一个值插入到已存在的列表头部,列表不存在时操作无效
        Long lpushxResult = stringRedisTemplate.opsForList().leftPushIfPresent("testList", "9");
        System.out.println("\tlpushx key node:" + lpushxResult);

        System.out.println(stringRedisTemplate.opsForList().range("testList", 0, stringRedisTemplate.opsForList().size("testList") - 1));

        //rpushx key node
        //将一个值插入到已存在的列表尾部(最右边),如果列表不存在,操作无效
        Long rpushxResult = stringRedisTemplate.opsForList().rightPushIfPresent("testList", "10");
        System.out.println("\trpushx key node:" + rpushResult);

        System.out.println(stringRedisTemplate.opsForList().range("testList", 0, stringRedisTemplate.opsForList().size("testList") - 1));

        //lrange key start end
        //返回列表中指定区间内的元素,区间以偏移量 START 和 END 指定。 其中 0 表示列表的第一个元素, 1 表示列表的第二个元素,以此类推,也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素
        List<String> lrangeResult = stringRedisTemplate.opsForList().range("testList", 0, 1);
        System.out.println("\tlrange key start end:" + lrangeResult);

        System.out.println(stringRedisTemplate.opsForList().range("testList", 0, stringRedisTemplate.opsForList().size("testList") - 1));

        //lrem key count value
        //根据参数 COUNT 的值,移除列表中与参数 VALUE 相等的元素
        //count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 COUNT
        //count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值
        //count = 0 : 移除表中所有与 VALUE 相等的值
        Long lremResult = stringRedisTemplate.opsForList().remove("testList", 2, "9");
        System.out.println("\tlrem key count value:" + lremResult);

        System.out.println(stringRedisTemplate.opsForList().range("testList", 0, stringRedisTemplate.opsForList().size("testList") - 1));

        //lset key index value
        //通过索引来设置元素的值,当索引参数超出范围,或对一个空列表进行 LSET 时,返回一个错误
        stringRedisTemplate.opsForList().set("testList", 2, "99");
        System.out.println("\tlset key index value:void");

        System.out.println(stringRedisTemplate.opsForList().range("testList", 0, stringRedisTemplate.opsForList().size("testList") - 1));

    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值