StackExchange.Redis icon indicating copy to clipboard operation
StackExchange.Redis copied to clipboard

How to retrieve multiple values in list if we pass multiple Keys in a list ?

Open Vijay27anand opened this issue 12 years ago • 7 comments

I have a requirement of passing a List of keys and I need to get the list of values for that keys. Is it possible to retrieve bulk of key values?

cache.Set("A", Object1); cache.Set("B", Object2); List<KeyValuePair<string, object>> redisbulkretrieve; List sKeys = new List(); sKeys.Add("A"); sKeys.Add("B");

(List<KeyValuePair<string, object>>)cache.get(sKeys) --> doesnot work..

can you help how to retrieve multiple values as list if we pass multiple Keys as list.

Vijay27anand avatar Sep 30 '14 09:09 Vijay27anand

And what is "cache" in this example? The IDatabase API certainly has a multi-key API here:

RedisValue[] StringGet(RedisKey[] keys, CommandFlags flags = CommandFlags.None);

and a matching Async version; however, without knowledge of your "cache" type, I can't comment - because that doesn't look like SE.Redis' API

mgravell avatar Sep 30 '14 10:09 mgravell

IDatabase cache = connection.GetDatabase();

I need to retrieve multiple list of Objects stored in Redis by sending a list of String keys.

Vijay27anand avatar Sep 30 '14 11:09 Vijay27anand

So:

var keys = new RedisKey[] { "A", "B" };
var values = cache.StringGet(keys);
var a = (string)values[0];
var b = (byte[])values[1];

or similar

mgravell avatar Sep 30 '14 12:09 mgravell

I used RedisValue[] StringGet(RedisKey[] keys, CommandFlags flags = CommandFlags.None); with bulk record (>1milion) then it not work and show message "this operation would involve too many arguments mget"

TranNghia1998 avatar Dec 24 '21 07:12 TranNghia1998

Well ... yes, don't do that. Batch your data into multiple calls with some reasonable number of keys per call. "Reasonable" depends on the size of each payload (there's a difference between fetching 1kB values vs 200MiB values), but I'd probably start with 1024 keys per call and see how it behaves.

mgravell avatar Dec 24 '21 08:12 mgravell

Well ... yes, don't do that. Batch your data into multiple calls with some reasonable number of keys per call. "Reasonable" depends on the size of each payload (there's a difference between fetching 1kB values vs 200MiB values), but I'd probably start with 1024 keys per call and see how it behaves.

I having issue copy database to server another. Can you solve help me?

TranNghia1998 avatar Dec 24 '21 08:12 TranNghia1998

Is just coping the RDB file sufficient? If not: crawl the keyspace via GetServer() and .Keys(...), and for each key: use KeyDump[Async] and KeyTimeToLive[Async] on the source and KeyRestore[Async] on the target.

(the advantage of this is that you don't need to know what type of value is behind each key - string, hash, etc)

mgravell avatar Dec 24 '21 11:12 mgravell