是否有从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());
}
答案 0 :(得分:1)
您想致电:
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);