diff --git a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java index 324e059079..4bd9408ae8 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java +++ b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java @@ -1,6 +1,7 @@ package redis.clients.jedis; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -21,8 +22,8 @@ public class JedisClusterInfoCache { private final Map nodes = new HashMap<>(); - private final Map slots = new HashMap<>(); - private final Map slotNodes = new HashMap<>(); + private final ConnectionPool[] slots = new ConnectionPool[Protocol.CLUSTER_HASHSLOTS]; + private final HostAndPort[] slotNodes = new HostAndPort[Protocol.CLUSTER_HASHSLOTS]; private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); private final Lock r = rwl.readLock(); @@ -148,8 +149,8 @@ private void discoverClusterSlots(Connection jedis) { } w.lock(); try { - this.slots.clear(); - this.slotNodes.clear(); + Arrays.fill(slots, null); + Arrays.fill(slotNodes, null); Set hostAndPortKeys = new HashSet<>(); for (Object slotInfoObj : slotsInfo) { @@ -224,8 +225,8 @@ public void assignSlotToNode(int slot, HostAndPort targetNode) { w.lock(); try { ConnectionPool targetPool = setupNodeIfNotExist(targetNode); - slots.put(slot, targetPool); - slotNodes.put(slot, targetNode); + slots[slot] = targetPool; + slotNodes[slot] = targetNode; } finally { w.unlock(); } @@ -236,8 +237,8 @@ public void assignSlotsToNode(List targetSlots, HostAndPort targetNode) try { ConnectionPool targetPool = setupNodeIfNotExist(targetNode); for (Integer slot : targetSlots) { - slots.put(slot, targetPool); - slotNodes.put(slot, targetNode); + slots[slot] = targetPool; + slotNodes[slot] = targetNode; } } finally { w.unlock(); @@ -260,7 +261,7 @@ public ConnectionPool getNode(HostAndPort node) { public ConnectionPool getSlotPool(int slot) { r.lock(); try { - return slots.get(slot); + return slots[slot]; } finally { r.unlock(); } @@ -269,7 +270,7 @@ public ConnectionPool getSlotPool(int slot) { public HostAndPort getSlotNode(int slot) { r.lock(); try { - return slotNodes.get(slot); + return slotNodes[slot]; } finally { r.unlock(); } @@ -311,8 +312,8 @@ public void reset() { } } nodes.clear(); - slots.clear(); - slotNodes.clear(); + Arrays.fill(slots, null); + Arrays.fill(slotNodes, null); } finally { w.unlock(); }