88import  redis .clients .jedis .exceptions .JedisNoReachableClusterNodeException ;
99import  redis .clients .jedis .exceptions .JedisRedirectionException ;
1010import  redis .clients .util .JedisClusterCRC16 ;
11- import  redis .clients .util .SafeEncoder ;
1211
1312public  abstract  class  JedisClusterCommand <T > {
1413
15-   private  JedisClusterConnectionHandler  connectionHandler ;
16-   private  int  maxAttempts ;
17-   private  ThreadLocal <Jedis > askConnection  = new  ThreadLocal <Jedis >();
14+   private  final   JedisClusterConnectionHandler  connectionHandler ;
15+   private  final   int  maxAttempts ;
16+   private  final   ThreadLocal <Jedis > askConnection  = new  ThreadLocal <Jedis >();
1817
1918  public  JedisClusterCommand (JedisClusterConnectionHandler  connectionHandler , int  maxAttempts ) {
2019    this .connectionHandler  = connectionHandler ;
@@ -28,18 +27,17 @@ public T run(String key) {
2827      throw  new  JedisClusterException ("No way to dispatch this command to Redis Cluster." );
2928    }
3029
31-     return  runWithRetries (SafeEncoder . encode (key ), this .maxAttempts , false , false );
30+     return  runWithRetries (JedisClusterCRC16 . getSlot (key ), this .maxAttempts , false , false );
3231  }
3332
3433  public  T  run (int  keyCount , String ... keys ) {
3534    if  (keys  == null  || keys .length  == 0 ) {
3635      throw  new  JedisClusterException ("No way to dispatch this command to Redis Cluster." );
3736    }
3837
39-     // For multiple keys, only execute if they all share the 
40-     // same connection slot. 
38+     // For multiple keys, only execute if they all share the same connection slot.  
39+     int   slot  =  JedisClusterCRC16 . getSlot ( keys [ 0 ]); 
4140    if  (keys .length  > 1 ) {
42-       int  slot  = JedisClusterCRC16 .getSlot (keys [0 ]);
4341      for  (int  i  = 1 ; i  < keyCount ; i ++) {
4442        int  nextSlot  = JedisClusterCRC16 .getSlot (keys [i ]);
4543        if  (slot  != nextSlot ) {
@@ -49,26 +47,25 @@ public T run(int keyCount, String... keys) {
4947      }
5048    }
5149
52-     return  runWithRetries (SafeEncoder . encode ( keys [ 0 ]) , this .maxAttempts , false , false );
50+     return  runWithRetries (slot , this .maxAttempts , false , false );
5351  }
5452
5553  public  T  runBinary (byte [] key ) {
5654    if  (key  == null ) {
5755      throw  new  JedisClusterException ("No way to dispatch this command to Redis Cluster." );
5856    }
5957
60-     return  runWithRetries (key , this .maxAttempts , false , false );
58+     return  runWithRetries (JedisClusterCRC16 . getSlot ( key ) , this .maxAttempts , false , false );
6159  }
6260
6361  public  T  runBinary (int  keyCount , byte []... keys ) {
6462    if  (keys  == null  || keys .length  == 0 ) {
6563      throw  new  JedisClusterException ("No way to dispatch this command to Redis Cluster." );
6664    }
6765
68-     // For multiple keys, only execute if they all share the 
69-     // same connection slot. 
66+     // For multiple keys, only execute if they all share the same connection slot.  
67+     int   slot  =  JedisClusterCRC16 . getSlot ( keys [ 0 ]); 
7068    if  (keys .length  > 1 ) {
71-       int  slot  = JedisClusterCRC16 .getSlot (keys [0 ]);
7269      for  (int  i  = 1 ; i  < keyCount ; i ++) {
7370        int  nextSlot  = JedisClusterCRC16 .getSlot (keys [i ]);
7471        if  (slot  != nextSlot ) {
@@ -78,7 +75,7 @@ public T runBinary(int keyCount, byte[]... keys) {
7875      }
7976    }
8077
81-     return  runWithRetries (keys [ 0 ] , this .maxAttempts , false , false );
78+     return  runWithRetries (slot , this .maxAttempts , false , false );
8279  }
8380
8481  public  T  runWithAnyNode () {
@@ -93,7 +90,7 @@ public T runWithAnyNode() {
9390    }
9491  }
9592
96-   private  T  runWithRetries (byte []  key , int  attempts , boolean  tryRandomNode , boolean  asking ) {
93+   private  T  runWithRetries (final   int   slot , int  attempts , boolean  tryRandomNode , boolean  asking ) {
9794    if  (attempts  <= 0 ) {
9895      throw  new  JedisClusterMaxRedirectionsException ("Too many Cluster redirections?" );
9996    }
@@ -113,7 +110,7 @@ private T runWithRetries(byte[] key, int attempts, boolean tryRandomNode, boolea
113110        if  (tryRandomNode ) {
114111          connection  = connectionHandler .getConnection ();
115112        } else  {
116-           connection  = connectionHandler .getConnectionFromSlot (JedisClusterCRC16 . getSlot ( key ) );
113+           connection  = connectionHandler .getConnectionFromSlot (slot );
117114        }
118115      }
119116
@@ -138,7 +135,7 @@ private T runWithRetries(byte[] key, int attempts, boolean tryRandomNode, boolea
138135        throw  jce ;
139136      }
140137
141-       return  runWithRetries (key , attempts  - 1 , tryRandomNode , asking );
138+       return  runWithRetries (slot , attempts  - 1 , tryRandomNode , asking );
142139    } catch  (JedisRedirectionException  jre ) {
143140      // if MOVED redirection occurred, 
144141      if  (jre  instanceof  JedisMovedDataException ) {
@@ -159,7 +156,7 @@ private T runWithRetries(byte[] key, int attempts, boolean tryRandomNode, boolea
159156        throw  new  JedisClusterException (jre );
160157      }
161158
162-       return  runWithRetries (key , attempts  - 1 , false , asking );
159+       return  runWithRetries (slot , attempts  - 1 , false , asking );
163160    } finally  {
164161      releaseConnection (connection );
165162    }
0 commit comments