I’m using the Stack Exchange Redis client, I have recently been using batches to quickly set hashes or delete hashes. However, I’m not sure the best way to get hashes in batches and still be able to correlate them with the redis keys I used to send. here is an example:
private async Task<IDictionary<string, string>> HashGetBulkAsync(IEnumerable<string> keys, string fieldName)
{
var batch = Redis.GetDatabase().CreateBatch();
var results = new Dictionary<string, string>();
var tasks = new List<Task<RedisValue>>();
foreach (var key in keys)
{
// I tried to wrap the "batch.HashGetAsync(key, fieldName)" into another task function
// that also returns the key, but the results are very slow
tasks.Add(batch.HashGetAsync(key, fieldName));
}
batch.Execute();
var taskResults = await Task.WhenAll(tasks).ConfigureAwait(false);
// This section seems wrong. I'm not sure if I can guarantee the same order
var i = 0;
foreach (var key in keys)
{
results.Add(key, taskResults[i]);
i++;
}
return results;
}
Does someone know a better way to do this?