diff --git a/src/main/scala/sedis.scala b/src/main/scala/sedis.scala index 61eed44..c24c71a 100644 --- a/src/main/scala/sedis.scala +++ b/src/main/scala/sedis.scala @@ -95,46 +95,24 @@ trait Dress { } object Dress extends Dress +abstract class AbstractPool { + def using[A <% java.io.Closeable, T](s: A)(f: A => T): T = try f(s) finally s.close() -class Pool(val underlying: JedisPool) { +} - def withClient[T](body: Dress.Wrap => T): T = { - val jedis: Jedis = underlying.getResource - try { - body(Dress.up(jedis)) - } finally { - underlying.returnResourceObject(jedis) - } - } - def withJedisClient[T](body: Jedis => T): T = { - val jedis: Jedis = underlying.getResource - try { - body(jedis) - } finally { - underlying.returnResourceObject(jedis) - } - } +class Pool(val jedisPool: JedisPool) extends AbstractPool { + + def withClient[T](body: Dress.Wrap => T): T = using(Dress.up(jedisPool.getResource))(body) + + def withJedisClient[T](body: Jedis => T): T = using(jedisPool.getResource)(body) } -class SentinelPool(val underlying: JedisSentinelPool) { +class SentinelPool(val jedisSentinelPool: JedisSentinelPool) extends AbstractPool { - def withClient[T](body: Dress.Wrap => T): T = { - val jedis: Jedis = underlying.getResource - try { - body(Dress.up(jedis)) - } finally { - underlying.returnResourceObject(jedis) - } - } - def withJedisClient[T](body: Jedis => T): T = { - val jedis: Jedis = underlying.getResource - try { - body(jedis) - } finally { - underlying.returnResourceObject(jedis) - } - } + def withClient[T](body: Dress.Wrap => T): T = using(Dress.up(jedisSentinelPool.getResource))(body) + + def withJedisClient[T](body: Jedis => T): T = using(jedisSentinelPool.getResource)(body) }