Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helper methods to use a Jedis object #3520

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 withJedisDo(Consumer<Jedis> consumer) {
try (Jedis jedis = getResource()) {
consumer.accept(jedis);
}
}

public <K> K withJedisGet(Function<Jedis, K> function) {
try (Jedis jedis = getResource()) {
return function.apply(jedis);
}
}

}
19 changes: 19 additions & 0 deletions src/main/java/redis/clients/jedis/JedisPooled.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 @@ -401,4 +403,21 @@ public final Pool<Connection> getPool() {
public Pipeline pipelined() {
return (Pipeline) super.pipelined();
}

public Jedis newJedis() {
return new Jedis(getPool().getResource());
Copy link
Collaborator

Choose a reason for hiding this comment

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

This bears the similar issue stated in #3533 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I didn't know this was there.
I'll close this PR and make one that limits helpers to JedisPool
I think now that JedisPooled doesn't need helpers like I've done

Thanks

}

public void withJedisDo(Consumer<Jedis> consumer) {
try (Jedis jedis = newJedis()) {
consumer.accept(jedis);
}
}

public <K> K withJedisGet(Function<Jedis, K> function) {
try (Jedis jedis = newJedis()) {
return function.apply(jedis);
}
}

}
30 changes: 30 additions & 0 deletions src/test/java/redis/clients/jedis/JedisPoolTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package redis.clients.jedis;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
Expand All @@ -8,6 +11,7 @@

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
Expand Down Expand Up @@ -453,4 +457,30 @@ public void testResetValidCredentials() {
}
}
}

@Test
public void testWithJedisDo() {
try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort())) {
pool.withJedisDo(jedis -> {
jedis.auth("foobared");
assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hnp.getHost()));
assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hnp.getPort()));
assertThat(jedis.time(), notNullValue());
});
}
}

@Test
public void testWithJedisGet() {
try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort())) {
List<String> result = pool.withJedisGet(jedis -> {
jedis.auth("foobared");
assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hnp.getHost()));
assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hnp.getPort()));
return jedis.time();
});
assertThat(result,notNullValue());
}
}

}
39 changes: 36 additions & 3 deletions src/test/java/redis/clients/jedis/JedisPooledTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package redis.clients.jedis;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
Expand Down Expand Up @@ -255,4 +254,38 @@ public void cleanUp() {
assertThat(cleanupCount.get(), equalTo(1));
}
}

@Test
public void testNewJedis() {
try (JedisPooled pool = new JedisPooled(hnp)) {
Jedis jedis = pool.newJedis();
assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hnp.getHost()));
assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hnp.getPort()));
assertThat(jedis.time(), notNullValue());
}
}

@Test
public void testWithJedisDo() {
try (JedisPooled pool = new JedisPooled(hnp)) {
pool.withJedisDo(jedis -> {
assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hnp.getHost()));
assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hnp.getPort()));
assertThat(jedis.time(), notNullValue());
});
}
}

@Test
public void testWithJedisGet() {
try (JedisPooled pool = new JedisPooled(hnp)) {
List<String> result = pool.withJedisGet(jedis -> {
assertThat(jedis.getClient().getHostAndPort().getHost(), equalTo(hnp.getHost()));
assertThat(jedis.getClient().getHostAndPort().getPort(), equalTo(hnp.getPort()));
return jedis.time();
});
assertThat(result,notNullValue());
}
}

}
Loading