一直苦恼于使用Jedis时要时时牢记关闭资源这个操作, 当Lambda出现后, 这种苦恼终于可以远离而去了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig;
public class Medis {
private final JedisPool jedisPool;
public Medis() { JedisPoolConfig config = new JedisPoolConfig(); jedisPool = new JedisPool(config, "192.168.15.20", 6379, 10000, "xpec@2015"); }
public void consumer(NonResultOperator consumer) { try(Jedis jedis = jedisPool.getResource()) { consumer.accept(jedis); } }
public <U> U accept(ResultOperator consumer) { try(Jedis jedis = jedisPool.getResource()) { return (U) consumer.accept(jedis); } } }
@FunctionalInterface interface ResultOperator { Object accept(redis.clients.jedis.Jedis t); }
@FunctionalInterface interface NonResultOperator { void accept(redis.clients.jedis.Jedis t); }
|
写一个测试类
1 2 3 4 5 6 7 8 9 10 11
| public class MedisTest {
@Test public void test() { Medis medis = new Medis(); medis.consumer(jedis -> jedis.set("123", "456"));
String result = medis.accept(jedis -> jedis.get("123")); System.out.println(result); } }
|
结果为