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

2020-12-13 16:00

阅读:340

标签:失效   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 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 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

上一篇:Java 读写文件示例

下一篇:java优先队列


评论


亲,登录后才可以留言!