Tho*_*ser 5 pipeline reply redis
我正在开发一个缓存,需要为每个调用增加几百个计数器,如下所示:
redis.pipelined do
keys.each{ |key| redis.incr key }
end
Run Code Online (Sandbox Code Playgroud)
在我的分析中,我看到我不需要的回复仍然被redis宝石收集并浪费了一些有价值的时间.我能以某种方式告诉redis我对回复不感兴趣吗?有没有更好的方法来增加很多值.
我没有找到MINCR
命令,例如..
提前致谢!
是的......至少在2.6中.您可以在LUA脚本中执行此操作,只需让LUA脚本返回空结果即可.这是使用booksleeve客户端:
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");
Run Code Online (Sandbox Code Playgroud)
或者做同样的事情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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
705 次 |
最近记录: |