com.redis.spring.batch.reader.Evalsha Maven / Gradle / Ivy
The newest version!
package com.redis.spring.batch.reader;
import java.io.IOException;
import java.util.List;
import java.util.function.Function;
import org.springframework.batch.item.Chunk;
import com.redis.spring.batch.common.Operation;
import com.redis.spring.batch.util.CodecUtils;
import com.redis.spring.batch.util.ConnectionUtils;
import io.lettuce.core.AbstractRedisClient;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.ScriptOutputType;
import io.lettuce.core.api.async.BaseRedisAsyncCommands;
import io.lettuce.core.api.async.RedisScriptingAsyncCommands;
import io.lettuce.core.codec.RedisCodec;
public class Evalsha implements Operation> {
private final String digest;
private final Function stringValueFunction;
private Function keyFunction;
private Function argsFunction;
public Evalsha(String filename, AbstractRedisClient client, RedisCodec codec) throws IOException {
this.digest = ConnectionUtils.loadScript(client, filename);
this.stringValueFunction = CodecUtils.stringValueFunction(codec);
}
public void setKeys(K key) {
setKeyFunction(t -> key);
}
public void setKeyFunction(Function function) {
this.keyFunction = function;
}
public void setArgsFunction(Function function) {
this.argsFunction = function;
}
@SuppressWarnings("unchecked")
public void setArgs(Object... args) {
V[] encodedArgs = (V[]) new Object[args.length];
for (int index = 0; index < args.length; index++) {
encodedArgs[index] = stringValueFunction.apply(String.valueOf(args[index]));
}
setArgsFunction(t -> encodedArgs);
}
@SuppressWarnings("unchecked")
@Override
public void execute(BaseRedisAsyncCommands commands, Chunk extends T> inputs,
Chunk>> outputs) {
RedisScriptingAsyncCommands scriptingCommands = (RedisScriptingAsyncCommands) commands;
for (T item : inputs) {
K[] keys = (K[]) new Object[] { keyFunction.apply(item) };
V[] args = argsFunction.apply(item);
outputs.add(scriptingCommands.evalsha(digest, ScriptOutputType.MULTI, keys, args));
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy