1
1
package redis .clients .jedis ;
2
2
3
3
import java .util .ArrayList ;
4
+ import java .util .Arrays ;
4
5
import java .util .Collections ;
5
6
import java .util .HashMap ;
6
7
import java .util .HashSet ;
21
22
public class JedisClusterInfoCache {
22
23
23
24
private final Map <String , ConnectionPool > nodes = new HashMap <>();
24
- private final Map < Integer , ConnectionPool > slots = new HashMap <>() ;
25
- private final Map < Integer , HostAndPort > slotNodes = new HashMap <>() ;
25
+ private final ConnectionPool [] slots = new ConnectionPool [ Protocol . CLUSTER_HASHSLOTS ] ;
26
+ private final HostAndPort [] slotNodes = new HostAndPort [ Protocol . CLUSTER_HASHSLOTS ] ;
26
27
27
28
private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock ();
28
29
private final Lock r = rwl .readLock ();
@@ -148,8 +149,8 @@ private void discoverClusterSlots(Connection jedis) {
148
149
}
149
150
w .lock ();
150
151
try {
151
- this . slots . clear ( );
152
- this . slotNodes . clear ( );
152
+ Arrays . fill ( slots , null );
153
+ Arrays . fill ( slotNodes , null );
153
154
Set <String > hostAndPortKeys = new HashSet <>();
154
155
155
156
for (Object slotInfoObj : slotsInfo ) {
@@ -224,8 +225,8 @@ public void assignSlotToNode(int slot, HostAndPort targetNode) {
224
225
w .lock ();
225
226
try {
226
227
ConnectionPool targetPool = setupNodeIfNotExist (targetNode );
227
- slots . put ( slot , targetPool ) ;
228
- slotNodes . put ( slot , targetNode ) ;
228
+ slots [ slot ] = targetPool ;
229
+ slotNodes [ slot ] = targetNode ;
229
230
} finally {
230
231
w .unlock ();
231
232
}
@@ -236,8 +237,8 @@ public void assignSlotsToNode(List<Integer> targetSlots, HostAndPort targetNode)
236
237
try {
237
238
ConnectionPool targetPool = setupNodeIfNotExist (targetNode );
238
239
for (Integer slot : targetSlots ) {
239
- slots . put ( slot , targetPool ) ;
240
- slotNodes . put ( slot , targetNode ) ;
240
+ slots [ slot ] = targetPool ;
241
+ slotNodes [ slot ] = targetNode ;
241
242
}
242
243
} finally {
243
244
w .unlock ();
@@ -260,7 +261,7 @@ public ConnectionPool getNode(HostAndPort node) {
260
261
public ConnectionPool getSlotPool (int slot ) {
261
262
r .lock ();
262
263
try {
263
- return slots . get ( slot ) ;
264
+ return slots [ slot ] ;
264
265
} finally {
265
266
r .unlock ();
266
267
}
@@ -269,7 +270,7 @@ public ConnectionPool getSlotPool(int slot) {
269
270
public HostAndPort getSlotNode (int slot ) {
270
271
r .lock ();
271
272
try {
272
- return slotNodes . get ( slot ) ;
273
+ return slotNodes [ slot ] ;
273
274
} finally {
274
275
r .unlock ();
275
276
}
@@ -311,8 +312,8 @@ public void reset() {
311
312
}
312
313
}
313
314
nodes .clear ();
314
- slots . clear ( );
315
- slotNodes . clear ( );
315
+ Arrays . fill ( slots , null );
316
+ Arrays . fill ( slotNodes , null );
316
317
} finally {
317
318
w .unlock ();
318
319
}
0 commit comments