Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/redis/clients/jedis/JedisPool.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package redis.clients.jedis;

import java.net.URI;
import java.util.function.Consumer;
import java.util.function.Function;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocketFactory;
Expand Down Expand Up @@ -392,4 +394,17 @@ public void returnResource(final Jedis resource) {
}
}
}

public void withJedisPoolDo(Consumer<Jedis> consumer) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest :
Rename withJedisPoolDo → withResource

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is if the two methods are renamed equally this won't compile

        withResource(jedis -> jedis.set("c"));
        String x = withResource(jedis -> jedis.get("c")); 

as it will trigger ambiguous method call in both lines.
Even if I like your idea, Java won't allow
(if you use a full body lambda it will make it, but that will reducefuture uses of the methods)

I'll suggest withResource and withResourceGet

try (Jedis jedis = this.getResource()) {
consumer.accept(jedis);
}
}

public <K> K withJedisPoolGet(Function<Jedis, K> function) {
try (Jedis jedis = this.getResource()) {
return function.apply(jedis);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest :
Rename withJedisPoolGet → withResource

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See bellow

}
}

}
26 changes: 26 additions & 0 deletions src/test/java/redis/clients/jedis/JedisPoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,30 @@ public void testResetValidCredentials() {
}
}
}

@Test
public void testWithJedisPoolDoWithConnection() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000);
pool.withJedisPoolDo(jedis -> {
jedis.auth(endpointStandalone0.getPassword());
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
});
pool.close();
assertTrue(pool.isClosed());
}

@Test
public void testWithJedisPoolGetWithConnection() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), endpointStandalone0.getHost(), endpointStandalone0.getPort(), 2000);
String result = pool.withJedisPoolGet(jedis -> {
jedis.auth(endpointStandalone0.getPassword());
jedis.set("foo", "bar");
return jedis.get("foo");
});
assertEquals("bar", result);
pool.close();
assertTrue(pool.isClosed());
}

}
Loading