StackExchange.Redis: Batch access for multiple hashes

一曲冷凌霜 提交于 2019-12-08 04:17:33

问题


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?

  1.         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();

回答1:


On performance: have you timed them?

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.



来源:https://stackoverflow.com/questions/28572388/stackexchange-redis-batch-access-for-multiple-hashes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!