I have a list of keys
that I want to get from Redis
. I wrote a function like this but it returns everything:
public IOrderedEnumerable<Fields> GetValues(List<string> symbols)
{
var retVal = new List<Fields>();
var patternStr = "[";
int count = 0;
foreach (var symbol in symbols)
{
patternStr += (symbol);
if (++count != symbols.Count)
{
patternStr += ", ";
}
}
patternStr += "]*";
foreach (var ep in redis.GetEndPoints())
{
var server = redis.GetServer(ep);
var keysList = server.Keys(database: 0, pattern: patternStr).ToList();
var keys = keysList.ToArray();
Console.WriteLine("Number of Symbols in this range{0} ", keys.Length);
foreach (var rk in keys)
{
var myValTask = db.StringGetAsync(rk.ToString());
var myVal = myValTask.Result;
var jsonStr = myVal.ToString();
...
}
...
}
...
The section of code I believe is the problem. I just want Redis
to return the subset
of keys
, and I am building a pattern by seperating them by "," :
var patternStr = "[";
int count = 0;
foreach (var symbol in symbols)
{
patternStr += (symbol);
if (++count != symbols.Count)
{
patternStr += ", ";
}
}
patternStr += "]*";
I know I can get all the keys, then filter them once I get them, but I want to avoid the network thrashing…
Edit 1
BTW, the keys look like this:
127.0.0.1:6379> keys *
1) "BBWI_2022-08-19"
2) "ABBV_2023-01-20"
3) "ZTS_2022-10-21"
2
Answers
If I understand correctly, you have a set of discrete keys that you want to fetch in a single batch. If so, you can fetch all items (within reason, say < 1000 at a time) by passing a
RedisKey[]
array toStringGetAsync
:You can use
Scan
command on redis server like this:and in your case would be something like this:
But I recommend to use
Scan
in very special scenarios because it acts as a cursor and will iterate between all keys in redis to find match and also please read this https://redis.io/commands/scanTest