美文网首页
spring-data-redis template配置

spring-data-redis template配置

作者: wangpeng123 | 来源:发表于2018-04-25 17:52 被阅读0次
    @Value("${union.redis.server}")
        private String hostName;
        @Value("${union.redis.port}")
        private int port;
        @Value("${union.redis.password}")
        private String password;
        @Value("${union.redis.database}")
        private int database;
        @Value("${union.redis.maxWaitMillis}")
        private int maxWaitMillis;
        @Value("${union.redis.minEvictableIdleTimeMillis}")
        private int minEvictableIdleTimeMillis;
        @Value("${union.redis.timeBetweenEvictionRunsMillis}")
        private int timeBetweenEvictionRunsMillis;
        @Value("${union.redis.timeout}")
        private int timeout;
        @Value("${union.redis.usePool}")
        private boolean usePool;
        @Value("${union.redis.maxTotal}")
        private int maxTotal;
        @Value("${union.redis.maxIdle}")
        private int maxIdle;
        @Value("${union.redis.minIdle}")
        private int minIdle;
    
    
        @Bean
        public JedisPoolConfig jedisPoolConfig() {
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxTotal(maxTotal);
            jedisPoolConfig.setMaxIdle(maxIdle);
            jedisPoolConfig.setMinIdle(minIdle);
            jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
            jedisPoolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
            jedisPoolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
            return jedisPoolConfig;
        }
    
        @Bean
        public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
            JedisConnectionFactory factory = new JedisConnectionFactory();
            factory.setPoolConfig(jedisPoolConfig);
            factory.setHostName(hostName);
            factory.setPort(port);
            factory.setPassword(password);
            factory.setTimeout(timeout);
            factory.setUsePool(usePool);
            factory.setDatabase(database);
            return factory;
        }
    
    @Bean
        public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory redisConnectionFactory) {
            RedisSerializer<String> stringSerializer = new StringRedisSerializer();
            Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
            ObjectMapper om = new ObjectMapper();
            om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            jackson2JsonRedisSerializer.setObjectMapper(om);
            RedisTemplate<String, Object> template = new RedisTemplate<>();
            template.setConnectionFactory(redisConnectionFactory);
            template.setKeySerializer(stringSerializer);
            template.setValueSerializer(jackson2JsonRedisSerializer);
            template.setHashKeySerializer(stringSerializer);
            template.setHashValueSerializer(jackson2JsonRedisSerializer);
            template.afterPropertiesSet();
            return template;
        }
    

    相关文章

      网友评论

          本文标题:spring-data-redis template配置

          本文链接:https://www.haomeiwen.com/subject/ahbylftx.html