pcf.cache.redis.RedisCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of RedisConfig Show documentation
Show all versions of RedisConfig Show documentation
A rolled up RedisConfig for Java-Based-Non-Spring Applications
package pcf.cache.redis;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import redis.clients.jedis.Jedis;
public class RedisCache {
private Jedis cache;
public RedisCache(String VCAP) {
JsonElement rootElement = new JsonParser().parse(VCAP);
JsonElement redisElement = rootElement.getAsJsonObject().get("p-redis");
JsonObject redisJsonObject = redisElement.getAsJsonArray().get(0).getAsJsonObject();
JsonObject redisCreds = redisJsonObject.get("credentials").getAsJsonObject();
String host = redisCreds.get("host").getAsString();
Integer port = redisCreds.get("port").getAsInt();
String pass = redisCreds.get("password").getAsString();
cache = new Jedis(host, port);
cache.connect();
cache.auth(pass);
}
public void setMany(String keyVals) {
for(String keyVal : keyVals.split(System.getProperty("line.separator"))) {
String[] keyValPair = keyVal.split(":");
String key = keyValPair[0].trim();
String value = keyValPair[1].trim();
cache.set(key, value);
}
}
public Jedis getCache() {
return cache;
}
}