0

The spring data redis always require inputs in byte[], so I tried to create a wrapper class so I don't need to convert String to bytes everytime.

However now I encountered an issue with evalSHA method, since it accepts varargs.

evalSha(byte[] scriptSha, ReturnType returnType, int numKeys, byte[]... keysAndArgs)

How to convert my varargs in String to varargs in byte[]?

Below is my current code :

public List<String> evalSHA(String script, int numKeys, String ... keys){
    List<String> result = null;
    RedisConnection redis = redisConnectionFactory.getConnection();
    byte[] scriptSHA = redis.get("SCRIPT:CALGROUPQUEUE".getBytes());

    if(scriptSHA==null || scriptSHA.length==0){
        logger.error("no such script");
        return null;
    }

    List<byte[]> keysInByte = vargsToList(keys);
    // what to do below?
    List<byte[]> resultBytes =  redis.evalSha  (scriptSHA, ReturnType.MULTI, ???); 

    if(resultBytes!=null && !resultBytes.isEmpty()){
        result = new ArrayList<>();
        //... to do later
    }
}
Rudy
  • 7,008
  • 12
  • 50
  • 85

1 Answers1

1

varargs is nothing but an array, which means in your case, it's a 2D byte array. So you can do the below:

List<byte[]> resultBytes =  redis.evalSha(scriptSHA, ReturnType.MULTI, keysInByte.toArray(new byte[][] {}));

This uses the toArray method of List to convert it into an array. To simplify this a bit, you could also have a vargsToArray() helper method instead, which returns an array directly.

Vasan
  • 4,810
  • 4
  • 20
  • 39