Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I HGET from many hashes in one request in StackExchange.Redis?

I have like 1000 hashes, and some of them has the same key, i.e.

Hash1 = {1: 1, 2: 1, 3: 5, 4: 7...}
Hash2 = {1: 4, 3: 5, 9: 7...}
Hash3 = {5: 4, 8: 5, 9: 7...}
...

I want to get all key 1 values in all hashes. I can do this:

List<value> values = new();
foreach (string hashKey in hashKeys)
{
  values.Add(await _client.HashGetAsync(hashKey, "1"));
}

return values;

But this do network to Redis hashKeys.Count() times. Can I do this in only one network request?

like image 479
Romulus Urakagi Ts'ai Avatar asked Jan 27 '26 09:01

Romulus Urakagi Ts'ai


1 Answers

Not directly via an inbuilt API - there is no single redis API that achieves this. However, you could use Lua (ScriptEvaluate), passing in multiple keys, looping inside the Lua code, and building a result array by calling hget repeatedly.

like image 83
Marc Gravell Avatar answered Jan 28 '26 23:01

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!