Skip to content

Commit

Permalink
Add TLS
Browse files Browse the repository at this point in the history
  • Loading branch information
amontenegro committed Jul 27, 2023
1 parent f34f7d1 commit 3b40981
Showing 1 changed file with 24 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,40 @@
import org.slf4j.LoggerFactory;

import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.DefaultRedisCredentials;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisClientConfig;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.RedisCredentials;
import redis.clients.jedis.RedisCredentialsProvider;
import redis.clients.jedis.params.SetParams;

public class RedisClient {

private static final Logger LOG = LoggerFactory.getLogger(RedisClient.class);

private static final int DEFAULT_CACHE_EXPIRY = 60;
private static final int DEFAULT_TIMEOUT = 3000;
private static final int DEFAULT_TIMEOUT = 5000;

private String redisHost;
private int redisPort;
private int cacheExpiryInSecs;
private int timeoutInMillis;
private JedisPool pool;
private SetParams setParams;

public RedisClient(String redisHost, int redisPort) {
this.redisHost = redisHost;
this.redisPort = redisPort;
// Default values
this.cacheExpiryInSecs = DEFAULT_CACHE_EXPIRY;
this.timeoutInMillis = DEFAULT_TIMEOUT;
init();

public RedisClient(String redisHost, int redisPort, String password) {
init(redisHost, redisPort, password, DEFAULT_CACHE_EXPIRY, DEFAULT_TIMEOUT);
}

public RedisClient(String redisHost, int redisPort, int cacheExpiryInSecs) {
this.redisHost = redisHost;
this.redisPort = redisPort;
this.cacheExpiryInSecs = cacheExpiryInSecs;
this.timeoutInMillis = DEFAULT_TIMEOUT;
init();
public RedisClient(String redisHost, int redisPort, String password, int cacheExpiryInSecs) {
init(redisHost, redisPort, password, cacheExpiryInSecs, DEFAULT_TIMEOUT);
}

public RedisClient(String redisHost, int redisPort, int cacheExpiryInSecs, int clientTimeoutInMillis) {
this.redisHost = redisHost;
this.redisPort = redisPort;
this.cacheExpiryInSecs = cacheExpiryInSecs;
this.timeoutInMillis = clientTimeoutInMillis;
init();
public RedisClient(String redisHost, int redisPort, String password, int cacheExpiryInSecs, int clientTimeoutInMillis) {
init(redisHost, redisPort, password, cacheExpiryInSecs, clientTimeoutInMillis);
}

private void init() {
private void init(String redisHost, int redisPort, String password, int cacheExpiryInSecs, int timeoutInMillis) {
JedisClientConfig config = DefaultJedisClientConfig.builder().connectionTimeoutMillis(timeoutInMillis).timeoutMillis(timeoutInMillis)
.socketTimeoutMillis(timeoutInMillis).build();
.socketTimeoutMillis(timeoutInMillis).password(password).ssl(true).build();
pool = new JedisPool(new HostAndPort(redisHost, redisPort), config);
setParams = new SetParams().ex(cacheExpiryInSecs);
}
Expand All @@ -70,4 +56,15 @@ public String get(String key) {
return jedis.get(key);
}
}

//Trying to test TLS
public static void main(String [] args) {
RedisClient c = new RedisClient("xxx", 6379, "xxxxx");
c.set("name", "angelito");
c.set("name2", "angelito2");
c.set("name3", "angelito3");
System.out.println(c.get("name"));
System.out.println(c.get("name2"));
System.out.println(c.get("name3"));
}
}

0 comments on commit 3b40981

Please sign in to comment.