参考
Lua 脚本
Programming in Lua
api
- 使用
RedisScript
- 使用
RedisTemplate.execute(RedisScript
script, List keys, Object... args)
几点需要关注的地方
redisScript.resultType
事实上, DefaultScriptExecutor 拿到 connection.eval 的返回结果后, 只是判断 redisScript.resultType 是否为空,为空则直接返回为空,
如果不为空, 则根据 返回结果是 byte[] 还是 list 调用反序列化函数进行反序列化。
protected T eval(RedisConnection connection, RedisScript script, ReturnType returnType, int numKeys,
byte[][] keysAndArgs, RedisSerializer resultSerializer) {
Object result;
try {
result = connection.evalSha(script.getSha1(), returnType, numKeys, keysAndArgs);
} catch (Exception e) {
if (!exceptionContainsNoScriptError(e)) {
throw e instanceof RuntimeException ? (RuntimeException) e : new RedisSystemException(e.getMessage(), e);
}
result = connection.eval(scriptBytes(script), returnType, numKeys, keysAndArgs);
}
if (script.getResultType() == null) {
return null;
}
return deserializeResult(resultSerializer, result);
}
....
@SuppressWarnings({ "rawtypes", "unchecked" })
protected T deserializeResult(RedisSerializer resultSerializer, Object result) {
if (result instanceof byte[]) {
if (resultSerializer == null) {
return (T) result;
}
return resultSerializer.deserialize((byte[]) result);
}
if (result instanceof List) {
List results = new ArrayList();
for (Object obj : (List) result) {
results.add(deserializeResult(resultSerializer, obj));
}
return (T) results;
}
return (T) result;
}
而序列化工具类则是调用 RedisTemplate
public T execute(final RedisScript script, final List keys, final Object... args) {
// use the Template's value serializer for args and result
return execute(script, template.getValueSerializer(), (RedisSerializer) template.getValueSerializer(), keys,
args);
}
返回值
redis 的返回值里, 只有 byte[]
字节, 或者 list
的我值数据, 所以不要写以下的代码
foo 下的数据不会返回.
local result = { foo = "name", "breaker", "time" }
return result
关于参数
@SuppressWarnings("unchecked")
public T execute(final RedisScript script, final List keys, final Object... args) {
// use the Template's value serializer for args and result
return execute(script, template.getValueSerializer(), (RedisSerializer) template.getValueSerializer(), keys,
args);
}
...
@SuppressWarnings({ "unchecked", "rawtypes" })
protected byte[][] keysAndArgs(RedisSerializer argsSerializer, List keys, Object[] args) {
final int keySize = keys != null ? keys.size() : 0;
byte[][] keysAndArgs = new byte[args.length + keySize][];
int i = 0;
if (keys != null) {
for (K key : keys) {
if (keySerializer() == null && key instanceof byte[]) {
keysAndArgs[i++] = (byte[]) key;
} else {
keysAndArgs[i++] = keySerializer().serialize(key);
}
}
}
for (Object arg : args) {
if (argsSerializer == null && arg instanceof byte[]) {
keysAndArgs[i++] = (byte[]) arg;
} else {
keysAndArgs[i++] = argsSerializer.serialize(arg);
}
}
return keysAndArgs;
}
显然, key 与 args 都是有使用 redisTemplate
的 值
序列化进行处理.
关于多个序列化工具的问题,
有时候,我们想对key 与值采用不同的序列化工具, 如 key , 使用 string 序列化, value 使用 json 序列化, 从代码上看, 需要重写
redisTemplate
另外, 如果想在 lua 脚本中使用 json, 可以使用全局变量 cjson
local value = { true, { foo = 'mindasset', joo = 'java' } }
local json_text = cjson.encode(value)
return json_text