SpringBoot集成Redis

依赖包:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

配置文件

如果是 properties 文件,使用:

spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.database=0
##连接超时时间(毫秒)
spring.redis.timeout=1800000
spring.redis.password=123456
##连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.lettuce.pool.max-wait=-1
##连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=8
##连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=0
##连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=20

如果用的是 yml文件,则是:

spring:
    redis:
        database: 0
        host: 127.0.0.1
        lettuce:
            pool:
                max-active: 20
                max-idle: 8
                max-wait: -1
                min-idle: 0
        password: 123456
        port: 6379
        timeout: 180000

Redis配置:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        // 创建模板
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        // 设置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 设置序列化工具
        GenericJackson2JsonRedisSerializer jsonRedisSerializer =
                new GenericJackson2JsonRedisSerializer();
        // key和 hashKey采用 string序列化
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        // value和 hashValue采用 JSON序列化
        redisTemplate.setValueSerializer(jsonRedisSerializer);
        redisTemplate.setHashValueSerializer(jsonRedisSerializer);
        return redisTemplate;
    }
}

代码示例


@Service
public class RedisTestServiceImpl {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 缓存的前缀
     */
    private static final String PREFIX = "PREFIX_";


    public String getData(String id) {
        String key = PREFIX + id;
        String data = redisTemplate.opsForValue().get(key);
        if (StringUtils.isNotBlank(data)) {
            return data;
        }
        //实际中一般是从数据库中查询,有数据就插入缓存。
        String value = "test";
        //缓存5分钟
        redisTemplate.opsForValue().set(key, value, 5L, TimeUnit.MINUTES);
        return value;
    }


}

参考资料:

https://blog.csdn.net/weixin_59654772/article/details/125692784

posted on   乐之者v  阅读(58)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-05-31 Java8 CompletableFuture处理多个异步任务
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示