zoukankan      html  css  js  c++  java
  • 【Spring Boot】 Spring Boot 2.x 版本 CacheManager 配置方式

    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(); }
  • 相关阅读:
    ubuntu上安装nginx+mysql+php5-fpm(PHP5
    创建内存盘
    ubuntu上安装apache2+mysql+php5-fpm(PHP5
    Cubieboard Linaro 搭建超节能监控平台
    CubieBoard开发板数据源介绍
    如何在github创建个人主页?
    android studio配置git
    greenDao使用时遇到的坑
    FragmentManager is already executing transactions
    AndroidSchedulers.mainThread()无法切换到主线程,原来是细节问题啊
  • 原文地址:https://www.cnblogs.com/EveningWind/p/11617271.html
Copyright © 2011-2022 走看看