redis连接util scala代码
object RedisUtil {
var jedisPool: JedisPool = _
def main(args: Array[String]): Unit = {
val jedisConn = getJedisClient
jedisConn.set("hello", "world")
closeJedisClient
}
def getJedisClient: Jedis = {
if (jedisPool == null) {
// println("开辟一个连接池")
val config = PropertiesUtil.load("config.properties")
val host = config.getProperty("redis.host")
val port = config.getProperty("redis.port")
val passwd = if (config.getProperty("redis.passwd") == "null") null else config.getProperty("redis.passwd")
val jedisPoolConfig = new JedisPoolConfig()
jedisPoolConfig.setMaxTotal(20) //最大连接数
jedisPoolConfig.setMaxIdle(7) //最大空闲
jedisPoolConfig.setMinIdle(7) //最小空闲
jedisPoolConfig.setBlockWhenExhausted(true) //忙碌时是否等待
jedisPoolConfig.setMaxWaitMillis(5000 * 3) //忙碌时等待时长 毫秒
jedisPoolConfig.setTestOnBorrow(true) //每次获得连接的进行测试
jedisPool = new JedisPool(jedisPoolConfig, host, port.toInt, 10 * Protocol.DEFAULT_TIMEOUT, passwd)
}
jedisPool.getResource
}
def closeJedisClient = {
print("--------关闭redis连接--------")
jedisPool.close()
}
}