• 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 }
     
  • 相关阅读:
    python学习笔记之--read、readline和readlines
    目录操作习题
    递归习题
    文件操作练习题
    HandleBase句柄的5种写法
    ContextBase
    BasegoSort
    PrototypePra原型_设计订单保存
    DesignPattenTemplate模板模式
    DesignPattenStrategy策略模式
  • 原文地址:https://www.cnblogs.com/hcklqy/p/11355687.html
Copyright © 2020-2023  润新知