Spring Boot2.X 自定义Redis的cacheManager,保存Json格式到Redis
Posted hcklqy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot2.X 自定义Redis的cacheManager,保存Json格式到Redis相关的知识,希望对你有一定的参考价值。
部分内容转自 https://blog.csdn.net/caojidasabi/article/details/83059642
配置好自定义RedisCacheManager 组件,就可以自动将要保存的数据以JSON格式存入Redis
1 @Configuration 2 public class MyRedisConfig 3 4 @Bean 5 public RedisCacheManager cacheManager(RedisConnectionFactory factory) 6 7 RedisSerializer<String> redisSerializer = new StringRedisSerializer(); 8 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); 9 10 //解决查询缓存转换异常的问题 11 ObjectMapper om = new ObjectMapper(); 12 om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 13 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 14 jackson2JsonRedisSerializer.setObjectMapper(om); 15 16 //配置序列化(解决乱码的问题) 17 RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() 18 .entryTtl(Duration.ZERO) 19 .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) 20 .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) 21 .disableCachingNullValues(); 22 23 RedisCacheManager cacheManager = RedisCacheManager.builder(factory) 24 .cacheDefaults(config) 25 .build(); 26 return cacheManager; 27 28
以上是关于Spring Boot2.X 自定义Redis的cacheManager,保存Json格式到Redis的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot2.x 的Druid连接池配置[附带监控]
Spring Boot2.x + Quartz 定时任务模块
spring cloudspring boot2.x下 使用feign,注解@EnableFeignClients 找不到的解决方法