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

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Tair(兼容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));
        });
    }

}
相关实践学习
基于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
目录
相关文章
|
3月前
|
NoSQL 安全 测试技术
Redis游戏积分排行榜项目中通义灵码的应用实战
Redis游戏积分排行榜项目中通义灵码的应用实战
89 4
|
3月前
|
Java
springboot将list封装成csv文件
springboot将list封装成csv文件
65 4
|
3月前
|
存储 消息中间件 NoSQL
Redis数据结构:List类型全面解析
Redis数据结构——List类型全面解析:存储多个有序的字符串,列表中每个字符串成为元素 Eelement,最多可以存储 2^32-1 个元素。可对列表两端插入(push)和弹出(pop)、获取指定范围的元素列表等,常见命令。 底层数据结构:3.2版本之前,底层采用**压缩链表ZipList**和**双向链表LinkedList**;3.2版本之后,底层数据结构为**快速链表QuickList** 列表是一种比较灵活的数据结构,可以充当栈、队列、阻塞队列,在实际开发中有很多应用场景。
|
4月前
|
NoSQL 关系型数据库 MySQL
MySQL与Redis协同作战:优化百万数据查询的实战经验
【10月更文挑战第13天】 在处理大规模数据集时,传统的关系型数据库如MySQL可能会遇到性能瓶颈。为了提升数据处理的效率,我们可以结合使用MySQL和Redis,利用两者的优势来优化数据查询。本文将分享一次实战经验,探讨如何通过MySQL与Redis的协同工作来优化百万级数据统计。
165 5
|
4月前
|
自然语言处理 Java API
Spring Boot 接入大模型实战:通义千问赋能智能应用快速构建
【10月更文挑战第23天】在人工智能(AI)技术飞速发展的今天,大模型如通义千问(阿里云推出的生成式对话引擎)等已成为推动智能应用创新的重要力量。然而,对于许多开发者而言,如何高效、便捷地接入这些大模型并构建出功能丰富的智能应用仍是一个挑战。
540 6
|
4月前
|
NoSQL 关系型数据库 MySQL
Redis 列表(List)
10月更文挑战第16天
56 2
|
4月前
|
缓存 NoSQL Java
Spring Boot与Redis:整合与实战
【10月更文挑战第15天】本文介绍了如何在Spring Boot项目中整合Redis,通过一个电商商品推荐系统的案例,详细展示了从添加依赖、配置连接信息到创建配置类的具体步骤。实战部分演示了如何利用Redis缓存提高系统响应速度,减少数据库访问压力,从而提升用户体验。
220 2
|
4月前
|
消息中间件 存储 监控
redis 的List类型 实现 排行榜
【10月更文挑战第8天】
58 2
|
4月前
|
存储 分布式计算 NoSQL
大数据-40 Redis 类型集合 string list set sorted hash 指令列表 执行结果 附截图
大数据-40 Redis 类型集合 string list set sorted hash 指令列表 执行结果 附截图
40 3
|
8月前
|
安全 Java
java线程之List集合并发安全问题及解决方案
java线程之List集合并发安全问题及解决方案
1107 1