package net.rainbowcode.jpixelface.redis;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.net.URI;
import java.net.URISyntaxException;
public class RedisUtils {
private static JedisPool pool = getPool();
private static JedisPool getPool() {
URI redisUri = null;
try {
redisUri = new URI(System.getenv("REDIS_URL"));
} catch (URISyntaxException e) {
e.printStackTrace();
System.exit(0);
}
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(10);
poolConfig.setMaxIdle(5);
poolConfig.setMinIdle(1);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
poolConfig.setTestWhileIdle(true);
return new JedisPool(poolConfig, redisUri);
}
/**
* Sets the key and expiry time
*
* @param key The key
* @param value The value
* @param expiry The expiry time in seconds
*/
public static void setAndExpire(String key, String value, int expiry) {
try (Jedis jedis = pool.getResource()) {
jedis.set(key, value);
jedis.expire(key, expiry);
}
}
/**
* Sets the key and expiry time
*
* @param key The key
* @param value The value
* @param expiry The expiry time in seconds
*/
public static void setAndExpire(byte[] key, byte[] value, int expiry) {
try (Jedis jedis = pool.getResource()) {
jedis.set(key, value);
jedis.expire(key, expiry);
}
}
public static String getAsString(String key) {
String retVal = null;
try (Jedis jedis = pool.getResource()) {
retVal = jedis.get(key);
}
return retVal;
}
public static byte[] getAsBytes(byte[] key) {
byte[] retVal = null;
try (Jedis jedis = pool.getResource()) {
retVal = jedis.get(key);
}
return retVal;
}
public static JsonObject getAsJson(String key) {
JsonObject retVal = null;
try (Jedis jedis = pool.getResource()) {
retVal = jsonObjectFromString(jedis.get(key));
}
return retVal;
}
private static JsonObject jsonObjectFromString(String string) {
JsonParser parser = new JsonParser();
JsonObject object = parser.parse(string).getAsJsonObject();
return object;
}
public static boolean exists(String key) {
boolean retVal = false;
try (Jedis jedis = pool.getResource()) {
retVal = jedis.exists(key);
}
return retVal;
}
public static boolean exists(byte[] key) {
boolean retVal = false;
try (Jedis jedis = pool.getResource()) {
retVal = jedis.exists(key);
}
return retVal;
}
}