コード例 #1
0
 /**
  * Construct a new ketamaRouter
  *
  * @param redisClientMap key is redis client, value is weight, must > 0.
  */
 public KetamaRouter(Map<RedisClient, Integer> redisClientMap) {
   this.redisClientMap = redisClientMap;
   ketamaClients = new TreeMap<Long, List<RedisClient>>();
   // build ke
   for (RedisClient client : redisClientMap.keySet()) {
     String hashKey = client.hashKey();
     int numReps = NUM_REPS * redisClientMap.get(client);
     for (int i = 0; i < numReps / 4; i++) {
       byte[] digest = HashAlgorithm.computeMd5(hashKey + "-" + i);
       for (int h = 0; h < 4; h++) {
         long k =
             (long) (digest[3 + h * 4] & 0xFF) << 24
                 | (long) (digest[2 + h * 4] & 0xFF) << 16
                 | (long) (digest[1 + h * 4] & 0xFF) << 8
                 | digest[h * 4] & 0xFF;
         this.getClientList(ketamaClients, k).add(client);
       }
     }
   }
 }