From 99a44915511ed418eadf2382de43bfabe4b02ac0 Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Sun, 4 Feb 2024 17:09:07 +0600 Subject: [PATCH] Support Client-side caching through URI/URL --- .../java/redis/clients/jedis/JedisPooled.java | 2 +- .../redis/clients/jedis/UnifiedJedis.java | 8 +- .../clients/jedis/util/JedisURIHelper.java | 78 +++++++++++++++++-- 3 files changed, 80 insertions(+), 8 deletions(-) diff --git a/src/main/java/redis/clients/jedis/JedisPooled.java b/src/main/java/redis/clients/jedis/JedisPooled.java index 44e476d7b85..3746c166d45 100644 --- a/src/main/java/redis/clients/jedis/JedisPooled.java +++ b/src/main/java/redis/clients/jedis/JedisPooled.java @@ -26,7 +26,7 @@ public JedisPooled() { * @param url */ public JedisPooled(final String url) { - this(URI.create(url)); + super(url); } /** diff --git a/src/main/java/redis/clients/jedis/UnifiedJedis.java b/src/main/java/redis/clients/jedis/UnifiedJedis.java index 3a2dec9d771..876180e28ea 100644 --- a/src/main/java/redis/clients/jedis/UnifiedJedis.java +++ b/src/main/java/redis/clients/jedis/UnifiedJedis.java @@ -72,7 +72,7 @@ public UnifiedJedis(final URI uri) { this(JedisURIHelper.getHostAndPort(uri), DefaultJedisClientConfig.builder() .user(JedisURIHelper.getUser(uri)).password(JedisURIHelper.getPassword(uri)) .database(JedisURIHelper.getDBIndex(uri)).protocol(JedisURIHelper.getRedisProtocol(uri)) - .ssl(JedisURIHelper.isRedisSSLScheme(uri)).build()); + .ssl(JedisURIHelper.isRedisSSLScheme(uri)).build(), JedisURIHelper.getClientSideCache(uri)); } public UnifiedJedis(final URI uri, JedisClientConfig config) { @@ -85,13 +85,17 @@ public UnifiedJedis(final URI uri, JedisClientConfig config) { .protocol(JedisURIHelper.getRedisProtocol(uri)) .ssl(JedisURIHelper.isRedisSSLScheme(uri)).sslSocketFactory(config.getSslSocketFactory()) .sslParameters(config.getSslParameters()).hostnameVerifier(config.getHostnameVerifier()) - .build()); + .build(), JedisURIHelper.getClientSideCache(uri)); } public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig) { this(new PooledConnectionProvider(hostAndPort, clientConfig), clientConfig.getRedisProtocol()); } + public UnifiedJedis(HostAndPort hostAndPort, JedisClientConfig clientConfig, ClientSideCache clientSideCache) { + this(new PooledConnectionProvider(hostAndPort, clientConfig), clientConfig.getRedisProtocol(), clientSideCache); + } + public UnifiedJedis(ConnectionProvider provider) { this(new DefaultCommandExecutor(provider), provider); } diff --git a/src/main/java/redis/clients/jedis/util/JedisURIHelper.java b/src/main/java/redis/clients/jedis/util/JedisURIHelper.java index 6bbd1599a89..8f0a245af64 100644 --- a/src/main/java/redis/clients/jedis/util/JedisURIHelper.java +++ b/src/main/java/redis/clients/jedis/util/JedisURIHelper.java @@ -1,6 +1,7 @@ package redis.clients.jedis.util; import java.net.URI; +import redis.clients.jedis.ClientSideCache; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Protocol; import redis.clients.jedis.RedisProtocol; @@ -54,11 +55,11 @@ public static int getDBIndex(URI uri) { public static RedisProtocol getRedisProtocol(URI uri) { if (uri.getQuery() == null) return null; - String[] pairs = uri.getQuery().split("&"); - for (String pair : pairs) { - int idx = pair.indexOf("="); - if ("protocol".equals(pair.substring(0, idx))) { - String ver = pair.substring(idx + 1); + String[] params = uri.getQuery().split("&"); + for (String param : params) { + int idx = param.indexOf("="); + if ("protocol".equals(param.substring(0, idx))) { + String ver = param.substring(idx + 1); for (RedisProtocol proto : RedisProtocol.values()) { if (proto.version().equals(ver)) { return proto; @@ -70,6 +71,73 @@ public static RedisProtocol getRedisProtocol(URI uri) { return null; // null (default) when not defined } + private static final Integer ZERO_INTEGER = 0; + + public static ClientSideCache getClientSideCache(URI uri) { + if (uri.getQuery() == null) return null; + + boolean guava = false, caffeine = false; // cache_lib + Integer maxSize = null; // cache_max_size --> 0 = disbale + Integer ttl = null; // cache_ttl --> 0 = no ttl + // cache-max-idle + + String[] params = uri.getQuery().split("&"); + for (String param : params) { + int idx = param.indexOf("="); + String key = param.substring(0, idx); + String val = param.substring(idx + 1); + + switch (key) { + + case "cache_lib": + switch (val) { + case "guava": + guava = true; + break; + case "caffeine": + caffeine = true; + break; + default: + throw new IllegalArgumentException("Unsupported library " + val); + } + break; + + case "cache_max_size": + maxSize = Integer.parseInt(val); + break; + + case "ttl": + ttl = Integer.parseInt(val); + break; + } + } + + // special cases + if (ZERO_INTEGER.equals(maxSize)) { + return null; + } + if (ZERO_INTEGER.equals(ttl)) { + ttl = null; // below, only null will be checked + } + if (!guava && !caffeine) { + throw new IllegalArgumentException("The cache library (guava OR caffeine) must be selected."); + } + + if (guava) { + GuavaCSC.Builder guavaBuilder = GuavaCSC.builder(); + if (maxSize != null) guavaBuilder.maximumSize(maxSize); + if (ttl != null) guavaBuilder.ttl(ttl); + return guavaBuilder.build(); + } else if (caffeine) { + CaffeineCSC.Builder caffeineBuilder = CaffeineCSC.builder(); + if (maxSize != null) caffeineBuilder.maximumSize(maxSize); + if (ttl != null) caffeineBuilder.ttl(ttl); + return caffeineBuilder.build(); + } + + return null; // null (default) when not defined + } + public static boolean isValid(URI uri) { if (isEmpty(uri.getScheme()) || isEmpty(uri.getHost()) || uri.getPort() == -1) { return false;