Redis实战--SpringBoot中对Redis数据类型list的基本操作示例

echo编辑整理,欢迎转载,转载请声明文章来源。欢迎添加echo微信(微信号:t2421499075)交流学习。 百战不败,依不自称常胜,百败不颓,依能奋力前行。——这才是真正的堪称强大!!!


该文章是接上一篇文章《Redis整合SpringBoot示例》的后续,操作用例代码比较多,这里展示核心代码所占篇幅很多,所以单独抽出来写

list类型在SpringBoot中的使用代码如下

package com.example.echo.redis;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

/**
 * @author XLecho
 * Date 2019/11/9 0009
 * Time 11:12
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTypeListUseTest {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 创建一个队列,并从队列的左边插入数据a
     */
    @Test
    public void testLpushList() {
        redisTemplate.opsForList().leftPush("queue", "a");
        System.out.println(redisTemplate.opsForList().range("queue", 0, -1));
    }

    /**
     * 从队列的左边插入数据b c
     */
    @Test
    public void testLpushAllList() {
        redisTemplate.opsForList().leftPushAll("queue", "b", "c");
        System.out.println(redisTemplate.opsForList().range("queue", 0, -1));
    }

    /**
     * 从队列的右边插入数据d e
     */
    @Test
    public void testRpushAllList() {
        redisTemplate.opsForList().rightPushAll("queue", "d", "e");
        System.out.println(redisTemplate.opsForList().range("queue", 0, -1));
    }

    /**
     * 从队列的左边弹出一个元素
     */
    @Test
    public void testLpopList() {
        redisTemplate.opsForList().leftPop("queue");
        System.out.println(redisTemplate.opsForList().range("queue", 0, -1));
    }

    /**
     * 从队列的左边弹出一个元素
     */
    @Test
    public void testRpopList() {
        redisTemplate.opsForList().leftPop("queue");
        System.out.println(redisTemplate.opsForList().range("queue", 0, -1));
    }

    /**
     * 获取队列指定索引的元素
     */
    @Test
    public void testLindexList() {
        String queue = redisTemplate.opsForList().index("queue", 2);
        System.out.println(queue);
        System.out.println(redisTemplate.opsForList().range("queue", 0, -1));
    }

    /**
     * 截取队列指定索引范围内的值
     * 注意:没有被截取就会被舍弃
     */
    @Test
    public void testLtrimList() {
        redisTemplate.opsForList().trim("queue", 0, 1);
        System.out.println(redisTemplate.opsForList().range("queue", 0, -1));
    }

    /**
     * 查看队列的长度
     */
    @Test
    public void testLlenList() {
        System.out.println(redisTemplate.opsForList().size("queue"));
    }

    /**
     * 删除队列
     */
    @Test
    public void testDelList() {
        redisTemplate.delete("queue");
        System.out.println(redisTemplate.opsForList().range("queue", 0, -1));
    }

    /**
     * 使用Brpop实现发布订阅
     */
    @Test
    public void testBrpopList() {
        redisTemplate.opsForList().leftPushAll("queue", "a", "b", "c", "d", "e");
        IntStream.range(0, 6).forEach(it -> {
            System.out.println(redisTemplate.opsForList().rightPop("queue", 60, TimeUnit.SECONDS));
        });
    }

}

项目源码地址:https://coding.net/u/xlsorry/p/SpringBootRedis/git

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xlecho

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值