springboot2.0.3 redis配置Jackson2JsonRedisSerializer

按照网上大多数的配置, 最后查看redis里的内容,各种\x0\x0\x00\x000\x0\x0,一看就是JdkSerializationRedisSerializer序列化的,注入Jackson2JsonRedisSerializer的redisTemplate并没有起作用,直到按这篇文章配置才解决。这篇文章中在配置cacheManager的时候把Jackson2JsonRedisSerializer注入进去了。
用Jackson2JsonRedisSerializer,被序列化的类不需要实现Serializable接口,也不会出现使用spring-boot-devtools时反序列化遇到的类型转换异常https://chenzhihao.cc/archives/526。
原文:http://www.morecoder.com/article/1047654.html

Spring Boot Cache + redis配置

一、介绍

  • 环境:SpringBoot2.X + SpringDataRedis
  • Spring boot默认使用的是SimpleCacheConfiguration,使用ConcurrentMapCacheManager来实现缓存。

二、配置redis和cache

1. 引入redis依赖


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

2. 配置文件

spring:
  redis:
      host: 192.168.1.192
      database: 1
      port: 6379
      password: 123456
      timeout: 1s
      jedis:
        pool:
          max-active: 20
          max-idle: 20
          min-idle: 10
          max-wait: -1ms
  cache:
    redis:
      use-key-prefix: true
      key-prefix: dev
      cache-null-values: false
      time-to-live: 20s

3. 配置RedisCacheManager

  • 解决redis保存数据乱码的问题
  • 解决从redis反序列化报错的问题
  • 增加失效时间
@Configuration
@ConfigurationProperties(prefix = "spring.cache.redis")
public class SpringCacheRedisConfig {
    private Duration timeToLive = Duration.ZERO;
    public void setTimeToLive(Duration timeToLive) {
        this.timeToLive = timeToLive;
    }

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        RedisSerializer 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);

        // 配置序列化(解决乱码的问题)
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(timeToLive)
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
                .disableCachingNullValues();

        RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
                .cacheDefaults(config)
                .build();
        return cacheManager;
    }
}

二、代码中使用缓存

1. 开启缓存

@EnableCaching
@SpringBootApplication
public class StatisticsApplication {

    public static void main(String[] args) {
        SpringApplication.run(StatisticsApplication.class, args);
    }
}

2. 在方法上使用@Cacheable注解

@Cacheable(value = "userStatistics", key = "methodName")
@Override
public List getListByPage2(UserStatisticsReqVo userStatisticsReqVo) {
   Page userStatisticsPage = userStatisticsMapper.selectListByPage(userStatisticsReqVo);
   return userStatisticsPage.getResult();
}

四、测试

分享图片

你可能感兴趣的:(springboot2.0.3 redis配置Jackson2JsonRedisSerializer)