Skip to content

Commit

Permalink
Helper methods to use a Jedis object
Browse files Browse the repository at this point in the history
  • Loading branch information
oscar-besga-panel committed Sep 2, 2023
1 parent d023d2f commit 31f8637
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
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) {
}
}
}

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

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

}
15 changes: 15 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,17 @@ public final Pool<Connection> getPool() {
public Pipeline pipelined() {
return (Pipeline) super.pipelined();
}

void withJedisDo(Consumer<Jedis> consumer) {
try (Connection connection = getPool().getResource()) {
consumer.accept(new Jedis(connection));
}
}

<K> K withJedisGet(Function<Jedis, K> function) {
try (Connection connection = getPool().getResource()) {
return function.apply(new Jedis(connection));
}
}

}

0 comments on commit 31f8637

Please sign in to comment.