2015-02-24 66 views
0

给出一个键列表,我想从Azure Redis缓存中提取多个值。 我们如何与Azure Redis Cache同时执行多个操作?Azure Redis缓存批量操作/多个操作

我们的数据是一个int/ComplexObject对。我们的数据位于SQL Server中。我们目前通过将我们的密钥List<int>转换为XElement对象并将其传递给存储过程来获取列表,但我们的密钥大小很小(3000个密钥),因此同一数据一次又一次地被多个用户访问。

这将是巨大的,如果我们可以只缓存3000键/值对一次 - 然后用类似访问它们:cache.GetValues(List<int> keys)

回答

1

看那MGET(http://redis.io/commands/mget)和MSET(http://redis.io/commands/mset)功能显示的Redis具有。这些在StackExchange.Redis客户端上受支持。

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); 
    } 

您将需要记住,在单个命令上获取或设置太多密钥可能会导致性能问题。