From 8e2e12cee1757ce538fbe746e160bd3fbaf40613 Mon Sep 17 00:00:00 2001 From: iroqueta Date: Wed, 12 Feb 2025 17:51:38 -0300 Subject: [PATCH] Implements support to set database and ssl in redis URL Issue 203458 --- .../com/genexus/cache/redis/RedisClient.java | 20 ++++++++++++------- .../cache/redis/TestRedisCacheClient.java | 14 +++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/gxcache-redis/src/main/java/com/genexus/cache/redis/RedisClient.java b/gxcache-redis/src/main/java/com/genexus/cache/redis/RedisClient.java index d88cff398..8256af0a3 100644 --- a/gxcache-redis/src/main/java/com/genexus/cache/redis/RedisClient.java +++ b/gxcache-redis/src/main/java/com/genexus/cache/redis/RedisClient.java @@ -22,16 +22,15 @@ import com.genexus.util.GXService; import com.genexus.util.GXServices; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisPool; -import redis.clients.jedis.JedisPoolConfig; -import redis.clients.jedis.Pipeline; +import redis.clients.jedis.*; public class RedisClient implements ICacheService2, Closeable { public static final ILogger logger = LogManager.getLogger(RedisClient.class); private String keyPattern = "%s_%s_%s"; //Namespace_KEY private static int REDIS_DEFAULT_PORT = 6379; + private static String REDIS_SCHEMA = "redis://"; + private static String REDIS_SSL_SCHEMA = "rediss://"; private JedisPool pool; private ObjectMapper objMapper; @@ -60,9 +59,11 @@ private void initCache(String hostOrRedisURL, String password, String cacheKeyPa String host = "127.0.0.1"; hostOrRedisURL = isNullOrEmpty(hostOrRedisURL) ? host: hostOrRedisURL; int port = REDIS_DEFAULT_PORT; + int database = Protocol.DEFAULT_DATABASE; + boolean ssl; - boolean isRedisURIScheme = hostOrRedisURL.startsWith("redis://"); - String sRedisURI = isRedisURIScheme ? hostOrRedisURL : "redis://" + hostOrRedisURL; + boolean isRedisURIScheme = hostOrRedisURL.startsWith(REDIS_SCHEMA) || hostOrRedisURL.startsWith(REDIS_SSL_SCHEMA); + String sRedisURI = isRedisURIScheme ? hostOrRedisURL : REDIS_SCHEMA + hostOrRedisURL; try { URI redisURI = new URI(sRedisURI); @@ -70,6 +71,11 @@ private void initCache(String hostOrRedisURL, String password, String cacheKeyPa if (redisURI.getPort() > 0) { port = redisURI.getPort(); } + String path = redisURI.getPath(); + if (path != null && path.length() > 1) { + database = Integer.parseInt(path.substring(1)); + } + ssl = redisURI.getScheme().equals("rediss"); } catch (URISyntaxException e) { logger.error(String.format("Could not parse Redis URL. Check for supported URLs: %s" , sRedisURI), e); throw e; @@ -77,7 +83,7 @@ private void initCache(String hostOrRedisURL, String password, String cacheKeyPa password = (!isNullOrEmpty(password)) ? Encryption.tryDecrypt64(password) : null; - pool = new JedisPool(new JedisPoolConfig(), host, port, redis.clients.jedis.Protocol.DEFAULT_TIMEOUT, password); + pool = new JedisPool(new JedisPoolConfig(), host, port, redis.clients.jedis.Protocol.DEFAULT_TIMEOUT, password, database, ssl); objMapper = new ObjectMapper(); objMapper diff --git a/gxcache-redis/src/test/java/com/genexus/test/cache/redis/TestRedisCacheClient.java b/gxcache-redis/src/test/java/com/genexus/test/cache/redis/TestRedisCacheClient.java index 654c7e9dd..cf4a55a21 100644 --- a/gxcache-redis/src/test/java/com/genexus/test/cache/redis/TestRedisCacheClient.java +++ b/gxcache-redis/src/test/java/com/genexus/test/cache/redis/TestRedisCacheClient.java @@ -35,6 +35,20 @@ public void connect_full_url() Assert.assertNotNull("Redis instance could not be created", redis); } + @Test + public void connect_full_ssl_url() + { + RedisClient redis = getRedisClient("rediss://localhost:6379", ""); + Assert.assertNotNull("Redis instance could not be created", redis); + } + + @Test + public void connect_full_url_database() + { + RedisClient redis = getRedisClient("redis://localhost:6379/4", ""); + Assert.assertNotNull("Redis instance could not be created", redis); + } + @Test public void connect_error_full_url() {