Reputation: 6068
In the StackExchange.Redis docs for scripting, it says a LoadedLuaScript can be used to avoid retransmitting the Lua script to redis each time it is evaluated. It then gives an example where the script is loaded into the server. I assume this shouldn't be done everytime you call the script, so where should the scripts be loaded?
Scripts are not persitent if redis server is restarted, so how should this be handled? Should they be loaded in ConnectionRestored event of the ConnectionMultiplexer? Presumably you would need to store them in a ConcurrentDictionary?
Upvotes: 6
Views: 1868
Reputation: 51
StackExchange.Redis handles Lua script caching internally. Normally you don't have to care about loading Lua scripts manually to redis.
StackExchange.Redis automatically transmits the Lua script to redis on the first call to 'ScriptEvaluate'. For further calls of the same script only the hash is used:
var prepared = LuaScript.Prepare(script);
prepared.Evaluate(someDb); // loads the script to redis and calls it
var prepared2 = LuaScript.Prepare(script);
prepared2.Evaluate(someDb); // calls the script by it's hash
LoadedLuaScript might be useful for edge cases like loading without executing.
I also clarified the official doku.
Background information
The library 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.
See ScriptEvalMessage:GetMessages in https://github.com/StackExchange/StackExchange.Redis/blob/main/src/StackExchange.Redis/RedisDatabase.cs
The behavior is also discussed and explained in the official StackExchange.Redis github repo:
Upvotes: 4