Redis可以禁用流水线命令的答复吗? [英] Can redis disable the replies for pipelined commands?

查看:102
本文介绍了Redis可以禁用流水线命令的答复吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently developing a cache that needs increase a few hundred counters for every call like this:

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

现在,在配置文件中,我看到redis gem仍收集了我不需要的答复,并浪费了一些宝贵的时间.我可以通过某种方式告诉Redis我对回复不感兴趣吗?有没有更好的方法来增加很多值.

In my profiling now I saw that the replies I don't need are still collected by the redis gem and waste some valueable time. Can I tell redis in some way that I'm not interested in the replies? Is there a better way to increment lots of values.

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

I didn't find a MINCR command, for example..

提前谢谢!

推荐答案

是的,至少在2.6中.您可以在LUA脚本中执行此操作,只需让LUA脚本返回空结果即可.这是使用booksleeve客户端的:

Yes... in 2.6, at least. You could do this in a LUA script, and simply have the LUA script return an empty result. Here it is using the booksleeve client:

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"数字作为参数传递,将中间部分更改为:

Or to do the same thing with incrby, passing the "by" numbers as arguments, change the middle portion to:

// 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

这篇关于Redis可以禁用流水线命令的答复吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆