gpt4 book ai didi

c# - StackExchange.Redis 中的 MSET

转载 作者:可可西里 更新时间:2023-11-01 11:17:03 24 4
gpt4 key购买 nike

有没有一种方法可以通过 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());
}

最佳答案

你想调用:

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

关于c# - StackExchange.Redis 中的 MSET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45237837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com