SpringBoot2.0的CacheManager配置

Redis习惯使用 Json格式来存储了,spring-data-redis 2.0 开始网上找的方法已经都不适用了,文档里也没说清楚,通过分析源码最后解决。

这里简单介绍一下我的解决方法

1、pom依赖

 


    org.springframework.boot
    spring-boot-starter-cache


    org.springframework.boot
    spring-boot-starter-data-redis


    org.apache.commons
    commons-pool2

2、配置yml

 

spring:
  cache:
    redis:
      use-key-prefix: true
      time-to-live: 1d
  redis:
    port: 6379
    host: ip
    password: pwd
    database: 0
    timeout: 1s
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 0
        max-wait: -1ms
 
3、自定义CacheConfiguration 类

   

import lombok.extern.log4j.Log4j2;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
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.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
 
import java.time.Duration;
 
@Log4j2
@Configuration
@EnableCaching
@ConfigurationProperties(prefix = "spring.cache.redis")
public class CacheConfiguration {
    private Duration timeToLive = Duration.ZERO;
    public void setTimeToLive(Duration timeToLive) {
        this.timeToLive = timeToLive;
    }
 
    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(this.timeToLive)
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer()))
                .disableCachingNullValues();
 
        RedisCacheManager redisCacheManager = RedisCacheManager.builder(connectionFactory)
                .cacheDefaults(config)
                .transactionAware()
                .build();
 
        log.debug("自定义RedisCacheManager加载完成");
        return redisCacheManager;
    }
 
    @Bean(name = "redisTemplate")
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
 
        redisTemplate.setKeySerializer(keySerializer());
        redisTemplate.setHashKeySerializer(keySerializer());
        redisTemplate.setValueSerializer(valueSerializer());
        redisTemplate.setHashValueSerializer(valueSerializer());
 
        log.debug("自定义RedisTemplate加载完成");
        return redisTemplate;
    }
 
    private RedisSerializer keySerializer() {
        return new StringRedisSerializer();
    }
 
    private RedisSerializer valueSerializer() {
        return new GenericJackson2JsonRedisSerializer();
    }
 
}

---------------------
作者:卜居老毛
来源:CSDN
原文:https://blog.csdn.net/maoxinrong/article/details/80777598
版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(SpringBoot2.0的CacheManager配置)