All Downloads are FREE. Search and download functionalities are using the official Maven repository.

redis.RedisCache Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version
package 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;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy