Skip to content

Implements support to set database and ssl in redis URL #939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -60,24 +59,31 @@ 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);
host = redisURI.getHost();
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;
}

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Loading