StackExchange.Redis中的MSET

时间:2017-07-21 12:34:04

标签: c# redis stackexchange.redis

是否有从StackExchange.Redis在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());
  }

1 个答案:

答案 0 :(得分:1)

您想致电:

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