Skip to content

Commit 5df838e

Browse files
committed
Creating ExecuteInTransaction function
1 parent aab0b67 commit 5df838e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Diff for: tests/NRedisStack.Tests/RedisFixture.cs

+22
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace NRedisStack.Tests
55
{
66
public class RedisFixture : IDisposable
77
{
8+
private readonly IDatabase _db; // TODO: Check if its right to have this here
9+
810
public RedisFixture() => Redis = ConnectionMultiplexer.Connect("localhost");
911

1012
public void Dispose()
@@ -13,5 +15,25 @@ public void Dispose()
1315
}
1416

1517
public ConnectionMultiplexer Redis { get; private set; }
18+
19+
/// <summary>
20+
/// Executes the contained commands within the context of a transaction.
21+
/// </summary>
22+
/// <param name="commandArgsTuples">each tuple represents a command and
23+
/// it's arguments to execute inside a transaction.</param>
24+
/// <returns>A RedisResult.</returns>
25+
public RedisResult[] ExecuteInTransaction(Tuple<string, string[]>[] commandArgsTuples)
26+
{
27+
var transaction = _db.CreateTransaction();
28+
var tasks = new List<Task<RedisResult>>();
29+
foreach (var tuple in commandArgsTuples)
30+
{
31+
tasks.Add(transaction.ExecuteAsync(tuple.Item1, tuple.Item2));
32+
}
33+
34+
transaction.Execute();
35+
Task.WhenAll(tasks).Wait();
36+
return tasks.Select(x => x.Result).ToArray();
37+
}
1638
}
1739
}

0 commit comments

Comments
 (0)