spring boot集成redis

引入依赖


xml 复制代码
		<!-- 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>

application.yml配置文件


yml 复制代码
server:
  port: 8456
spring:
  data:
    redis:
      host: 127.0.0.1
      password: 123456
      port: 6379
      database: 2
      lettuce:
        pool:
          max-active: 8   # 最大连接
          max-idle: 8     # 最大空闲连接
          min-idle: 0     # 最小空闲连接
          max-wait: 100ms   # 等待时间

序列化配置类


java 复制代码
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

}

常用方法


相关推荐
爬山算法2 小时前
Redis(158)Redis的主从同步问题如何解决?
数据库·redis·缓存
小马爱打代码4 小时前
Spring Boot:模块化实战 - 保持清晰架构
java·spring boot·架构
8***Z895 小时前
springboot 异步操作
java·spring boot·mybatis
i***13245 小时前
Spring BOOT 启动参数
java·spring boot·后端
IT_Octopus5 小时前
(旧)Spring Securit 实现JWT token认证(多平台登录&部分鉴权)
java·后端·spring
kk哥88995 小时前
Spring详解
java·后端·spring
S***26755 小时前
Spring Cloud Gateway 整合Spring Security
java·后端·spring
码事漫谈5 小时前
C++单元测试框架选型与实战速查手册
后端
OneLIMS5 小时前
Windows Server 2022 + IIS + ASP.NET Core 完整可上传大文件的 报错的问题
windows·后端·asp.net
码事漫谈5 小时前
C++ 依赖管理三剑客:vcpkg、Conan、xmake 速查手册
后端