springboot中redis使用list来缓存数据

[@Anna_1314@]Let bygones be bygones*Love today!
一、应用背景
最近有个项目有这样的背景:从mq订阅了一批数据,收到数据后会将数据按照当前接收到的时间整点(分为单位,因为数据量比较大而且频繁,且从缓存拉取数据的时候也是每分钟拉一次,批量读取分析)缓存到list,键以时间来标记,当然下面的代码并不是实现该背景功能。不多说,下面开始整个代码流程。
二、添加springboot项目redis依赖

<!--在pom.xml中spring-boot-starter-data-redis的依赖,Spring Boot2.x后底层不在是Jedis
        如果做版本升级的朋友需要注意下-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

三、redis的简单配置

# ============redis===========
# 在application.properties文件中配置如下内容,由于Spring Boot2.x的改动,
# 连接池相关配置需要通过spring.redis.lettuce.pool
#或者spring.redis.jedis.pool进行配置了
spring.redis.host=localhost
spring.redis.port=6379
# 一般来说是不用配置的,Spring Cache 会根据依赖的包自行装配---》spring cache指定redis做缓存
spring.cache.type=redis
#spring.redis.password=root #根据需要
# 连接超时时间(毫秒)
spring.redis.timeout=10000
# Redis默认情况下有16个分片,这里配置具体使用的分片,默认是0
spring.redis.database=0
# 连接池最大连接数(使用负值表示没有限制) 默认 8
spring.redis.lettuce.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
spring.redis.lettuce.pool.max-wait=
# 连接池中的最大空闲连接 默认 8
spring.redis.lettuce.pool.max-idle=8
# 连接池中的最小空闲连接 默认 0
spring.redis.lettuce.pool.min-idle=0

四、在代码中增加一个redis的配置类

package com.zsh.config;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisCacheAutoConfiguration {

    @Bean
    public RedisTemplate<String,Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory){
        RedisTemplate<String, Serializable> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

}

代码含义比较简单,故不做过多说明。
五、示例测试代码

@RestController
@RequestMapping("/redis")
public class RedisController {
    @Autowired
    private RedisTemplate<String, Serializable> redisCacheTemplate;

    @GetMapping("/list")
    public List<String> getList(String param){
        String key = "flow:list";//缓存的key,
        String[] params = param.split(",");
        for(String s:params){
            redisCacheTemplate.opsForList().rightPush(key,s);
        }
        List list = redisCacheTemplate.opsForList().range(key,0,-1);
        return list;
    }
}

END!
[@Anna_1314@]Let bygones be bygones*Love today!
Spend life with someone who makes you happy, not someone who you have to impress.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值