Created
September 27, 2016 04:05
-
-
Save yayj/854f21cfa318e36eddb3ab07fc6855dd to your computer and use it in GitHub Desktop.
Jedis with ARM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CloseableJedis implements AutoCloseable { | |
private final Jedis delegate; | |
private final JedisPool pool; | |
private boolean broken = false; | |
public CloseableJedis(JedisPool pool) { | |
this.pool = pool; | |
delegate = pool.getResource(); | |
} | |
public void setBroken() { | |
broken = true; | |
} | |
public Jedis get() { | |
return delegate; | |
} | |
@Override | |
public void close() throws JedisException { | |
if (broken) { | |
pool.returnBrokenResource(delegate); | |
} else { | |
pool.returnResource(delegate); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Utils { | |
public static <T> T withJedis(JedisPool pool, final Function<Jedis, T> func) { | |
try (CloseableJedis connection = new CloseableJedis(pool)) { | |
return new Function<CloseableJedis, T>() { | |
@Override | |
public T apply(CloseableJedis input) { | |
try { | |
return func.apply(input.get()); | |
} catch (JedisConnectionException e) { | |
input.setBroken(); | |
throw e; | |
} | |
} | |
}.apply(connection); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment