Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we need to cache loaded scripts when using StackExchange.Redis?

I'm creating a library for use principally in a non-web environment that makes use of Lua scripts via StackExchange.Redis.

Should we be loading Lua scripts for every Evaluate call when using StackExchange.Redis, like

var prepped = LuaScript.Prepare(_someScript);
var loaded = prepped.Load(someServer);
loaded.Evaluate(someDb);

or should we be loading once and then reusing the LoadedLuaScript instances each time we're going to Evaluate them in a given process?

like image 241
Tim Barrass Avatar asked Feb 17 '26 20:02

Tim Barrass


2 Answers

Scripts should be loaded once, at startup, and keep the LoadedLuaScript instances to Evaluate when needed. StackExchange.Redis doesn't cache the LoadedLuaScripts anywhere, so if you go through the prep and load process on every call then all you'll do is have StackExchange.Redis transfer the script over to Redis, where it'll be hashed, Redis will realise that it already has it, then pass the hash back.

If your processes aren't starting up frequently (for some value of frequently) then it might be reasonable to load all scripts at startup even if they've been loaded in Redis already, as it won't lead to multiple instances of those scripts being cached at Redis. You could keep the LoadedLuaScripts available in a simple cache, like:

    private static readonly string _helloScript =
          "print(\"Hello World!\")"
        ;

    public void LoadScripts(IDatabase db, IServer srv)
    {
        var scripts = new Dictionary<string, string>
        {
            { "sayHello",                      _helloScript },
        };

        foreach (var scriptName in scripts.Keys)
        {
            var prepped = LuaScript.Prepare(scripts[scriptName]);

            _scripts.Add(scriptName, prepped.Load(srv));
        }
    }

    public void SayHello(IDatabase db)
    {
        _scripts["sayHello"].Evaluate(db);
    }
like image 195
Tim Barrass Avatar answered Feb 19 '26 09:02

Tim Barrass


For performance reasons Lua scripts can be loaded to redis and then called by their hash. This loading is optional and should be done only once to gain the performance benefit.

With StackExchange.Redis you don't have to explicitly use the Load method. It is easier to let StackExchange.Redis handle the caching. The LuaScript class 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
like image 35
ChriZ Avatar answered Feb 19 '26 10:02

ChriZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!