StackExchange.Redis:批量访问多个哈希 [英] StackExchange.Redis: Batch access for multiple hashes
问题描述
因此,我需要大量访问许多不同的哈希值(在StackExchange.Redis中,我具有不同的RedisKey的哈希值).
So I need to access in bulk many different hashes (in StackExchange.Redis, I have different RedisKey's).
最好(最快)的方法是什么? 例如,对于这两种可能的实现,是正确的吗?哪个效果更好?
What is the best (fastest) way to do it? For example, for these two possible implementations, is either correct? Which one works better?
List<Task<HashEntry[]>> list = new List<Task<HashEntry[]>>();
List<RedisKey> keys; //Previously initialized list of keys
foreach (var key in keys)
{
var task = db.HashGetAllAsync(key);
list.Add(task);
}
await Task.WhenAll(list);
2.
List<Task<HashEntry[]>> list = new List<Task<HashEntry[]>>();
List<RedisKey> keys; //Previously initialized list of keys
IBatch batch = db.CreateBatch();
foreach (var key in keys)
{
var task = batch.HashGetAllAsync(key);
list.Add(task);
}
batch.Execute();
推荐答案
性能方面:您为它们计时了吗?
On performance: have you timed them?
除此之外:两者都起作用,并且具有不同的权衡;例如,后者是同步的-避免了 all 的TPL开销和复杂性,可以从中受益.您可能还需要考虑第三个选项-一个Lua脚本,该脚本接受键的数组作为输入,并为每个键调用HGETALL
.
Other than that: both work, and have different trade-offs; the latter is synchronous, for example - bit benefits from avoiding all of the TPL overheads and complications. You might also want to consider a third option - a Lua script that accepts and array of keys as input and invokes HGETALL
for each.
这篇关于StackExchange.Redis:批量访问多个哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!