使用stack exchange Redis客户端以批处理方式读取所有密钥

使用stack exchange Redis客户端以批处理方式读取所有密钥,redis,stackexchange.redis,Redis,Stackexchange.redis,我正在使用StackExchange.Redis(2.1.58),我使用游标编写下面的代码 Dictionary<string, string> keyResult = new Dictionary<string, string>(); var Server = Connection.GetServer(Connection.GetEndPoints()[0]); long current_cursor

我正在使用StackExchange.Redis(2.1.58),我使用游标编写下面的代码

Dictionary<string, string> keyResult = new Dictionary<string, string>();

                var Server = Connection.GetServer(Connection.GetEndPoints()[0]);

                long current_cursor = 0;

                int next_cursor =-1;
                long page_size = 10;

                while(next_cursor!=0)
                {   
                        var allkeys = Server.Keys(RedisCache.Database, argKeyPattern, 10, 0, next_cursor==-1 ? 0 : next_cursor);


                        var cursor = ((StackExchange.Redis.IScanningCursor)allkeys);

                        foreach (var key in allkeys)
                        {
                            if (current_cursor == cursor.Cursor)

                            {
                                keyResult.Add(key, RedisCache.StringGet(key));

                            }
                            else
                            {
                                next_cursor = Convert.ToInt32(cursor.Cursor);
                                current_cursor = next_cursor;

                                break;

                            }
                        }
                    }
Dictionary keyResult=newdictionary();
var Server=Connection.GetServer(Connection.GetEndPoints()[0]);
长电流_光标=0;
int next_cursor=-1;
长页_大小=10;
while(下一个光标!=0)
{   
var allkeys=Server.Keys(RedisCache.Database,argKeyPattern,10,0,next\u cursor==-1?0:next\u cursor);
var cursor=((StackExchange.Redis.IScanningCursor)所有键);
foreach(所有键中的var键)
{
if(当前光标==光标.cursor)
{
添加(key,RedisCache.StringGet(key));
}
其他的
{
next\u cursor=Convert.ToInt32(cursor.cursor);
当前光标=下一个光标;
打破
}
}
}
这段代码运行得很好,我的问题是,有没有其他方法可以更高效地从Redis批处理读取密钥? 谢谢

Keys(…)方法值得特别一提:它不寻常,因为它没有*异步对应项。原因是,在幕后,系统将确定最合适的使用方法(键vs扫描,基于服务器版本),如果可能,将使用扫描方法将IEnumerable交还给您,该IEnumerable在内部执行所有分页操作,因此您无需查看游标操作的实现细节。如果扫描不可用,它将使用密钥,这可能导致服务器阻塞

乍一看,应该避免使用
命令。但是,如果命令可用,库已经通过使用
SCAN
为您修复了该问题。所以我觉得你在这里很好

如果扫描命令不可用,则
Keys()
将退回到使用
Keys
命令


SCAN
命令在版本2.8++

中可用,但目前无论页面大小如何,它都不应用分页,这就是我需要使用的原因cursor@Dilip检查redis服务器的版本可能吗?它说,如果available@Dilip更新答案。因为您使用的是redis 2.1,所以SCAN命令不可用,所以它会退回到KEYS命令My bad,2.1是lib版本。但无论如何,请再次检查redis服务器hi@TuanAnhTran是的,我给出了我的lib版本,让我检查我的服务器版本,将更新您。谢谢