package com.inkasso.config.redis;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Description:Redis参数配置
 * @Author:JingJiang
 * @E-mail:254702920@qq.com
 * @Date:2017年6月6日 上午10:27:44
 */
@Configuration
public class RedisConfig {

    @Value("${redis.maxTotal}")
    private int maxTotal;
    @Value("${redis.maxIdle}")
    private int maxIdle;
    @Value("${redis.numTestsPerEvictionRun}")
    private int numTestsPerEvictionRun;
    @Value("${redis.softMinEvictableIdleTimeMillis}")
    private long softMinEvictableIdleTimeMillis;
    @Value("${redis.minEvictableIdleTimeMillis}")
    private long minEvictableIdleTimeMillis;
    @Value("${redis.timeBetweenEvictionRunsMillis}")
    private long timeBetweenEvictionRunsMillis;
    @Value("${redis.maxWaitMillis}")
    private long maxWaitMillis;
    @Value("${redis.testOnBorrow}")
    private boolean testOnBorrow;
    @Value("${redis.testWhileIdle}")
    private boolean testWhileIdle;
    @Value("${redis.url}")
    private String url;
    @Value("${redis.port}")
    private int port;
    @Value("${redis.indexDB}")
    private int indexDB;

    @Bean(name = "jedisPool")
    public JedisPool jedisPool() {
        JedisPoolConfig jpc = new JedisPoolConfig();
        jpc.setMaxTotal(maxTotal);
        jpc.setMaxIdle(maxIdle);
        jpc.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
        jpc.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);
        jpc.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        jpc.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        jpc.setMaxWaitMillis(maxWaitMillis);
        jpc.setTestOnBorrow(testOnBorrow);
        jpc.setTestWhileIdle(testWhileIdle);
        JedisPool jp = new JedisPool(jpc, url, port);
        Jedis jedis = jp.getResource();
        jedis.select(indexDB);
        jedis.flushDB();
        jedis.close();
        return jp;
    }


    @Bean
    public RedisSerializer fastJson2JsonRedisSerializer() {
        return new FastJson2JsonRedisSerializer(Object.class);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory,
        RedisSerializer fastJson2JsonRedisSerializer) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(fastJson2JsonRedisSerializer);
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(fastJson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

}
