Skip to content

Commit f787a37

Browse files
authored
Implements support to set database and ssl in redis URL (#939)
Issue 203458
1 parent 1f810bb commit f787a37

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

gxcache-redis/src/main/java/com/genexus/cache/redis/RedisClient.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,15 @@
2222
import com.genexus.util.GXService;
2323
import com.genexus.util.GXServices;
2424

25-
import redis.clients.jedis.Jedis;
26-
import redis.clients.jedis.JedisPool;
27-
import redis.clients.jedis.JedisPoolConfig;
28-
import redis.clients.jedis.Pipeline;
25+
import redis.clients.jedis.*;
2926

3027

3128
public class RedisClient implements ICacheService2, Closeable {
3229
public static final ILogger logger = LogManager.getLogger(RedisClient.class);
3330
private String keyPattern = "%s_%s_%s"; //Namespace_KEY
3431
private static int REDIS_DEFAULT_PORT = 6379;
32+
private static String REDIS_SCHEMA = "redis://";
33+
private static String REDIS_SSL_SCHEMA = "rediss://";
3534
private JedisPool pool;
3635
private ObjectMapper objMapper;
3736

@@ -60,24 +59,31 @@ private void initCache(String hostOrRedisURL, String password, String cacheKeyPa
6059
String host = "127.0.0.1";
6160
hostOrRedisURL = isNullOrEmpty(hostOrRedisURL) ? host: hostOrRedisURL;
6261
int port = REDIS_DEFAULT_PORT;
62+
int database = Protocol.DEFAULT_DATABASE;
63+
boolean ssl;
6364

64-
boolean isRedisURIScheme = hostOrRedisURL.startsWith("redis://");
65-
String sRedisURI = isRedisURIScheme ? hostOrRedisURL : "redis://" + hostOrRedisURL;
65+
boolean isRedisURIScheme = hostOrRedisURL.startsWith(REDIS_SCHEMA) || hostOrRedisURL.startsWith(REDIS_SSL_SCHEMA);
66+
String sRedisURI = isRedisURIScheme ? hostOrRedisURL : REDIS_SCHEMA + hostOrRedisURL;
6667

6768
try {
6869
URI redisURI = new URI(sRedisURI);
6970
host = redisURI.getHost();
7071
if (redisURI.getPort() > 0) {
7172
port = redisURI.getPort();
7273
}
74+
String path = redisURI.getPath();
75+
if (path != null && path.length() > 1) {
76+
database = Integer.parseInt(path.substring(1));
77+
}
78+
ssl = redisURI.getScheme().equals("rediss");
7379
} catch (URISyntaxException e) {
7480
logger.error(String.format("Could not parse Redis URL. Check for supported URLs: %s" , sRedisURI), e);
7581
throw e;
7682
}
7783

7884
password = (!isNullOrEmpty(password)) ? Encryption.tryDecrypt64(password) : null;
7985

80-
pool = new JedisPool(new JedisPoolConfig(), host, port, redis.clients.jedis.Protocol.DEFAULT_TIMEOUT, password);
86+
pool = new JedisPool(new JedisPoolConfig(), host, port, redis.clients.jedis.Protocol.DEFAULT_TIMEOUT, password, database, ssl);
8187

8288
objMapper = new ObjectMapper();
8389
objMapper

gxcache-redis/src/test/java/com/genexus/test/cache/redis/TestRedisCacheClient.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ public void connect_full_url()
3535
Assert.assertNotNull("Redis instance could not be created", redis);
3636
}
3737

38+
@Test
39+
public void connect_full_ssl_url()
40+
{
41+
RedisClient redis = getRedisClient("rediss://localhost:6379", "");
42+
Assert.assertNotNull("Redis instance could not be created", redis);
43+
}
44+
45+
@Test
46+
public void connect_full_url_database()
47+
{
48+
RedisClient redis = getRedisClient("redis://localhost:6379/4", "");
49+
Assert.assertNotNull("Redis instance could not be created", redis);
50+
}
51+
3852
@Test
3953
public void connect_error_full_url()
4054
{

0 commit comments

Comments
 (0)