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

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: Redis实战--SpringBoot中对Redis数据类型list的基本操作示例
该文章是接上一篇文章《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

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
23天前
|
存储 消息中间件 NoSQL
Redis数据类型详解:选择合适的数据结构优化你的应用
Redis数据类型详解:选择合适的数据结构优化你的应用
|
24天前
|
NoSQL Java Redis
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
275 0
|
1月前
|
XML Java 数据库连接
spring boot 参数的过滤注解与实战
在Spring Boot应用中,对于入参的过滤,通常会涉及到对Web层的数据验证和处理。Spring Boot借助Spring框架提供了强大的验证框架支持,主要基于JSR-303/JSR-380(Bean Validation API)规范,以及Spring自身的@Valid或@Validated注解来实现请求参数的验证。以下是一些常见的使用案例来展示如何对参数进行过滤和验证。
29 1
|
1月前
|
消息中间件 NoSQL Java
springboot redis 实现消息队列
springboot redis 实现消息队列
39 1
|
1天前
|
存储 NoSQL Redis
Redis入门到通关之Redis数据结构-List篇
Redis入门到通关之Redis数据结构-List篇
|
2天前
|
存储 NoSQL Redis
第十八章 Redis查看配置文件和数据类型
第十八章 Redis查看配置文件和数据类型
11 0
|
8天前
|
人工智能 前端开发 Java
Java语言开发的AI智慧导诊系统源码springboot+redis 3D互联网智导诊系统源码
智慧导诊解决盲目就诊问题,减轻分诊工作压力。降低挂错号比例,优化就诊流程,有效提高线上线下医疗机构接诊效率。可通过人体画像选择症状部位,了解对应病症信息和推荐就医科室。
148 10
|
25天前
|
存储 XML NoSQL
Redis支持哪些数据类型?
Redis提供五种数据类型:String(支持JSON、XML等序列化,最大512MB),Hash(键值对,适合存储对象),List(有序列表,可在两端添加元素),Set(无序唯一元素集合),以及Sorted Set(有序集合,元素带分数排序)。每种类型有特定应用场景,优化了数据操作效率。
8 0
|
1月前
|
存储 缓存 安全
Spring Boot从入门到实战
本课程从SpringBoot的最基础的安装、配置开始到SpringBoot的日志管理、Web业务开发、数据存储、数据缓存,安全控制及相关企业级应用,全程案例贯穿,案例每一步的都会讲解实现思路,全程手敲代码实现。让你不仅能够掌SpringBoot的应用,还能了解背后的原理,学习完本课程后,能够让你动手独立完成一个中小型的SpringBoot Web应用开发。
19 1
Spring Boot从入门到实战
|
1月前
|
缓存 NoSQL Java
spring cache整合redis实现springboot项目中的缓存功能
spring cache整合redis实现springboot项目中的缓存功能
46 1