Reputation: 3870
I'm trying to use StackExchange.Redis on Azure WebApp and need to run some Lua scripts.
The recommended way is to load the scripts to the server but I have difficulty understanding the correct pattern.
The way I think it should be done is on WebApp startup:
LuaScript luaScript = LuaScript.Prepare(scriptString);
var endpoints = redis.GetEndPoints();
var endpoint = endpoints[0];
IServer server = redis.GetServer(endpoint);
LoadedLuaScript loadedScript = luaScript.Load(server);
Then keep the LoadedLuaScript for later use (together with the ConnectionMultiplexer). Then later when I want to run the Lua script:
IDatabase db = redis.GetDatabase();
db.ScriptEvaluate(loadedScript);
Is this the correct way?
As Azure can have more than one Redis node, should I run the luaScript.Load for each endpoint and keep only one of them for later use? i.e.:
LuaScript luaScript = LuaScript.Prepare(script);
var endpoints = redis.GetEndPoints();
LoadedLuaScript loadedScript;
foreach (var endpoint in endpoints)
{
IServer server = redis.GetServer(endpoint);
loadedScript = luaScript.Load(server);
}
return loadedScript;
Upvotes: 13
Views: 2135
Reputation: 51
See the documentation. With StackExchange.Redis you don't have to manually load the Lua script to redis. And you don't have to use LoadedLuaScript.
Even when you have multiple Redis nodes the LuaScript class handles the transmission of the script automatically. It tries to call the script by it's hash and if the script is missing it gets a NOSCRIPT error and then transmits the script.
The correct way is to simply call
LuaScript.Prepare(script).Evaluate(myDb)
See ScriptEvalMessage:GetMessages in https://github.com/StackExchange/StackExchange.Redis/blob/main/src/StackExchange.Redis/RedisDatabase.cs
Upvotes: 0