在 StackExchange.Redis 中为多个端点/服务器使用 LoadedLuaScript

时间:2021-06-28 07:30:38

标签: redis stackexchange.redis redis-cluster

根据文档 here,您将脚本缓存在服务器上,然后使用 LoadedLuaScript 引用来评估它们。配置多个端点/服务器时,评估脚本的正确方法是什么?在字典中缓存第一个 LoadedLuaScript 并使用该单个引用是否正确?见下面的伪代码。

        private IDatabase _db;
        private ConnectionMultiplexer _multiplexer;
        private readonly IDictionary<ScriptType, LoadedLuaScript> _luaScripts = new Dictionary<ScriptType, LoadedLuaScript>;

        public void Initialize(string connection)
        {
            var configuration = ConfigurationOptions.Parse(connection);
            _multiplexer = ConnectionMultiplexer.Connect(configuration);
            _db = _multiplexer.GetDatabase(0);

            // Need to load on all servers?
            foreach (var endpoint in configuration.EndPoints)
            {
                IServer server = _multiplexer.GetServer(endpoint);

                string scriptCode = ScriptHelpers.GetScript(ScriptType.ReleaseHashsetLock);
                var script = LuaScript.Prepare(scriptCode);
                var loadedScript = script.Load(server);

                // Only keep reference to one loaded script?
                if (!_luaScripts.ContainsKey(ScriptType.ReleaseHashsetLock))
                {
                    _luaScripts.Add(ScriptType.ReleaseHashsetLock, loadedScript);
                }
            }
        }

        public async Task CallScript(object parameters)
        {
            // Correct way to call LoadedScript?
            RedisResult result = await _luaScripts[ScriptType.ReleaseHashsetLock]
                .EvaluateAsync(_db, parameters);

            // TODO
        }

0 个答案:

没有答案
相关问题