From 31f86375d29d1b04c291af6e2040b54f8d33fe47 Mon Sep 17 00:00:00 2001 From: oscar-besga-panel Date: Sat, 2 Sep 2023 15:51:50 +0200 Subject: [PATCH] Helper methods to use a Jedis object --- src/main/java/redis/clients/jedis/JedisPool.java | 15 +++++++++++++++ .../java/redis/clients/jedis/JedisPooled.java | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/main/java/redis/clients/jedis/JedisPool.java b/src/main/java/redis/clients/jedis/JedisPool.java index 3cc9aad107..e7fcbc09d8 100644 --- a/src/main/java/redis/clients/jedis/JedisPool.java +++ b/src/main/java/redis/clients/jedis/JedisPool.java @@ -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; @@ -392,4 +394,17 @@ public void returnResource(final Jedis resource) { } } } + + void withJedisDo(Consumer consumer) { + try (Jedis jedis = getResource()) { + consumer.accept(jedis); + } + } + + K withJedisGet(Function function) { + try (Jedis jedis = getResource()) { + return function.apply(jedis); + } + } + } diff --git a/src/main/java/redis/clients/jedis/JedisPooled.java b/src/main/java/redis/clients/jedis/JedisPooled.java index bc751527ca..e77a498728 100644 --- a/src/main/java/redis/clients/jedis/JedisPooled.java +++ b/src/main/java/redis/clients/jedis/JedisPooled.java @@ -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; @@ -401,4 +403,17 @@ public final Pool getPool() { public Pipeline pipelined() { return (Pipeline) super.pipelined(); } + + void withJedisDo(Consumer consumer) { + try (Connection connection = getPool().getResource()) { + consumer.accept(new Jedis(connection)); + } + } + + K withJedisGet(Function function) { + try (Connection connection = getPool().getResource()) { + return function.apply(new Jedis(connection)); + } + } + }