Make JedisCluster multihread by improving connection handling

This commit is contained in:
Marcos Nils
2014-02-18 21:59:53 -03:00
parent b2fa6b6c40
commit e4de67048e
4 changed files with 262 additions and 265 deletions

View File

@@ -5,25 +5,12 @@ import java.util.Set;
public class JedisSlotBasedConnectionHandler extends
JedisClusterConnectionHandler {
private Jedis currentConnection;
public JedisSlotBasedConnectionHandler(Set<HostAndPort> nodes) {
super(nodes);
}
public Jedis getConnection() {
return currentConnection != null ? currentConnection
: getRandomConnection().getResource();
}
private void returnCurrentConnection() {
if (currentConnection != null) {
nodes.get(
currentConnection.getClient().getHost()
+ currentConnection.getClient().getPort())
.returnResource(currentConnection);
}
return getRandomConnection().getResource();
}
@Override
@@ -34,13 +21,11 @@ public class JedisSlotBasedConnectionHandler extends
@Override
public Jedis getConnectionFromSlot(int slot) {
returnCurrentConnection();
JedisPool connectionPool = slots.get(slot);
if (connectionPool == null) {
connectionPool = getRandomConnection();
}
currentConnection = connectionPool.getResource();
return connectionPool.getResource();
return connectionPool.getResource();
}
}