springboot中redis使用list来缓存数据

版权声明:本博客可以随便转载,愿意注明出处就注明,不愿意也无所谓,学到就好。 https://blog.csdn.net/yuyongqun/article/details/84196687

[@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.

猜你喜欢

转载自blog.csdn.net/yuyongqun/article/details/84196687
今日推荐