【问题标题】:Azure Redis Cache Batch Operations / Multiple OperationsAzure Redis 缓存批处理操作/多个操作
【发布时间】:2015-02-24 17:10:53
【问题描述】:

给定一个键列表,我想从 Azure Redis 缓存中提取多个值。 我们如何使用 Azure Redis 缓存同时执行多个操作?

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

如果我们可以一次缓存 3000 个键/值对,那就太好了 - 然后通过类似以下方式访问它们:cache.GetValues(List<int> keys)

【问题讨论】:

    标签: azure-caching azure-redis-cache


    【解决方案1】:

    Azure Redis 缓存没有什么特别之处。您可能希望执行 Redis 中支持的事务操作,如下所示http://redis.io/topics/transactions 如果你使用 Stack Exchange Redis 客户端,那么你可以参考这个页面https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/Transactions.md

    【讨论】:

      【解决方案2】:

      查看 Redis 具有的 MGet (http://redis.io/commands/mget) 和 MSet (http://redis.io/commands/mset) 功能。 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);
          }
      

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

      【讨论】:

        猜你喜欢
        • 2015-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-04
        • 1970-01-01
        • 2018-05-19
        • 2012-01-29
        • 1970-01-01
        相关资源
        最近更新 更多