zoukankan      html  css  js  c++  java
  • 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 }
     
  • 相关阅读:
    8.22
    webstrom安装流程
    8.21
    8.20
    8.20学习笔记
    使用WebClient异步获取http资源
    导航栏,可直接使用
    asp.net mvc5实现单点登录
    使用C#调用Word的接口生成doc文件与html文件
    下载网页并保存
  • 原文地址:https://www.cnblogs.com/hcklqy/p/11355687.html
Copyright © 2011-2022 走看看