Correct config for redis cloud in heroku env

This commit is contained in:
Roman Mandeleil 2015-01-20 21:17:44 +02:00
parent 84859727c4
commit 93f6228034
2 changed files with 25 additions and 13 deletions

View File

@ -30,7 +30,7 @@ public class RedisDataSource implements KeyValueDataSource{
if (name == null) throw new NullPointerException("no name set to the db"); if (name == null) throw new NullPointerException("no name set to the db");
this.jedis = RedisPool.getResource(); this.jedis = RedisPool.getResource(name);
if (jedis == null) this.jedis = new Jedis("localhost"); if (jedis == null) this.jedis = new Jedis("localhost");
if (CONFIG.databaseReset()) if (CONFIG.databaseReset())
@ -45,19 +45,16 @@ public class RedisDataSource implements KeyValueDataSource{
@Override @Override
public byte[] get(byte[] key) { public byte[] get(byte[] key) {
jedis.select(index);
return jedis.get(key); return jedis.get(key);
} }
@Override @Override
public void put(byte[] key, byte[] value) { public void put(byte[] key, byte[] value) {
jedis.select(index);
jedis.set(key, value); jedis.set(key, value);
} }
@Override @Override
public void delete(byte[] key) { public void delete(byte[] key) {
jedis.select(index);
jedis.del(key); jedis.del(key);
} }
@ -68,7 +65,6 @@ public class RedisDataSource implements KeyValueDataSource{
@Override @Override
public void updateBatch(Map<byte[], byte[]> rows) { public void updateBatch(Map<byte[], byte[]> rows) {
jedis.select(index);
Pipeline pipeline = jedis.pipelined(); Pipeline pipeline = jedis.pipelined();
Iterator<Map.Entry<byte[], byte[]>> iterator = rows.entrySet().iterator(); Iterator<Map.Entry<byte[], byte[]>> iterator = rows.entrySet().iterator();

View File

@ -19,27 +19,43 @@ import java.net.URISyntaxException;
public class RedisPool { public class RedisPool {
private static final Logger logger = LoggerFactory.getLogger("db"); private static final Logger logger = LoggerFactory.getLogger("db");
private static JedisPool pool; private static JedisPool state;
private static JedisPool details;
static { static {
try { try {
if (System.getenv("REDISCLOUD_URL") != null){
URI redisUri = new URI(System.getenv("REDISCLOUD_URL")); if (System.getenv("REDIS_STATE") != null) {
URI redisUri = new URI(System.getenv("REDIS_STATE"));
logger.info("Init redis pool: " + redisUri.toString()); logger.info("Init redis pool: " + redisUri.toString());
pool = new JedisPool(new JedisPoolConfig(), state = new JedisPool(new JedisPoolConfig(),
redisUri.getHost(), redisUri.getHost(),
redisUri.getPort(), redisUri.getPort(),
Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT,
redisUri.getUserInfo().split(":",2)[1]); redisUri.getUserInfo().split(":",2)[1]);
} }
if (System.getenv("REDIS_DETAILS") != null) {
URI redisUri = new URI(System.getenv("REDIS_DETAILS"));
logger.info("Init redis pool: " + redisUri.toString());
details = new JedisPool(new JedisPoolConfig(),
redisUri.getHost(),
redisUri.getPort(),
Protocol.DEFAULT_TIMEOUT,
redisUri.getUserInfo().split(":",2)[1]);
}
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
logger.info("Pool is not available"); logger.info("Pool is not available");
} }
} }
public static Jedis getResource(){ public static Jedis getResource(String name){
if (pool == null) return null; if (state == null) return null;
return pool.getResource();
if (name.equals("state")) return state.getResource();
if (name.equals("details")) return details.getResource();
return null;
} }
} }