码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot2.X 自定义Redis的cacheManager,保存Json格式到Redis

时间:2019-08-15 05:49:47      阅读:21      评论:0      收藏:0      [点我收藏+]

标签:问题   yacc   map   turn   解决   article   asa   factor   disconf   

部分内容转自 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

标签:问题   yacc   map   turn   解决   article   asa   factor   disconf   

原文地址:https://www.cnblogs.com/hcklqy/p/11355687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!