美文网首页
SpringBoot2.0整合Redis

SpringBoot2.0整合Redis

作者: 帅气的名称被占用 | 来源:发表于2019-04-11 18:39 被阅读0次

    依赖:
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    springboot2.0的redis整合包多出lettuce连接池,需要commons-pool2,所以项目pom依赖要添加commons-pool2


    <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.6.0</version>
    </dependency>
    1、配置类
    @Configuration
    public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
    
        RedisTemplate<String, Object> template = new RedisTemplate<>();
    
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
    
        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);
    
        template.setConnectionFactory(factory);
        //key序列化方式
        template.setKeySerializer(redisSerializer);
        //value序列化
        template.setValueSerializer(jackson2JsonRedisSerializer);
        //value hashmap序列化
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
    
        return template;
    }
    
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        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);
    
        // 配置序列化(解决乱码的问题),过期时间30秒
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(30))
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
                .disableCachingNullValues();
    
        RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
                .cacheDefaults(config)
                .build();
        return cacheManager;
    }
    

    }
    2、yml配置
    spring:
    redis:
    database: 0
    host: 127.0.0.1
    jedis:
    pool:
    #最大连接数据库连接数,设 0 为没有限制
    max-active: 8
    #最大等待连接中的数量,设 0 为没有限制
    max-idle: 8
    #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
    max-wait: -1ms
    #最小等待连接中的数量,设 0 为没有限制
    min-idle: 0
    lettuce:
    pool:
    max-active: 8
    max-idle: 8
    max-wait: -1ms
    min-idle: 0
    shutdown-timeout: 100ms
    password: ''
    port: 6379
    3、使用
    1)StringRedisTemplate,存储字符串类型
    //控制层调用
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    2)、RedisTemplate, 存储对象类型
    //控制层调用
    @Autowired
    private RedisTemplate redisTemplate;

    相关文章

      网友评论

          本文标题:SpringBoot2.0整合Redis

          本文链接:https://www.haomeiwen.com/subject/zupxwqtx.html