RedisConfig.scala 2.3 KB
Newer Older
梦境迷离's avatar
重构  
梦境迷离 已提交
1
package io.github.dreamylost.config
梦境迷离's avatar
梦境迷离 已提交
2

梦境迷离's avatar
梦境迷离 已提交
3 4
import org.bitlap.tools.log
import org.bitlap.tools.logs.LogType
梦境迷离's avatar
梦境迷离 已提交
5
import org.springframework.beans.factory.annotation.Value
梦境迷离's avatar
梦境迷离 已提交
6 7
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
梦境迷离's avatar
梦境迷离 已提交
8 9 10
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory
import redis.clients.jedis.JedisPoolConfig

梦境迷离's avatar
重构  
梦境迷离 已提交
11
/** redis连接配置
梦境迷离's avatar
梦境迷离 已提交
12
  *
梦境迷离's avatar
梦境迷离 已提交
13
  * @since 2018年9月8日
梦境迷离's avatar
梦境迷离 已提交
14 15
  * @author 梦境迷离
  */
梦境迷离's avatar
梦境迷离 已提交
16
@Configuration
17
@log(logType = LogType.Slf4j)
梦境迷离's avatar
梦境迷离 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
class RedisConfig {

  //主机地址
  @Value("${spring.redis.host}")
  private var host: String = _
  //端口
  @Value("${spring.redis.port}")
  private var port: Int = _
  //允许超时
  @Value("${spring.redis.timeout}")
  private var timeout: Int = _
  //认证
  @Value("${spring.redis.password}")
  private var password: String = _
  //数据库索引数量
  @Value("${spring.redis.database}")
  private var database: Int = _
  //连接池中的最大空闲连接
  @Value("${spring.redis.pool.max-idle}")
  private var maxIdle: Int = _
  //连接池中的最小空闲连接
  @Value("${spring.redis.pool.min-idle}")
  private var minIdle: Int = _
  //连接池最大连接数(使用负值表示没有限制)
  @Value("${spring.redis.pool.max-active}")
  private var maxActive: Int = _
  //连接池最大阻塞等待时间(使用负值表示没有限制)
  @Value("${spring.redis.pool.max-wait}")
  private var maxWait: Int = _

梦境迷离's avatar
重构  
梦境迷离 已提交
48
  /** Jedis数据源配置
梦境迷离's avatar
梦境迷离 已提交
49
    *
梦境迷离's avatar
重构  
梦境迷离 已提交
50
    * @return JedisPoolConfig
梦境迷离's avatar
梦境迷离 已提交
51
    */
梦境迷离's avatar
梦境迷离 已提交
52 53 54 55 56 57
  @Bean
  def jedisPoolConfig(): JedisPoolConfig = {
    val jedisPoolConfig = new JedisPoolConfig
    jedisPoolConfig.setMaxIdle(maxIdle)
    jedisPoolConfig.setMinIdle(minIdle)
    jedisPoolConfig.setMaxWaitMillis(maxWait)
58
    log.info("Init the RedisPoolConfig Finished")
梦境迷离's avatar
梦境迷离 已提交
59 60 61
    jedisPoolConfig
  }

梦境迷离's avatar
重构  
梦境迷离 已提交
62
  /** Jedis数据连接工厂
梦境迷离's avatar
梦境迷离 已提交
63
    *
梦境迷离's avatar
重构  
梦境迷离 已提交
64
    * @return JedisConnectionFactory
梦境迷离's avatar
梦境迷离 已提交
65
    */
梦境迷离's avatar
梦境迷离 已提交
66 67 68 69 70 71 72 73 74
  @Bean
  def redisConnectionFactory(poolConfig: JedisPoolConfig): JedisConnectionFactory = {
    val factory: JedisConnectionFactory = new JedisConnectionFactory
    factory.setHostName(host)
    factory.setPort(port)
    factory.setTimeout(timeout)
    factory.setPassword(password)
    factory.setDatabase(database)
    factory.setPoolConfig(poolConfig)
75
    log.info("Init the Redis instance Finished")
梦境迷离's avatar
梦境迷离 已提交
76 77 78
    factory
  }

梦境迷离's avatar
梦境迷离 已提交
79
}