0

Given a list of Keys, I want to pull out multiple values from Azure Redis Cache. How do we perform multiple operations at the same time with Azure Redis Cache?

Our data is a int/ComplexObject pair. Our data is located in SQL Server. We currently get the list by converting our List<int> of keys into a XElement object and passing that into a stored procedure - but our key size is quite small (3000 keys) - so the same data is being accessed again and again by multiple users.

It would be great if we can just cache the 3000 key/value pairs once - and then access them with something like: cache.GetValues(List<int> keys)

Simcha Khabinsky
  • 1,970
  • 2
  • 19
  • 34

2 Answers2

1

There is nothing special for Azure Redis cache. You would want to do the transaction operation supported in Redis as shown here http://redis.io/topics/transactions If you using Stack Exchange Redis client then you can refer to this page https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/Transactions.md

pranav rastogi
  • 4,124
  • 23
  • 23
1

Look at the MGet (http://redis.io/commands/mget) and MSet (http://redis.io/commands/mset) functionality that Redis has. These are supported on the StackExchange.Redis client.

private static void MGet(CancellationToken cancellationToken)
    {
        var pairs = new KeyValuePair<RedisKey, RedisValue>[] {
            new KeyValuePair<RedisKey,RedisValue>("key1", "value1"),
            new KeyValuePair<RedisKey,RedisValue>("key2", "value2"),
            new KeyValuePair<RedisKey,RedisValue>("key3", "value3"),
            new KeyValuePair<RedisKey,RedisValue>("key4", "value4"),
            new KeyValuePair<RedisKey,RedisValue>("key5", "value5"),
        };

        var keys = pairs.Select(p => p.Key).ToArray();

        Connection.GetDatabase().StringSet(pairs);

        var values = Connection.GetDatabase().StringGet(keys);
    }

You will want to keep in mind that getting or setting too many keys on a single command can lead to performance problems.

JonCole
  • 2,902
  • 1
  • 17
  • 19