StackExchange.Redis 中的 MSET

MSET in StackExchange.Redis

有没有办法在 Redis 中从 StackExchange.Redis 执行 MSET

参考documentation后,我写的下面的代码是执行StringSetAsync在Redis中添加多个键值对。我们有类似 IDatabase.StringSet(RedisKey[], RedisValue[]) 的东西吗?

  public void Add(IEnumerable<CacheKeyValue> cacheKeyValues)
  {
      var tasks = new List<Task>();

      foreach(var kv in cacheKeyValues.ToList())
      {
          tasks.Add(((Task<bool>)DB.StringSetAsync(kv.Key, ((RedisValue)kv.Value))).ContinueWith((b) => kv.Status = true));
      }

      Task.WaitAll(tasks.ToArray());
  }

您想打电话给:

bool StringSet(KeyValuePair<RedisKey, RedisValue>[] values, When when = When.Always, CommandFlags flags = CommandFlags.None);

但仅传递第一个参数(这意味着第二个和第三个参数是默认参数,这意味着您将获得 MSET 行为)。

根据https://github.com/StackExchange/StackExchange.Redis/blob/c4c9c1fdb455070415e82d2f104fc89a90b057b5/StackExchange.Redis/StackExchange/Redis/IDatabase.cs

/// <summary>
/// Sets the given keys to their respective values. If "not exists" is specified, this will not perform any operation at all even if just a single key already exists.
/// </summary>
/// <returns>True if the keys were set, else False</returns>
/// <remarks>http://redis.io/commands/mset</remarks>
/// <remarks>http://redis.io/commands/msetnx</remarks>
bool StringSet(KeyValuePair<RedisKey, RedisValue>[] values, When when = When.Always, CommandFlags flags = CommandFlags.None);

还有一个async等价物:

/// <summary>
/// Sets the given keys to their respective values. If "not exists" is specified, this will not perform any operation at all even if just a single key already exists.
/// </summary>
/// <returns>True if the keys were set, else False</returns>
/// <remarks>http://redis.io/commands/mset</remarks>
/// <remarks>http://redis.io/commands/msetnx</remarks>
Task<bool> StringSetAsync(KeyValuePair<RedisKey, RedisValue>[] values, When when = When.Always, CommandFlags flags = CommandFlags.None);