我有一个中等复杂度的LUA脚本,我通过redis-cli测试过。使用:
EVALSHA sha1 numkeys key1..keyn arg1..arg2
我的脚本需要2个参数,没有键。
我正在将.NET脚本从文本文件加载到string luaString
,然后使用
LuaScript lua = LuaScript.Prepare(luaString)
创建LuaScript对象。到目前为止还不错?
现在我创建:
object luaParams = new { '1923920', '{ "type":"message", "property1":"this is an example" }' };
RedisResult r = lua.Evaluate(IDatabase, luaParams);
抛出异常:
其他信息:ERR错误运行脚本(致电 f_a14d7a96f7556c52775eb277db66dfe0bfadd6a5):@ user_script:37: @user_script:37:Lua redis()命令参数必须是字符串或 整数
我看过github scripting.md,但它的例子似乎并没有解决我的问题,或者我只是没有“得到”某些东西。
我错过了什么?我试过引用和不引用的luaParams,为无键添加0。
答案 0 :(得分:0)
Here's how I did it... (with some psuedo code mixed in since i can't provide exact - it lacks sufficient context)
string actualScript = File(pathToFullLuaScriptOnDotNetServer).ReadToEnd();
string luaSha = rClient.CalculateSha1(actualScript);
//This current impl is not optimized - should do this in AppStart
bool hasScript = rClient.HasLuaScript(luaSha);
if (!hasScript)
{
luaSha = rClient.LoadLuaScript(CommWebAPI.RedisConfig.scriptString);
}
...
string geom = f["geometry"].ToString();
string[] p = new string[] { integerParam + "", geom.StripNewLines() };
List<string> result = rClient.ExecLuaShaAsList(luaSha, p);
//getLuaScriptError is a custom function - see below
string luaError = getLuaScriptError(result);
if (null == luaError)
{
aggregatedResults = aggregatedResults.Union(result).ToList();
}
else
{
Debug.Print("Redis LUA error: " + luaError);
}
...
private string getLuaScriptError(List<string> response)
{
//check if LUA error: 2 results, 0 == null, 1 == error message.
if (2 == response.Count)
{
if (null == response[0])
{
return response[1];
}
}
return null;
}