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

【Spring Boot】 Spring Boot 2.x 版本 CacheManager 配置方式

时间:2019-10-02 15:04:45      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:失效   shm   enabled   any   hash   LEDE   nbsp   rod   rom   

Spring Boot 1.X RedisCacheManager 配置方式

@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
    RedisCacheManager cacheManager= new RedisCacheManager(redisTemplate);
    cacheManager.setDefaultExpiration(60);
    Map<String,Long> expiresMap=new HashMap<>();
    expiresMap.put("Product",5L);
    cacheManager.setExpires(expiresMap);
    return cacheManager;
}

Spring Boot 2.X RedisCacheManager 配置方式

  /**
     * SpringBoot 2.X 版本配置方式
     *
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public CacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) {
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        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);
        CacheKeyPrefix keyPrefix = new CacheKeyPrefix() {
            @Override
            public String compute(String cacheName) {
                return cacheName + "::";
            }
        };
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
          //失效时间 .entryTtl(Duration.ofMinutes(
3)) .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) .disableCachingNullValues(); return RedisCacheManager.builder(redisConnectionFactory) .cacheDefaults(config) .build(); }

 

【Spring Boot】 Spring Boot 2.x 版本 CacheManager 配置方式

标签:失效   shm   enabled   any   hash   LEDE   nbsp   rod   rom   

原文地址:https://www.cnblogs.com/EveningWind/p/11617271.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!