【问题标题】:Can redis disable the replies for pipelined commands?redis 可以禁用流水线命令的回复吗?
【发布时间】:2018-01-12 21:37:44
【问题描述】:

我目前正在开发一个缓存,需要为每个调用增加几百个计数器,如下所示:

redis.pipelined do
  keys.each{ |key| redis.incr key }
end

现在在我的分析中,我看到我不需要的回复仍然由 redis gem 收集,浪费了一些宝贵的时间。我可以以某种方式告诉redis我对回复不感兴趣吗?有没有更好的方法来增加很多值。

例如,我没有找到MINCR 命令..

提前致谢!

【问题讨论】:

    标签: redis pipeline reply


    【解决方案1】:

    是的……至少在 2.6 中。您可以在 LUA 脚本中执行此操作,并且只需让 LUA 脚本返回一个空结果。这里使用的是 bookleeve 客户端:

    const int DB = 0; // any database number
    // prime some initial values
    conn.Keys.Remove(DB, new[] {"a", "b", "c"});
    conn.Strings.Increment(DB, "b");
    conn.Strings.Increment(DB, "c");
    conn.Strings.Increment(DB, "c");
    
    // run the script, passing "a", "b", "c", "c" to
    // increment a & b by 1, c twice
    var result = conn.Scripting.Eval(DB,
        @"for i,key in ipairs(KEYS) do redis.call('incr', key) end",
        new[] { "a", "b", "c", "c"}, // <== aka "KEYS" in the script
        null); // <== aka "ARGV" in the script
    
    // check the incremented values
    var a = conn.Strings.GetInt64(DB, "a");
    var b = conn.Strings.GetInt64(DB, "b");
    var c = conn.Strings.GetInt64(DB, "c");
    
    Assert.IsNull(conn.Wait(result), "result");
    Assert.AreEqual(1, conn.Wait(a), "a");
    Assert.AreEqual(2, conn.Wait(b), "b");
    Assert.AreEqual(4, conn.Wait(c), "c");
    

    或者要对incrby 做同样的事情,将“by”数字作为参数传递,将中间部分更改为:

    // run the script, passing "a", "b", "c" and 1, 1, 2
    // increment a & b by 1, c twice
    var result = conn.Scripting.Eval(DB,
        @"for i,key in ipairs(KEYS) do redis.call('incrby', key, ARGV[i]) end",
        new[] { "a", "b", "c" }, // <== aka "KEYS" in the script
        new object[] { 1, 1, 2 }); // <== aka "ARGV" in the script
    

    【讨论】:

    • 太好了!完全忽略了脚本方法。非常感谢!
    【解决方案2】:

    不,这是不可能的。没有办法告诉 Redis 不回复。

    避免在某些时候同步等待回复的唯一方法是运行完全异步的客户端(例如异步模式下的 node.js 或hiredis)。

    【讨论】:

    • 在 2.6 中很有可能......
    【解决方案3】:

    Redis 3.2 版明确支持这一点:

    https://redis.io/commands/client-reply

    CLIENT REPLY 命令控制服务器是否会回复客户端的命令。可以使用以下模式: 上。这是服务器对每个命令都返回回复的默认模式。 离开。在这种模式下,服务器不会回复客户端命令。 跳过。此模式会跳过命令的回复。

    返回值 当使用 OFF 或 SKIP 子命令调用时,不会做出响应。使用 ON 调用时: 简单字符串回复:OK。

    【讨论】:

    • 如何在 Redis CLI 之外运行此命令?有没有实现它的库?
    • @SeanO 我不确定hiredis 库是否足够聪明,知道如果上面的命令在真正的命令之前就不用等待回复。
    猜你喜欢
    • 2019-01-22
    • 2018-10-13
    • 2014-04-04
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    相关资源
    最近更新 更多