2017-07-21 68 views
0

有沒有一種方法可以在Redis中執行MSET StackExchange.Redis。MSET在StackExchange.Redis

在提到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

要撥打:

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

這個不錯。我會測試它並將其標記爲答案。我們是否有類似的將多個鍵/值對添加到集合?我認爲它在redis中也不可用(https://redis.io/commands#set)。在這種情況下,我可以使用流水線[https://stackexchange.github.io/StackExchange.Redis/PipelinesMultiplexers]。 – Brij

+0

您可能最好爲@Brij創建一個全新的問題。 – mjwills