use ThreadLocal<Random> instead of creating Random each time
* We can't use ThreadLocalRandom because we need to support JDK6
This commit is contained in:
@@ -11,6 +11,7 @@ import static redis.clients.jedis.JedisClusterInfoCache.getNodeKey;
|
|||||||
|
|
||||||
public abstract class JedisClusterConnectionHandler {
|
public abstract class JedisClusterConnectionHandler {
|
||||||
protected final JedisClusterInfoCache cache;
|
protected final JedisClusterInfoCache cache;
|
||||||
|
private ThreadLocal<Random> random = new ThreadLocal<Random>();
|
||||||
|
|
||||||
abstract Jedis getConnection();
|
abstract Jedis getConnection();
|
||||||
|
|
||||||
@@ -28,6 +29,7 @@ public abstract class JedisClusterConnectionHandler {
|
|||||||
|
|
||||||
public JedisClusterConnectionHandler(Set<HostAndPort> nodes, final GenericObjectPoolConfig poolConfig) {
|
public JedisClusterConnectionHandler(Set<HostAndPort> nodes, final GenericObjectPoolConfig poolConfig) {
|
||||||
this.cache = new JedisClusterInfoCache(poolConfig);
|
this.cache = new JedisClusterInfoCache(poolConfig);
|
||||||
|
this.random.set(new Random());
|
||||||
initializeSlotsCache(nodes, poolConfig);
|
initializeSlotsCache(nodes, poolConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +82,7 @@ public abstract class JedisClusterConnectionHandler {
|
|||||||
|
|
||||||
protected JedisPool getRandomConnection() {
|
protected JedisPool getRandomConnection() {
|
||||||
Object[] nodeArray = cache.getNodes().values().toArray();
|
Object[] nodeArray = cache.getNodes().values().toArray();
|
||||||
return (JedisPool) (nodeArray[new Random().nextInt(nodeArray.length)]);
|
return (JedisPool) (nodeArray[this.random.get().nextInt(nodeArray.length)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user