package com.fkzy.warn.common.conf;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Redis 工具类
 *
 * @author yuyantian
 * @date 2021/9/7 14:30
 */
@Configuration
@EnableCaching
@Component
public class RedisConfig extends CachingConfigurerSupport {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.password}")
    private String password;


    @Value("${spring.redis.pool.timeout}")
    private int timeout;

    @Value("${spring.redis.pool.max-total}")
    private int maxTotal;

    @Value("${spring.redis.pool.max-idle}")
    private int maxIdle;

    @Value("${spring.redis.pool.max-wait}")
    private long maxWaitMillis;

    @Bean
    public JedisPool redisPoolFactory() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(maxTotal);
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        jedisPoolConfig.setTestOnBorrow(true);//使用时进行扫描，确保都可用
        jedisPoolConfig.setTestWhileIdle(true);//Idle时进行连接扫描
        jedisPoolConfig.setTestOnReturn(true);//还回线程池时进行扫描
        jedisPoolConfig.setMinEvictableIdleTimeMillis(60000 * 30);
        jedisPoolConfig.setTimeBetweenEvictionRunsMillis(60000 * 30);
        jedisPoolConfig.setNumTestsPerEvictionRun(-1);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
        return jedisPool;
    }


}

