spring boot 简单整合 Redis

复制代码
1.添加依赖

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


2.配置

spring.redis.host=42.192.50.187
spring.redis.port=6379
spring.redis.password=redis@123
spring.redis.database=0
spring.redis.timeout=10000ms
# 连接池 - 最大连接数,默认:8
spring.redis.lettuce.pool.max-active=8
# 连接池 - 最大连接阻塞等待时间,默认:-1
spring.redis.lettuce.pool.max-wait=10000ms
# 连接池 - 最大、最小空闲连接数,最大默认:8,最小默认:0
spring.redis.lettuce.pool.max-idle=200
spring.redis.lettuce.pool.min-idle=5




3.配置类

/**
 * 配置 RedisTemplate 序列化
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

        // String 类型 - 序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        // hash 类型 - 序列化
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        // 注入连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        return redisTemplate;
    }

}

4.使用

# 注入依赖;
    @Autowired
    private RedisTemplate redisTemplate;

存储数据:redisTemplate.opsForValue().set("user:" + ticket, user);
相关推荐
今日说"法"1 小时前
Rust探秘:所有权转移在函数调用中的表现
开发语言·后端·rust
刘一说2 小时前
深入理解 Spring Boot 嵌入式 Web 容器:从原理到性能调优
前端·spring boot·firefox
你的人类朋友2 小时前
设计模式的原则有哪些?
前端·后端·设计模式
程序员小凯2 小时前
Spring Boot文件处理与存储详解
java·spring boot·后端
2501_938774294 小时前
Leaflet 弹出窗实现:Spring Boot 传递省级旅游口号信息的前端展示逻辑
前端·spring boot·旅游
ruleslol5 小时前
SpringBoot13-文件上传02-阿里云OSS
spring boot
武子康6 小时前
大数据-139 ClickHouse MergeTree 最佳实践:Replacing 去重、Summing 求和、分区设计与物化视图替代方案
大数据·后端·nosql
该用户已不存在6 小时前
7个让全栈开发效率起飞的 Bun 工作流
前端·javascript·后端
蹦跑的蜗牛6 小时前
Spring Boot 使用 Redis 实现消息队列
spring boot·1024程序员节
清空mega6 小时前
从零开始搭建 flask 博客实验(2)
后端·python·flask