springboot2的redis缓存管理器cacheManager配置,使存入json格式数据

springboot中默认存入缓存的数据,使用的序列化方案对应的值不方便查看,我们希望存入json格式值,所以要配置cacheManager达到效果

springboot2的缓存管理器配置和springboot1有所差别,所以记录下来:

效果图:

springboot2的redis缓存管理器cacheManager配置,使存入json格式数据_第1张图片

 

cacheManager配置代码:
 //缓存管理器
    @Bean(name = "cacheManager")
    @Primary
    public CacheManager cacheManager(ObjectMapper objectMapper, RedisConnectionFactory redisConnectionFactory) {
        //设置序列化
        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 cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .disableCachingNullValues()
//                .computePrefixWith(cacheName -> "yourAppName".concat(":").concat(cacheName).concat(":"))
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));

        return RedisCacheManager.builder(redisConnectionFactory)
                .cacheDefaults(cacheConfiguration)
                .build();
    }

 

你可能感兴趣的:(springboot2的redis缓存管理器cacheManager配置,使存入json格式数据)