簡體   English   中英

StackExchange.Redis中的MSET

[英]MSET in StackExchange.Redis

是否有一種方法可以從MSET在Redis中執行MSET

在參考文檔之后 ,我編寫的以下代碼執行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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM