【发布时间】:2021-05-30 11:08:21
【问题描述】:
我正在尝试执行包含两个操作的基本事务操作
- 获取集合的长度
scard MySet - 弹出给定长度的整个集合:
spop MySet len
我知道可以连续使用smembers 和del。但我想要实现的是获取第一个操作的输出并在第二个操作中使用它并在事务中执行。到目前为止,这是我尝试过的:
var transaction = this.cacheClient.Db1.Database.CreateTransaction();
var itemLength = transaction.SetLengthAsync(key).ContinueWith(async lengthTask =>
{
var length = await lengthTask;
try
{
// here I want to pass the length argument
return await transaction.SetPopAsync(key, length); // probably here transaction is already committed
// so it never passes this line and no exceptions thrown.
}
catch (Exception ex)
{
throw;
}
});
await transaction.ExecuteAsync();
另外,我用CreateBatch 尝试了同样的事情并得到了同样的结果。我目前正在使用上面提到的解决方法。我知道也可以评估 Lua 脚本,但我想知道事务是否可能,或者我做错了什么。
【问题讨论】:
标签: c# redis stackexchange.redis