简体   繁体   English

如何在 c# 中使用 redis 管道(StackExchange.Redis)?

[英]How to use redis pipiline(StackExchange.Redis) in c#?

I have currently tested redis-benchmark on my linux system and was impressed by the results.我目前已经在我的 linux 系统上测试了 redis-benchmark,结果给我留下了深刻的印象。 But while benchmarking, I used pipelining of 16 commands.但是在进行基准测试时,我使用了 16 个命令的流水线。 Now I am trying to execute it on c#.现在我试图在 c# 上执行它。 My main problem is I want to log some thousands of random data into redis and I can't figure how to used pipelining with this.我的主要问题是我想将数千个随机数据记录到 redis 中,但我不知道如何使用流水线。

Thanks in advance.提前致谢。

The most explicit way to use pipelining in StackExchange.Redis is to use the CreateBatch API:在 StackExchange.Redis 中使用流水线的最明确方法是使用CreateBatch API:

var db = conn.GetDatabase();
var batch = db.CreateBatch();
// not shown: queue some async operations **without** awaiting them (yet)
batch.Execute(); // this sends the queued commands
// now await the things you queued

however, note that you can achieve a lot without that, since:但是,请注意,如果没有它,您可以取得很多成就,因为:

  • concurrent load from different threads (whether sync or async) is multiplexed , allowing effective sharing of a single connection来自不同线程(无论是同步还是异步)的并发负载是多路复用的,允许有效共享单个连接
  • the same trick of "issue multiple async operations but don't await them just yet" still works fine even without the batch API (using the batch API ensures that the batch is sent as a contiguous block without work from concurrent threads getting interleaved within the batch; this is similar to, but less strict than, the CreateTransaction() API)即使没有批处理API (使用批处理 API 可确保批处理作为连续块发送,而无需来自并发线程的工作在批处理;这类似于CreateTransaction() API,但没有那么严格)

Note also that in some bulk scenarios you might also want to consider Lua ( ScriptEvaluate() );另请注意,在某些批量场景中,您可能还需要考虑 Lua ( ScriptEvaluate() ); this API is varadic, so can adapt to arbitrary argument lengths - your Lua simply needs to inspect the sizes of KEYS and ARGV ( discussed in the EVAL documentation ).这个 API 是可变的,因此可以适应任意参数长度 - 您的 Lua 只需要检查KEYSARGV的大小(EVAL文档中讨论)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM