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


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

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

    效果图:

    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();
        }
  • 相关阅读:
    猫眼 top_100 爬取 ___只完成了第一页
    内涵段子——脑筋急转弯——spider
    django 常用 详解
    获取定位信息
    对AFNetworking的二次封装
    对UIImageView+WebCache的封装
    CoreText 实现图文混排
    剖析RAC中的@weakify、@strongify
    实现点击按钮旋转动画
    java面向对象编程思想的理解
  • 原文地址:https://www.cnblogs.com/javayuan/p/9372418.html
Copyright © 2020-2023  润新知