[英]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
行為)。
/// <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.