Py学习  »  Redis

在C中获取redis lua脚本输出#

Pankaj Rawat • 4 年前 • 314 次点击  

在实现lua脚本之前,我在循环中读取散列,有时会得到一个连接错误。

为了减少网络呼叫,我尝试了lua脚本(如下),它正在工作

local result = {}
local ruletypes = @RuleTypes
local assetId = @AssetId

for rule in ruletypes:gmatch("([^,]+)") do
local ruleType = rule..'_hash'
local val = redis.call('HGET', ruleType, assetId)
result[#result + 1] = val
end

return result

缓存服务 执行lua脚本

public class CacheService
{
    private readonly IDatabase cache;
    private static readonly string connectionString = ConfigurationManager.AppSettings["RedisConnection"];
    private static LoadedLuaScript ruleLuaScript;
    public CacheService()
    {
        this.cache = Connection.GetDatabase();
        ruleLuaScript = LoadLuaScriptForGetRule(@"Lua/getMultiHashWithParameter.lua");
    }

    private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
    {
        return ConnectionMultiplexer.Connect(connectionString);
    });

    public static ConnectionMultiplexer Connection
    {
        get
        {
            return lazyConnection.Value;
        }
    }        

    private LoadedLuaScript LoadLuaScriptForGetRule(string scriptUrl)
    {
        string script = File.ReadAllText(scriptUrl);
        LuaScript luaScript = LuaScript.Prepare(script);
        var endpoints = Connection.GetEndPoints();
        var endpoint = endpoints[0];
        IServer server = Connection.GetServer(endpoint);
        return luaScript.Load(server);
    }
    public void PrepareLuaScript(dynamic parameters)
    {
        RedisResult result = this.cache.ScriptEvaluate(ruleLuaScript, parameters);
        if (!result.IsNull)
        {
            //how to read result?
        }
    }
}

luascript输出

enter image description here

问题

  1. 我不确定,注册lua脚本是否正确?

  2. 如何从redisresult中读取json数据?有没有通用的解决方案?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/44184
 
314 次点击