3
votes

I have a medium complexity LUA script which i have tested via the redis-cli. Using:

EVALSHA sha1 numkeys key1..keyn arg1..arg2

My script takes 2 arguments, no keys.

I'm loading the script on the .NET side from a text file into string luaString, then using

LuaScript lua = LuaScript.Prepare(luaString) to create the LuaScript object. So far so good?

Now i create:

object luaParams = new { '1923920', '{ "type":"message", "property1":"this is an example" }' };

RedisResult r = lua.Evaluate(IDatabase, luaParams);

Throws exception:

Additional information: ERR Error running script (call to f_a14d7a96f7556c52775eb277db66dfe0bfadd6a5): @user_script:37: @user_script: 37: Lua redis() command arguments must be strings or integers

I've looked at the github scripting.md but its example doesn't seem to address my scenario, or I am just not "getting" something.

What am I missing? I've tried quoted and unquoted luaParams, adding a 0 for no keys.

1

1 Answers

1
votes

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;
}