springboot2.x+redisデータのキャッシュ操作

8004 ワード

springboot2.1.5 + redis + mybatis
demoアドレス
比較springboot 1.xのバージョン、Redisの配置に対してとても大きい変換が発生しました
Redis        
Redis      ,   RedisCacheConfiguration   
    /**
     * 1.  @cacheable      
     * 2.   redis         ,  2  
     * @return
     */
    @Bean
    public RedisCacheConfiguration redisCacheConfiguration(){
        FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
        RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
        configuration = configuration.serializeValuesWith(
                RedisSerializationContext.
                        SerializationPair.
                        fromSerializer(fastJsonRedisSerializer)
        ).entryTtl(Duration.ofMillis(expirationLong));//Duration.ofMillis(expiration)
        return configuration;
    }

    @Bean(name = "redisTemplate")
    @ConditionalOnMissingBean(name = "redisTemplate")
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();

        /** -------------------          ,  redisCacheConfiguration            ,         --------------------- */
        //   
        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
        // value       fastJsonRedisSerializer
        template.setValueSerializer(fastJsonRedisSerializer);
        template.setHashValueSerializer(fastJsonRedisSerializer);
        // key      StringRedisSerializer
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        /** -------------------  json     java  -------------------- */
        //     AutoType,     
         ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
        //         ,        addAccept            , java     ,            
        //  ParserConfig.getGlobalInstance().addAccept("com.jing.modules.dto");
        //  ParserConfig.getGlobalInstance().addAccept("com.jing.modules.dto");
        //  ParserConfig.getGlobalInstance().addAccept("com.jing.modules.dto");
        //  ParserConfig.getGlobalInstance().addAccept("com.jing.modules.dto");
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }